about arraylist
-
I got a question. i am new in this things so How do you get to the properties of een instance in an arraylist. And it can be different kind of objects in the arraylist.
-
I got a question. i am new in this things so How do you get to the properties of een instance in an arraylist. And it can be different kind of objects in the arraylist.
I cannot get your first question. Arraylist can hold any kind of object. But for a particular instance, it should hold the same kind of object, ie., if you are keeping a string, you have to keep only strings, or if it is integer, you have to keep only integer.
Pradipta Basu
-
I cannot get your first question. Arraylist can hold any kind of object. But for a particular instance, it should hold the same kind of object, ie., if you are keeping a string, you have to keep only strings, or if it is integer, you have to keep only integer.
Pradipta Basu
example object motor() object window() Arraylist carParts carParts.add(motor) carParts.add(window) now i wanna get to the properties of the objects motor and winow in carParts. I wanna change value in those objects.
-
example object motor() object window() Arraylist carParts carParts.add(motor) carParts.add(window) now i wanna get to the properties of the objects motor and winow in carParts. I wanna change value in those objects.
foreach(object part in carParts)
{
if(part is motor)
(part as motor).Cylinders = 12;if(part is window)
(part as window).Electric = true;
}Cast it to access the properties. Or store the object to a new variable. For instance:
Motor newMotor = null;
if(part is Motor)
newMotor = (part as Motor);if(newMotor != null)
{
//access properties via newMotor object
}-Larantz-
for those about to code, we salute you
http://www.tellus-software.com -
foreach(object part in carParts)
{
if(part is motor)
(part as motor).Cylinders = 12;if(part is window)
(part as window).Electric = true;
}Cast it to access the properties. Or store the object to a new variable. For instance:
Motor newMotor = null;
if(part is Motor)
newMotor = (part as Motor);if(newMotor != null)
{
//access properties via newMotor object
}-Larantz-
for those about to code, we salute you
http://www.tellus-software.comor, without looking up type of object twice:
Motor newMotor = null;
if((newMotor = part as Motor) != null)
{
//access properties via newMotor object
}
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
or, without looking up type of object twice:
Motor newMotor = null;
if((newMotor = part as Motor) != null)
{
//access properties via newMotor object
}
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
or, without the confusing assignment-and-cast-plus-equality-in-one
// newMotor is null if myArrayList[0] is not of type Motor Motor newMotor = myArrayList[0] as Motor; if(newMotor != null) { }
IMHO it's just about as confusing as ternary operator. btw, I don't see why would you comment what standart C# operator
as
does?? Oh wait, maybe because from... some lines... if(newMotor != null) { }
it's not immediately clear that you are not testing for null, but for type of object...
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
IMHO it's just about as confusing as ternary operator. btw, I don't see why would you comment what standart C# operator
as
does?? Oh wait, maybe because from... some lines... if(newMotor != null) { }
it's not immediately clear that you are not testing for null, but for type of object...
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
AS is just a no-exception cast. If the cast object is not of the casted type it returns null. Perfect if you want to cast and read a property.
CP ate my response :mad: I know what
as
*operator* does, thank you. However I am missing your point.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus