473,724 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Type cast ArrayList items to class objects


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
14 18748
buddy,

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

Cor
"budy_ludy" <se*******@gmai l.comschreef in bericht
news:11******** **************@ i42g2000cwa.goo glegroups.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
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(myArr aylist.Item(0))
.......
.......
.......
Function2(myCla ss )
-------------------

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(myAr rayList(0),myCl ass).WhatEver
///
I hope this helps,

Cor
"budy_ludy" <se*******@gmai l.comschreef in bericht
news:11******** **************@ i42g2000cwa.goo glegroups.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
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(myArr aylist.Item(0))
......
......
......
Function2(myCla ss )
-------------------
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.Ad d(myObject)
DirectCast(myA rrayList(0),myC lass).WhatEver
///
I hope this helps,
Cor

"budy_ludy" <se*******@gmai l.comschreef in bericht
news:11******* *************** @i42g2000cwa.go oglegroups.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
Try this (I think this is what you meant).

Public class myClass
....
End Class

Function1()
Dim myObject as myClass
myArraylist.Add (myObject)
dim myFunctionRetur n as boolean
myFunctionRetur n = Function2(Direc tCast(myArrayli st.Item(0), myclass))
......
End Function

Function2(myVar iable 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(myArr aylist.Item(0))
.......
.......
.......
Function2(myCla ss )
-------------------

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(myAr rayList(0),myCl ass).WhatEver
///
I hope this helps,

Cor
"budy_ludy" <se*******@gmai l.comschreef in bericht
news:11******** **************@ i42g2000cwa.goo glegroups.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
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 myFunctionRetur n as boolean
myFunctionRetur n = Function2(Direc tCast(myArrayli st.Item(0), myclass))
......
End Function

Function2(myVar iable 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(myArr aylist.Item(0))
.......
.......
.......
Function2(myCla ss )
-------------------

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(myAr rayList(0),myCl ass).WhatEver
///
I hope this helps,
>
Cor
>
>
"budy_ludy" <se*******@gmai l.comschreef in bericht
news:11******** **************@ i42g2000cwa.goo glegroups.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
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 myFunctionRetur n as boolean
myFunctionRetur n = Function2(Direc tCast(myArrayli st.Item(0), myclass))
......
End Function

Function2(myVar iable 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(myArr aylist.Item(0))
.......
.......
.......
>
>
Function2(myCla ss )
-------------------
>
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(myAr rayList(0),myCl ass).WhatEver
///
I hope this helps,

Cor


"budy_ludy" <se*******@gmai l.comschreef in bericht
news:11******** **************@ i42g2000cwa.goo glegroups.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
Hi Kerry Moorman ,

i have a scenario like this,

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

vbobject = new myClass
vbobject.Functi on1( DirectCast(outa rray.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 myFunctionRetur n as boolean
myFunctionRetur n = Function2(Direc tCast(myArrayli st.Item(0), myclass))
......
End Function
>
Function2(myVar iable 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(myArr aylist.Item(0))
.......
.......
.......


Function2(myCla ss )
-------------------

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(myAr rayList(0),myCl ass).WhatEver
///
I hope this helps,
>
Cor
>
>
"budy_ludy" <se*******@gmai l.comschreef in bericht
news:11******** **************@ i42g2000cwa.goo glegroups.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
budy_ludy wrote:
vb
----
ArrayList inarray,outarra y
outarray = LibraryFunction (inarray)

vbobject = new myClass
vbobject.Functi on1( DirectCast(outa rray.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

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,outarra y
outarray = LibraryFunction (inarray)

vbobject = new myClass
vbobject.Functi on1( DirectCast(outa rray.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2126
by: Pure Krome | last post by:
How do i serialize two instances (of some object) in the same class? I'm not sure how i would de-serialize a class that has two or more instances of some object type, and how the deserialize method would KNOW which serialized instance to read. eg.
4
547
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question"; (string)xyz; now we only have a type object of System.String type
1
2129
by: martin | last post by:
hi guys, have you ever tried to create a type-cast operator to a 'reference/pointer to an array'? i hadn't, not until yesterday when i discovered that it is not exactly the trivial task one'd assume it to be - none of my apparently naive attempts were accepted for a valid type-cast operator by any of the compilers i threw them at (i.e. gcc 2.95.3, gcc 3.3.3, vc 2003). so assume we have:
3
21220
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object obj=Activator.CreateInstance(strAssemblyName, "MyClass"); //Now, I want to do something like ((MyClass)obj).Method //Can I do this?
6
11299
by: SA | last post by:
Hi all: I have an object of a base class that needs to be cast to an object of a specialized class. What is the best way to do this? (I thought about creating a constructor in the specialized class that takes an argument of the type of the base class and then copy property values over, but that seems like a hassle) --
2
2632
by: =?Utf-8?B?RHJldw==?= | last post by:
I have a current requirement to all a subsidiary to use a current legacy application and interface with my new system to inject data when appropriate. I wanted to create a web service since we are both .Net shops and this would seem to be the easiest method. But I ran into one snafu. I wanted to return to the subsidiary an ArrayList of my Organization objects which contain the orgs id, name etc. so they can use that information in the data...
2
6535
by: Bigi | last post by:
Hi, Please help, this has been driving me nuts for nearly 2 days now. This vb6 code works: Public oEng As New ebizEngine Public oMsg As ebizMessage Function EbizGetFromQueue() As String
7
3950
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
55
3978
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable built-in-like behavior for objects of class type. That leads to the "necessity" for the exception machinery so that errors from constructors can be handled. Is all that complexity worth it just to get built-in-like behavior from class objects? Maybe a...
0
8868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9389
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9243
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8063
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4771
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2613
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.