Question about casting

  • Thread starter Thread starter Andy Chen
  • Start date Start date
A

Andy Chen

Hi,

I have a Hashtable, key is string and value is ArrayList. The problem is I
cannot cast the value from object to ArrayList.
like this:

Hashtable ht = new Hashtable();
ArrayList al = new ArrayList();
al.Add("test");
ht.Add("key1", al);

ArrayList al1 = (ArrayList)(Hashtable["key1"]; // .

The result of al1 is a ArrayList which has 1 item. al1[0] was a string value
"system.collections.arraylist", and there is no cast exception throwed.
What's the problem?

Thanks!
Andy
 
You are mistaken.
You have not done what you say that you have (Your lookup line wouldn't
compile)
Check again or post full code.
 
Andy,

The result should be an array list with 1 element - string with text "test".
If you don't get that you do something wrong.

Can you post compilable (preferable console) sample that demonstrates your
problem.
 
Thanks for your respnoses!
I made mistake at somewhere else, the casting is OK.

I apologize for this trash post.

Stoitcho Goutsev (100) said:
Andy,

The result should be an array list with 1 element - string with text
"test". If you don't get that you do something wrong.

Can you post compilable (preferable console) sample that demonstrates your
problem.



--

Stoitcho Goutsev (100)

Andy Chen said:
Hi,

I have a Hashtable, key is string and value is ArrayList. The problem is
I cannot cast the value from object to ArrayList.
like this:

Hashtable ht = new Hashtable();
ArrayList al = new ArrayList();
al.Add("test");
ht.Add("key1", al);

ArrayList al1 = (ArrayList)(Hashtable["key1"]; // .

The result of al1 is a ArrayList which has 1 item. al1[0] was a string
value "system.collections.arraylist", and there is no cast exception
throwed.
What's the problem?

Thanks!
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top