|
I am trying to add an instantated class (which is an Object - I hope!) to an Arraylist every time the data in the class changes. This instantated object is composed of several other instantated classes (objects).
Example:
ArrayList myObj = new ArrayList();
class foo;
foo.strName = "name";
myObj.Add(foo);
...
foo.strName = "newname";
myObj.Add(foo);
When I look (via QuickWatch) at the Arraylist elements, they all appear to be the same, i.e.,
myObj[0] foo.strName == "newname" &&
myObj[1] foo.strName == "newname".
Yet, I know the data in the class has changed.
Am I doing something that is invalid? What am I doing wrong?
Thanks
|
|
|
|
|
You need to instantate a new object every time. If you just change a member of the object you still have the same object it doesn't automagically create a new object and it'll still be the same object that is already in your ArrayList.
|
|
|
|
|
hi there,
it sounds to me that u are adding only one object to array list.
and actually the reference to the object stored in the arraylist. So whenever u change it's attribute, all objectes in the arraylist will take affect, I think.
So the code should be:
class foo = new cfoo();
foo.strName="name";
obj.Add(foo);
....
foo = new cfoo();
...
obj.Add(foo);
Try it
<< >>
|
|
|
|
|
In your code, you did not instantiate a new foo object, instead you are modifying the foo.strName only.
An ArrayList stores reference to your object, therefore if you store two reference to the same object they will result in the same object.
Try this instead:
ArrayList myObj = new ArrayList();
ClassName foo = new ClassName();
foo.strName = "name";
myObj.Add(foo);
...
foo = new ClassName();
foo.strName = "newname";
myObj.Add(foo);
This will result in two different objects which references are contained in your myObj ArrayList.
Does that help?
Edbert P.
Sydney, Australia
-- modified at 18:58 Wednesday 26th October, 2005
|
|
|
|
|
Thanks to All,
My problem was not quite as simple as I showed in my example (i.e., I have multiple embedded classes involved), but all of you put me on the right track. I didn't realize that a reference to my class was what was put into the Arraylist.
I am able to do what I originally wanted, but had to do it a different way.
Thanks
Don
|
|
|
|
|
After a day of scratching my head, I think I've found the problem with my latest project, however I'm not sure what to do about it.
I've wrapped a legacy DLL with a .NET remoting solution that allows the functions to be accessed easily from ASP.NET. The DLL is used for reporting.
On my test system, with demo configuration and minimal data, everything worked fine.
At the clients site, where the reports are larger, the remoting service returns a null reference exception when the reports are over 4k.
Here is functions P/Invoke signature:
[DllImport("opsrpts.dll", CallingConvention=CallingConvention.Cdecl)]
static extern String DisplayReport(int rvc, int wsobj, int rptnum, int period, int scope, int reset, int range, int start, int end, int suppress);
and the C declaration:
extern "C" DLLIMP_DBI char* __cdecl DisplayReport(int rvc, int wsobj, int rptnum, int period, int scope, int reset, int range, int start, int end, int suppress)
I'm assuming that the attempt to append more than 4k in a single string operation blows something up somewhere. At this point it's the only logical explaination I have.
Is there any way other than modifying the report to get the larger reports back? Since there is no way to just take the char* and work with it, I can't think of a way other than changing the DLL to use multiple calls to pull the data back in chunks.
Thanks in advance!
|
|
|
|
|
Mark Tutt wrote: char* __cdecl DisplayReport
Shoot the person that wrote that function, shoot him dead. People like that should not be allowed behind a keyboard. Suggest to him in a nice way that he should join the police academy.
Alternatively if you run out of bullets, or dont have any handy, try the StringBuilder class instead.
The correct way would be to get the value for that char*, then do Marshal.PtrToString , then pray the person that wrote the DLL used the default malloc/free mechanism. PInvoke free() passing the char* to prevent memory leaks.
xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!
|
|
|
|
|
leppie wrote: Shoot the person that wrote that function, shoot him dead. People like that should not be allowed behind a keyboard. Suggest to him in a nice way that he should join the police academy.
Alternatively if you run out of bullets, or dont have any handy, try the StringBuilder class instead.
Now now, the DLL in question is part of a legacy codebase that goes back nearly 15 years, and is pretty much exclusively C/ASM. Gotta give a little respect to the people who wrote the stuff that the world runs on.
I'll take a look at Marshal.PtrToString...
|
|
|
|
|
Mark Tutt wrote: pretty much exclusively C/ASM.
Even for C that is bad.
Mark Tutt wrote: Gotta give a little respect to the people who wrote the stuff that the world runs on.
Now I'm getting more worried...
xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!
|
|
|
|
|
Hi all, I'm new to this forum and to c# and my first project is a simple calendar for my tablet pc. In this project I dynamicly create buttons based on current month, looks like this:
private void createButtons()
{
int xet;
int numFirstDay;
DateTime myDateTime = new DateTime(activeYear,activeMonth,1);
if(myDateTime.DayOfWeek==DayOfWeek.Monday)
{
//functions for checking witch day in week the first day is }
int yet=569; int numdays =
DateTime.DaysInMonth(activeYear,activeMonth);
for(int i=1; i350)
{
yet+=20;
xet=16;
}
p.Y=yet;
p.X=xet;
aButton.Location=p;
aButton.Width=50;
aButton.Height=20;
aButton.Tag=i;
aButton.Name="button"+i;
aButton.Text=i.ToString();
if(i==activeDay)
{
aButton.BackColor = System.Drawing.SystemColors.ControlDark;
}
aButton.Click+=new EventHandler(aButton_Click);
this.Controls.Add(aButton);
xet+=50;
}
showDate();
}
This works fine and the buttons works as planned, now to my problem. When I change month from current to upcoming or earlier I like to relocate the buttons (x and y value) and hide for example button 31 if the next month only has 30 days. But I don't know how to adress these buttons from other functions?
Best Regards
Magnus
|
|
|
|
|
hi there,
mwallon wrote: But I don't know how to adress these buttons from other functions
Just suggest: whenever u create a button, u should store it into an arraylist
So u should be able to reference to the button via arraylist
<< >>
|
|
|
|
|
I assume you're missing a aButton = new Button(); from your code somewhere?
If you are dynamically creating buttons in your loop you will need to keep a reference to them to be able to access them again. Putting them in an ArrayList is usually a good solution. Another way to do it would be to iterate through your form's ControlCollection and find your buttons again which is also an option, but if you have a lot of other controls it isn't very efficient. Another alternative would be to add a group box or a panel to your form and put all your buttons in there.
|
|
|
|
|
Yes Wjousts you are right that line disapeard for some reason while I pasted the code.
The arrayList sounded very good but I didn't realy got it to work.
I just triead a simple thing like hidding one button:
btnList[1].Visible=false;
but got an error message saying:
'Object does not contain a definition for visible'
I created the list like this:
ArrayList btnList = new ArrayList();
and add buttons to it like this:
btnList.Add(aButton);
|
|
|
|
|
The problem is that the array list contains objects, not buttons. btnList[1] returns an object that happens to be a button and you are responsible for casting it back to a button. In other words, like this:
<br />
Button b = (Button)btnList[1];<br />
b.Visible = false;<br />
|
|
|
|
|
That explains it =) Thanks alot for the help
|
|
|
|
|
Does anybody know if it's possible to pass a parameter to a remote object constructor using .NET remoting withing having access to the remote objects themselves?
I have a server solution that contains a project with remote objects and a project with remote interfaces, of which the remote objects implement. My client solution only contains the remote interfaces project. I'm currently getting a reference to the interface using the Activator.GetObject() method and making calls on the remote objects. However, I now have a need to pass a parameter into the constructor of the remote object and the only way I can determine that will work is if I share my remote objects project in my client solution, which I don't want to do. Any ideas?
|
|
|
|
|
Adding "constructor parameters" to Activator.GetObject() or any of the .Net Remoting associated calls would add unneeded complexity to .Net Remoting (more on the server side than the client side). If you really need this kind of functionality, I suggestion is just make another initializer function uninispiringly called Initialize that can be overloaded to take any number of params you want.
|
|
|
|
|
That's not good enough because the remote objects are defined to be SingleCall rather than Singletons. If I call Initialize and initialize some variable on the server, it won't be set when I make the second call.
|
|
|
|
|
I assume that what System.Activator.GetObject(Type, String, Object) overload is for. Look in the docs for a special constructor like ISerializable has.
xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!
|
|
|
|
|
Write two functions that accept three arrays as function arguments. All three arrays should be created in the main(). First, prices should be initialized with the following vaules: 9.92, 6.32, 12.63, 5.95, and 10.29 in the main(). Second, the units array should be populated using a loop and the user input from the console in the main(). The first function should store the product of the corresponding values in the prices and units arrays in the amounts array (ex. amounts[1] = prices[1] * units[1]). The second function has to display the report when called?
|
|
|
|
|
|
so what is ur question here?
You might try this ur self first....
<< >>
|
|
|
|
|
Thanks for posting your homework question here. However, no-one here is going to do this for you, and if we did, what would you do when your next assignment comes along.
This looks pretty trivial to me, if you don't understand the task, go and ask your lecturer. If, once you've tried to make it work, you still can't get it right, feel free to ask questions here. Specific questions that show you're trying to learn, and not just trying to get a free ride off the internet.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Exactly where are you stuck? The code for this should be very trival, are you having difficult with the coding or with parsing the question?
|
|
|
|
|
Is there anything such class, or will I have to write my own?
|
|
|
|
|