Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm new to Vb.net and would like to know if you can make a generic class. I've read the generic class articles here, but they are not exactly what I want. I'm looking for generic properties to go along with the class

Right now I have one property called "Item" (or _item). What I need is two examples of properties for the generic class and at least one method of the class.

And for the last... I'm an old guy, so this is not a school project. It's only for helping me to learn OOP.

So thank you in advance for any help

FOUND MY ANSWER. TRIED TO DELETE BUT CAN'T
THANKS FOR LOOKING

What I have tried:

Asking other people and other forums.
Posted
Updated 20-Apr-16 3:42am
v3
Comments
Karthik_Mahalingam 20-Apr-16 6:19am    
honest answer for what i have tried: :)
Sergey Alexandrovich Kryukov 20-Apr-16 6:20am    
Not clear. What such "generic properties" would mean, exactly, what would be your ultimate goal or reason to have them? You need to describe some usage scenario.

There are generic classes, methods, and they have generic parameters, which are types.
If type T is a generic parameter of a generic class, property of this type is generic, in this sense. In other words, the property will be instantiated when you instantiate complete class from a generic one. I would say, this is the only thing which can make sense, with properties.

So, it that what you want?

—SA
Duncan Edwards Jones 20-Apr-16 6:51am    
If you have found the answer, please post it here so others may also find it when they google your question...

 
Share this answer
 
You can declare a class as being dynamic by adding (Of TBase) after the class declaration (You can use any name instead of TBase - that is just my usual)

for example:-

VB
Public Declare Class Handful(Of TBase)

End Class


Now - if you want to have a property whose type comes from that generic you declare it using TBase as its type e.g.

VB
Public Declare Class Handful(Of TBase)

   Public Property Thumb() As TBase 

End Class


Now you can declare instances of the generic class by specifying the specific type to use and it will enforce the type-safe coding you would expect. e,g,

VB
Dim MyVar As New Handful(Of Integer)

MyVar.Thumb = 1

Dim MyOtherVar As Handful(Of String)

MyOtherVar.Thumb = "First Digit"
 
Share this answer
 
Please see my comment to the question.

Formally speaking, there are no generic properties; and I don't see how they can make any sense. I'm not sure that you are not confusing two different things, such as generic properties and properties of generic-parameter type. The properties of generic-parameter type makes perfect sense:
C#
class MyClass<T> {
   internal T MyProperty { get; set; }
   internal static void DoSomething<T2>(T2 myParameter) { /* ... */ }
   // note: T2 has nothing to do with generic class parameter T
}

//...

MyClass<string> myInstance = new MyClass<string>();
myInstance.MyProperty = "some string";
myInstance.DoSomething<int>(42);

MyClass<int> myOtherInstance = new MyClass<int>();
myOtherInstance.MyProperty = 13;

Property does not have the same semantics as function, to be generic. Why do you think I made an example generic function static? Yes, it could be an instance function, but what would it do? It would make some sense if it did something to the class instance, but the trouble is: the class instance has no "knowledge" of T2. T and T2 are unrelated.

[EDIT]

Why not generic properties? Because there is noting to do with generic parameters. What is property of type T? This is, essentially, a couple of function, getter returning T, and setter accepting T, plus, for instance properties, "this", a reference to the instance. There is no a room to use any other type.

[END EDIT]

If this explanation does not clear the subject for you, you have to explain your idea behind those non-existing "generic properties".

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900