| 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
| > > > >
| > >
| > >
|