473,545 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic Types Question

I have this structure:

Class ElemBase

Class Elem1 : Inherits ElemBase

Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits
System.Componen tModel.BindingL ist(Of GenElem)

Class Colec1 : Inherits ColecBase(Of Elem1)

Class Class1

Public E As ElemBase

Public C As ColecBase(Of ElemBase)

End Class

Class Class2

Dim ColH As Colec1

Dim Cl1 As Class1

Sub proc()

Cl1.C = ColH

End Sub

End Class

I get an error in sub proc because Colect1 can't be converted to
ColecBase(of ElemBase)

there is a way to do this?
Jan 16 '07 #1
2 1477
Angel Mateos wrote:
I have this structure:

Class ElemBase

Class Elem1 : Inherits ElemBase

Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits
System.Componen tModel.BindingL ist(Of GenElem)

Class Colec1 : Inherits ColecBase(Of Elem1)

Class Class1

Public E As ElemBase

Public C As ColecBase(Of ElemBase)

End Class

Class Class2

Dim ColH As Colec1

Dim Cl1 As Class1

Sub proc()

Cl1.C = ColH

End Sub

End Class

I get an error in sub proc because Colect1 can't be converted to
ColecBase(of ElemBase)

there is a way to do this?
The important thing to notice is that generic instanciations don't keep
the inheritance chain of the type arguments (in other words, a generic
collection(of integer) doesn't inherit from a generic collection(of
object), even though ints do inherit from objects).

You are getting the error because you're assigning an incompatible
type: Class Colec1 is a collection of Elem1, not a collection of
ElemBase, as Class1.C expects. Even though Elem1 inherits from
ElemBase, the Colec1 class can only deal with the specific type Elem1,
while Class1.C requires a collection that accepts any ElemBase
inheritance, a contract that a Colec1 can't honor.

Suppose the compiler "didn't see" the error of the construction and let
you assign ColH to Col1.C, as you intended.

This means that you'd be able to compile the following erroneous code:

Dim Bad As New ElemBase '<-- this is no Elem1
Cl1.C.Add(Bad) '<-- Oops!

Maybe a way to achieve what you want is to provide a common interface
to all ColecBase instanciations, say, ICollection(of ElemBase), where
you can explicitly cast the provided ElemBase to the instanciated
GenElem type:

<aircode>
Class ColecBase(Of GenElem As {ElemBase, New})
Inherits System.Componen tModel.BindingL ist(Of GenElem)
Implements ICollection(Of ElemBase)

Private Sub ICollection_Add (ByVal item As ElemBase) _
Implements System.Collecti ons.Generic.ICo llection(Of ElemBase).Add
'This may throw an invalid cast at run time!
Me.Add(CType(it em, GenElem))
End Sub

'...
End Class

Class Class1
Public E As ElemBase
Public C As ICollection(Of ElemBase)
End Class

</aircode>

Notice, however, that such castings is the problem generics came to
solve. Maybe a revision of your requirements is in order... =P

HTH.

Regards,

Branco.

Jan 16 '07 #2
Thanks you for your explanation, has been very instructive for me.

"Branco Medeiros" <br************ *@gmail.comescr ibió en el mensaje
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Angel Mateos wrote:
>I have this structure:

Class ElemBase

Class Elem1 : Inherits ElemBase

Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits
System.Compone ntModel.Binding List(Of GenElem)

Class Colec1 : Inherits ColecBase(Of Elem1)

Class Class1

Public E As ElemBase

Public C As ColecBase(Of ElemBase)

End Class

Class Class2

Dim ColH As Colec1

Dim Cl1 As Class1

Sub proc()

Cl1.C = ColH

End Sub

End Class

I get an error in sub proc because Colect1 can't be converted to
ColecBase(of ElemBase)

there is a way to do this?

The important thing to notice is that generic instanciations don't keep
the inheritance chain of the type arguments (in other words, a generic
collection(of integer) doesn't inherit from a generic collection(of
object), even though ints do inherit from objects).

You are getting the error because you're assigning an incompatible
type: Class Colec1 is a collection of Elem1, not a collection of
ElemBase, as Class1.C expects. Even though Elem1 inherits from
ElemBase, the Colec1 class can only deal with the specific type Elem1,
while Class1.C requires a collection that accepts any ElemBase
inheritance, a contract that a Colec1 can't honor.

Suppose the compiler "didn't see" the error of the construction and let
you assign ColH to Col1.C, as you intended.

This means that you'd be able to compile the following erroneous code:

Dim Bad As New ElemBase '<-- this is no Elem1
Cl1.C.Add(Bad) '<-- Oops!

Maybe a way to achieve what you want is to provide a common interface
to all ColecBase instanciations, say, ICollection(of ElemBase), where
you can explicitly cast the provided ElemBase to the instanciated
GenElem type:

<aircode>
Class ColecBase(Of GenElem As {ElemBase, New})
Inherits System.Componen tModel.BindingL ist(Of GenElem)
Implements ICollection(Of ElemBase)

Private Sub ICollection_Add (ByVal item As ElemBase) _
Implements System.Collecti ons.Generic.ICo llection(Of ElemBase).Add
'This may throw an invalid cast at run time!
Me.Add(CType(it em, GenElem))
End Sub

'...
End Class

Class Class1
Public E As ElemBase
Public C As ICollection(Of ElemBase)
End Class

</aircode>

Notice, however, that such castings is the problem generics came to
solve. Maybe a revision of your requirements is in order... =P

HTH.

Regards,

Branco.

Jan 17 '07 #3

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

Similar topics

4
4413
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their reuse for various template parameters. I wonder if there exist techniques for achieving a dynamic polymorphism using the generic programming. Is this...
1
3420
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to it. Therefore I wanted to creat a class member which represents the pointer to the array. Unfortunately I get the folowring compile error for the...
3
2182
by: Boris | last post by:
I have a class which should like this ideally: generic <typename T> public ref class ManagedClass { T ^managedMember; UnmanagedClass<U*unmanagedMember; }; I actually would like to specify the type U depending on the type T. As
7
2034
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation: displayMsgDelegate = DisplayStatusMessage; implementation: public void DisplayStatusMessage(string statusMessage)
11
1879
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
0
7410
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...
0
7923
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...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4960
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...
0
3466
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...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
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.