|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to use a Generic's base type?Now, on another class, i want to add a method which can take in an object of my generic class... but the catch is, i want it to be able to take in an instance of the generic REGARDLESS of what the "Of" type of the generic is. E.g. .... given a generic class MyGen(Of T) I want to be able to write another class as such: Class MyOther ..... Public Sub DoIt(myarg As MyGen) ... do something ... End Sub ..... End Class The editor/compiler complains though that i did not specify an "Of" on the argument myarg. But that is specifically what i want to do, is make a method which just takes a MyGen instance, regardless of its "Of" Can i do this? Thanks in advance, - Arthur Dent. Arthur,
| but the catch is, i want it to be able to take in an instance of the Have you tried to define DoIt as a generic Method?generic | REGARDLESS of what the "Of" | type of the generic is. Something like: | Class MyOther Because of "type inference" you can just call DoIt with an instance of the | ..... | Public Sub DoIt(Of T) (myarg As MyGen(Of T)) | ... do something ... | End Sub | ..... | End Class generic, without specifically specifying T, and the compiler will "infer T". Dim a As New MyGen(Of Integer) Dim b As New MyGen(Of String) Dim other As New MyOther other.DoIt(a) other.DoIt(b) If you like you can specify the T parameter: other.DoIt(Of Integer)(a) other.DoIt(Of String)(b) However there is no real benefit to specifying the type, as long as VB can infer the type. If VB cannot infer the type based on the parameters, then you need to give the type. -- Show quoteHide quoteHope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Arthur Dent" <hitchhikersguideto-n***@yahoo.com> wrote in message news:%23DIqQjWIGHA.3944@tk2msftngp13.phx.gbl... | Hi all... Heres what im looking to do.... I have a Generic class i wrote. | Now, on another class, i want to add a method which can take in an object of | my generic class... | but the catch is, i want it to be able to take in an instance of the generic | REGARDLESS of what the "Of" | type of the generic is. | | E.g. .... given a generic class MyGen(Of T) | | I want to be able to write another class as such: | | Class MyOther | ..... | Public Sub DoIt(myarg As MyGen) | ... do something ... | End Sub | ..... | End Class | | The editor/compiler complains though that i did not specify an "Of" on the | argument myarg. | But that is specifically what i want to do, is make a method which just | takes a MyGen instance, | regardless of its "Of" | | Can i do this? | | Thanks in advance, | - Arthur Dent. | |
Show Modal, then Again
TopMost without SetFocus Get associated icon for a file A question about finding class methods in the Help throw/handle exceptions while using backgroundworker DUMB question about IndexOf VB.NET profiler What is the correct case convention for Set? Listbox idiotic example progress bar right to left |
|||||||||||||||||||||||