Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to assign a value from an ArrayList element 1

Status
Not open for further replies.

jslmvl

Vendor
Jan 26, 2008
268
0
0
GB
How to assign a value from an ArrayList element? Following will be wrong:
ArrayList al = new ArrayList();
al.Add(12);
int k = al[0];
Thanks a lot for your help.
 
The ArrayList returns Object. You have to cast to the correct type first.

int k = (int)al[0];
 
If you don't mind, I want to ask you more:

What is the difference between cast at the left such as (int)something and at the right such as something.ToString().
 
this will throw an error. CastException to be exact. a string cannot be cast to an integer.
Code:
object o = "1"; //or 1.ToString();
int i = (int)o;
This will not thrown an exception. it will set i to the default value (0). for classes the default value is null
Code:
object o = "1"; //or 1.ToString();
int i = o as int;
something.ToString() is not casting. this is just a returning the string representation of the object. for classes this is usually the full class name. for structs is the value. Some classes override the default ToString() value to return an actual value. An example of this is DateTime. DateTime is a class and calling date.ToString() will return the string value of the date.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
At left they are called casting operators and they change (convert) the System.Type. In the previous example, you tried to do assignment "int <- object". The object had to to be casted on the fly and then saved into "k" variable. The {int}al[0] is now integer but the al[0] is still object. Thats why i said "on the fly".

The .ToString() is a function which returns a string. This exists everywhere and help us to quickly format the output.
Say that you want to format the number to show us 2 decimal digits. Two conversions in one line.

Code:
ArrayList al = new ArrayList();
al.Add(3.4);
MessageBox.Show([b]([/b](double)al[0][b])[/b].ToString("#.00"));

This will show 3.40
Note that the .ToString() is on the double (see bold parentheses)
 
Omg, terrible. I wish there was an Edit button.
On the fly means that i dont save the casted object (to double in this example) to a variable.
 
T0 jmeckley,

Thak you for your help.

int i = o as int; Is this C#?
 
yes

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Is there any reason to be using an ArrayList with generic collections available now?
Code:
var al = new List<int>();
al.Add(12);
int k = al[0];
 
not really. on the rare occasion you do need a ArrayList I would opt for an IDictionary implementation like HashTable or HybridDictionary instead. I would also keep the dictionary encapsulated within the object instantiating it.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
T0 jmeckley,

Thank you. I tried your code:
object o = "1"; //or 1.ToString();
int i = o as int;
and got this:
Error 1 The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type)
 
makes sense, short-sighted on my part. here is another example
Code:
class Foo {}
class Bar{}

class TryMe
{
   public Bar GetExceptionWithCast()
   {
      object o = new Foo();
      return (Bar)o;
   }


   public Bar GetNullValue()
   {
      object o = new Foo();
      return o as Bar;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top