How to Type cast ArrayList items to class objects

B

budy_ludy

Hi All,

I am new to vb .net,

I have an ArrayList and i store class objects in it,
and later i want to retrieve each ArrayList items and type cast to the
class,
How can it be done ?
I used CType for casting but it is throwing exception.


regards
ludy
 
C

Cor Ligthert [MVP]

buddy,

You mean something as
\\\
dim myObject is myClass
myArraylist.Add(myObject)
DirectCast(myArrayList(0),myClass).WhatEver
///
I hope this helps,

Cor
 
B

budy_ludy

Hi Cor

Below is the situation,
I am trying to pass the arraylist from function1 to the function2
which accepts class,
even if i type cast using Direct cast i get exception.


Public class myClass

Function1()
-----------
dim myObject is myClass
myArraylist.Add(myObject)

Function2(myArraylist.Item(0))
.......
.......
.......


Function2(myClass )
 
G

GhostInAK

Hello budy_ludy,

What Exception exactly?

-Boo
Hi Cor

Below is the situation,
I am trying to pass the arraylist from function1 to the function2
which accepts class,
even if i type cast using Direct cast i get exception.
Public class myClass

Function1()
-----------
dim myObject is myClass
myArraylist.Add(myObject)
Function2(myArraylist.Item(0))
......
......
......
Function2(myClass )
 
G

Guest

Try this (I think this is what you meant).

Public class myClass
....
End Class

Function1()
Dim myObject as myClass
myArraylist.Add(myObject)
dim myFunctionReturn as boolean
myFunctionReturn = Function2(DirectCast(myArraylist.Item(0), myclass))
......
End Function

Function2(myVariable as myClass ) as boolean

End Function
 
B

budy_ludy

Hi,

sorry if i have not communicated properly earlier,

Exact situation,

C#
I have a Library created in C# and it has a class myClass(only integers
and strings),
i store the objects in ArrayList.

VB
i call the Library in VB and it returns me the ArrayList(items of
myClass objects),
i retrieve the first element and typecast it to myClass, which has the
same definition in
both vb and c# (the class has only integers and string as elements and
no methods)
Same class definition in vb and c#(library)

now at the point when i try to type cast ArrayList element to myClass
in vb, it gives me below exception,
----------------------------
Unable to cast object of type Library.myClass to type myClass

When casting from a number, the value must be a number less than
infinity

Make sure the source type is convertible to the destination type

----------------------------

i tried both DirectCast and also CType but it gives me exception

any other alternative, if it is not possible to communicate class from
c# library to vb ?
 
G

Guest

budy_ludy,

In your VB project, is your myClass object dimensioned as a new instance of
Library.myClass?

Or do you have 2 classes: Library.myClass and another myClass defined in
your VB project?

Kerry Moorman
 
B

budy_ludy

Hi Kerry Moorman ,

i have a scenario like this,

.......
vb
----
ArrayList inarray,outarray
outarray = LibraryFunction(inarray)

vbobject = new myClass
vbobject.Function1( DirectCast(outarray.Item(0), myClass) )


and now i understood that there is one function, Function1 in myClass
of vb,
and the same function i have not defined in c#.
In vb i call the Function1 and pass the items from ArrayList,
and i get exception when i call Function1 ,
should i have a dummy function1 in c# library class ?
 
C

Chris Dunaway

budy_ludy said:
vb
----
ArrayList inarray,outarray
outarray = LibraryFunction(inarray)

vbobject = new myClass
vbobject.Function1( DirectCast(outarray.Item(0), myClass) )


and now i understood that there is one function, Function1 in myClass
of vb,
and the same function i have not defined in c#.
In vb i call the Function1 and pass the items from ArrayList,
and i get exception when i call Function1 ,
should i have a dummy function1 in c# library class ?

You have TWO classes? Once in a .vb file in your vb project and
another defined in a .dll written in C#? Is that correct?

If so, then you can't do that. Even if your class in VB was identical
to the class in C#, they are still considered different types by the
runtime and you cannot cast between them.

Just use the class in the C# library and don't try to duplicate it in
VB and you should be ok.

Chris
 
B

budy_ludy

Chris Dunaway,

which means i cannot cast a c# class to vb class even though they share
same structure,

ok got it,
 
C

Claes Bergefall

Well, umm, yeah, that is correct I guess.

However, I think you're missing the point: *Any* class you create in C# is
perfectly usuable from VB and vice versa. If you defined myClass in your C#
project then use that class from your VB project. Don't create a new class
with the same name. A class is never the same as another class even though
they might have the same name.

/claes
 
C

Chris Dunaway

budy_ludy said:
Chris Dunaway,

which means i cannot cast a c# class to vb class even though they share
same structure,

Exactly. Just use the C# class and add the function that you need to
that or if you need it in VB or cannot alter the C# class, then in your
VB class, just inherit from the C# class then you can add your function
in VB plus you will be able to cast from the C# class to the VB class.
 
B

budy_ludy

ok got the point, i will reuse the vb class in c# and from the c#
library i will return the class object and in vb i will type cast the
returned arraylist item to vb class.

Thank you to all.
 
C

Cor Ligthert [MVP]

budy,

I don't see the problem as long as you have set a reference to your C# class
and that is correct CLS compliant written.

Than the code should be the same as in my first message.

The only thing is that in that I have used Whatever, because I don't know
what properties/methods you have in that C# class.

I hope this helps,

Cor
 
J

Jay B. Harlow [MVP - Outlook]

| Same class definition in vb and c#(library)

Ah! There's the rub

The "class definition" should only be in one language/library either VB or
C#.

Although they may have similar layouts, they are distinct as the CLR uses
assembly, version, namespace & type name to identify a type. Seeing as the
VB class is in a VB assembly & the C# class is in a C# assembly the runtime
will consider them distinct types.

One of the major benefits of .NET is its interoperability in that you can
define the class once in C#, then use that class definition all your want in
VB and visa versa. Provided the class is CLS compliant....

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi,
|
| sorry if i have not communicated properly earlier,
|
| Exact situation,
|
| C#
| I have a Library created in C# and it has a class myClass(only integers
| and strings),
| i store the objects in ArrayList.
|
| VB
| i call the Library in VB and it returns me the ArrayList(items of
| myClass objects),
| i retrieve the first element and typecast it to myClass, which has the
| same definition in
| both vb and c# (the class has only integers and string as elements and
| no methods)
| Same class definition in vb and c#(library)
|
| now at the point when i try to type cast ArrayList element to myClass
| in vb, it gives me below exception,
| ----------------------------
| Unable to cast object of type Library.myClass to type myClass
|
| When casting from a number, the value must be a number less than
| infinity
|
| Make sure the source type is convertible to the destination type
|
| ----------------------------
|
| i tried both DirectCast and also CType but it gives me exception
|
| any other alternative, if it is not possible to communicate class from
| c# library to vb ?
|
|
|
|
|
| Dennis wrote:
| > Try this (I think this is what you meant).
| >
| > Public class myClass
| > ....
| > End Class
| >
| > Function1()
| > Dim myObject as myClass
| > myArraylist.Add(myObject)
| > dim myFunctionReturn as boolean
| > myFunctionReturn = Function2(DirectCast(myArraylist.Item(0),
myclass))
| > ......
| > End Function
| >
| > Function2(myVariable as myClass ) as boolean
| >
| > End Function
| > > -------------------
| >
| > --
| > Dennis in Houston
| >
| >
| > "budy_ludy" wrote:
| >
| > > Hi Cor
| > >
| > > Below is the situation,
| > > I am trying to pass the arraylist from function1 to the function2
| > > which accepts class,
| > > even if i type cast using Direct cast i get exception.
| > >
| > >
| > > Public class myClass
| > >
| > > Function1()
| > > -----------
| > > dim myObject is myClass
| > > myArraylist.Add(myObject)
| > >
| > > Function2(myArraylist.Item(0))
| > > .......
| > > .......
| > > .......
| > >
| > >
| > > Function2(myClass )
| > > -------------------
| > >
| > > I get exception when i call Function2 inside Function1 even after
| > > DirectCast
| > >
| > >
| > >
| > >
| > > Cor Ligthert [MVP] wrote:
| > > > buddy,
| > > >
| > > > You mean something as
| > > > \\\
| > > > dim myObject is myClass
| > > > myArraylist.Add(myObject)
| > > > DirectCast(myArrayList(0),myClass).WhatEver
| > > > ///
| > > > I hope this helps,
| > > >
| > > > Cor
| > > >
| > > >
| > > > "budy_ludy" <[email protected]> schreef in bericht
| > > > | > > > >
| > > > > Hi All,
| > > > >
| > > > > I am new to vb .net,
| > > > >
| > > > > I have an ArrayList and i store class objects in it,
| > > > > and later i want to retrieve each ArrayList items and type cast to
the
| > > > > class,
| > > > > How can it be done ?
| > > > > I used CType for casting but it is throwing exception.
| > > > >
| > > > >
| > > > > regards
| > > > > ludy
| > > > >
| > >
| > >
|
 

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

Top