By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,435 Members | 2,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,435 IT Pros & Developers. It's quick & easy.

How to Type cast ArrayList items to class objects

P: n/a

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

Aug 31 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
buddy,

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

Cor
"budy_ludy" <se*******@gmail.comschreef in bericht
news:11**********************@i42g2000cwa.googlegr oups.com...
>
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

Aug 31 '06 #2

P: n/a
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" <se*******@gmail.comschreef in bericht
news:11**********************@i42g2000cwa.googlegr oups.com...

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
Aug 31 '06 #3

P: n/a
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 )
-------------------
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" <se*******@gmail.comschreef in bericht
news:11**********************@i42g2000cwa.googleg roups.com...
>>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

Aug 31 '06 #4

P: n/a
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" <se*******@gmail.comschreef in bericht
news:11**********************@i42g2000cwa.googlegr oups.com...
>
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
>

Aug 31 '06 #5

P: n/a
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" <se*******@gmail.comschreef in bericht
news:11**********************@i42g2000cwa.googlegr oups.com...

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
Sep 1 '06 #6

P: n/a
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
"budy_ludy" wrote:
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" <se*******@gmail.comschreef in bericht
news:11**********************@i42g2000cwa.googlegr oups.com...
>
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
>
>
>

Sep 1 '06 #7

P: n/a
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 ?

Kerry Moorman wrote:
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
"budy_ludy" wrote:
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" <se*******@gmail.comschreef in bericht
news:11**********************@i42g2000cwa.googlegr oups.com...

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

Sep 1 '06 #8

P: n/a
budy_ludy wrote:
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

Sep 1 '06 #9

P: n/a

Chris Dunaway,

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

ok got it,

Chris Dunaway wrote:
budy_ludy wrote:
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
Sep 1 '06 #10

P: n/a
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

"budy_ludy" <se*******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>
Chris Dunaway,

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

ok got it,

Sep 1 '06 #11

P: n/a
budy_ludy wrote:
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.

Sep 1 '06 #12

P: n/a

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.


Chris Dunaway wrote:
budy_ludy wrote:
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.
Sep 1 '06 #13

P: n/a
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

"budy_ludy" <se*******@gmail.comschreef in bericht
news:11*********************@e3g2000cwe.googlegrou ps.com...
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" <se*******@gmail.comschreef in bericht
news:11**********************@i42g2000cwa.googlegr oups.com...

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



Sep 1 '06 #14

P: n/a
| 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
"budy_ludy" <se*******@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
| 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" <se*******@gmail.comschreef in bericht
| news:11**********************@i42g2000cwa.googlegr oups.com...
|
| 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
|
|
|
|
Sep 1 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.