|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to Type cast ArrayList items to class objectsHi 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 buddy,
You mean something as \\\ dim myObject is myClass myArraylist.Add(myObject) DirectCast(myArrayList(0),myClass).WhatEver /// I hope this helps, Cor Show quoteHide quote "budy_ludy" <sent2a***@gmail.com> schreef in bericht news:1157041346.317762.139540@i42g2000cwa.googlegroups.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 > 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: Show quoteHide quote > buddy, > > You mean something as > \\\ > dim myObject is myClass > myArraylist.Add(myObject) > DirectCast(myArrayList(0),myClass).WhatEver > /// > I hope this helps, > > Cor > > > "budy_ludy" <sent2a***@gmail.com> schreef in bericht > news:1157041346.317762.139540@i42g2000cwa.googlegroups.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 > > Hello budy_ludy,
What Exception exactly? -Boo Show quoteHide quote > 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" <sent2a***@gmail.com> schreef in bericht >> news:1157041346.317762.139540@i42g2000cwa.googlegroups.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 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 > ------------------- -- Show quoteHide quoteDennis 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" <sent2a***@gmail.com> schreef in bericht > > news:1157041346.317762.139540@i42g2000cwa.googlegroups.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 > > > > > 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: Show quoteHide quote > 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" <sent2a***@gmail.com> schreef in bericht > > > news:1157041346.317762.139540@i42g2000cwa.googlegroups.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 > > > > > > > > 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 Show quoteHide quote "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" <sent2a***@gmail.com> schreef in bericht > > > > news:1157041346.317762.139540@i42g2000cwa.googlegroups.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 > > > > > > > > > > > > > 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: Show quoteHide quote > 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" <sent2a***@gmail.com> schreef in bericht > > > > > news:1157041346.317762.139540@i42g2000cwa.googlegroups.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 > > > > > > > > > > > > > > > > > > budy_ludy wrote:
Show quoteHide quote > vb You have TWO classes? Once in a .vb file in your vb project and> ---- > 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 ? > 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 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: Show quoteHide quote > 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 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 Show quoteHide quote "budy_ludy" <sent2a***@gmail.com> wrote in message news:1157117750.053169.126500@e3g2000cwe.googlegroups.com... > > Chris Dunaway, > > which means i cannot cast a c# class to vb class even though they share > same structure, > > ok got it, > > > budy_ludy wrote:
> Chris Dunaway, Exactly. Just use the C# class and add the function that you need to> > which means i cannot cast a c# class to vb class even though they share > same structure, 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. 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: Show quoteHide quote > 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. 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 Show quoteHide quote "budy_ludy" <sent2a***@gmail.com> schreef in bericht news:1157101426.522310.17740@e3g2000cwe.googlegroups.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" <sent2a***@gmail.com> schreef in bericht >> > > news:1157041346.317762.139540@i42g2000cwa.googlegroups.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 >> > > > >> > >> > > | Same class definition in vb and c#(library) Ah! There's the rubThe "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.... -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "budy_ludy" <sent2a***@gmail.com> wrote in message news:1157101426.522310.17740@e3g2000cwe.googlegroups.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" <sent2a***@gmail.com> schreef in bericht | > > > news:1157041346.317762.139540@i42g2000cwa.googlegroups.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 | > > > > | > > | > > |
Best practices for creating a "wizard"
question about some good practises Question about 'Using' and resources formula for calculating sales tax (GST) Return DataRow VB 2005 with Active Directory VB .NET use MailMessage User Controls basic question using com-port in VB.net use varible value in include file |
|||||||||||||||||||||||