a basic casting question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm new to C#. This piece of code works fine and needs no explicit cast in
vb.net.

itemId = arrCart(cartId, i)

When I converted it to C#, it complained that it cannot do the implicit
conversion. So I tried:

itemId = (int)arrCart[cartId, i];

But I get an exception error read: Specified cast is not valid.

Can anyone tell me how to correct this?
 
Can anyone tell me how to correct this?

Not without knowing what arrCart returns.


Mattias
 
Sorry.

int itemtId;
object[,] arrCart;

and we can say cartId = 100 and i =0.
 
int itemtId;
object[,] arrCart;

and we can say cartId = 100 and i =0.

OK, then what does arrCart[100,1] contain? The cast (which would
actually be an unboxing operation) will only succeed if it contains a
boxed int.


Mattias
 
Tried Convert.ToInt16 instead of the case and it resoved it. Thanks!

Mattias Sjögren said:
int itemtId;
object[,] arrCart;

and we can say cartId = 100 and i =0.

OK, then what does arrCart[100,1] contain? The cast (which would
actually be an unboxing operation) will only succeed if it contains a
boxed int.


Mattias
 

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