Dodsworth
asked on
Is it possible to pass a class to a subroutine as an arguement
Does the title make sense ?
I currently have..
Where Catalog is a LinqtoSQL entity class
I need something like (pseudo*)
Any ideas?
I currently have..
Private Sub getStuff()
For Each prop In GetType(Catalog).GetProperties()
System.Diagnostics.Debug.WriteLine(prop.Name & " " & prop.PropertyType.ToString)
Next
Where Catalog is a LinqtoSQL entity class
I need something like (pseudo*)
Private Sub getStuff(mClass As Class)
For Each prop In GetType(mClass).GetProperties()
System.Diagnostics.Debug.WriteLine(prop.Name & " " & prop.PropertyType.ToString)
Next
Any ideas?
ASKER
getStuff(Catalog)
gives
'Catalog is a Type and cannot be used in an expression"
:(
You need to pass a Type.
Try the following
Dim obj As New Catalog
getStuff(GetType(obj))
Try the following
Dim obj As New Catalog
getStuff(GetType(obj))
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The GetType operator expects a type name to be passed to it, not a variable name. To get the type from a variable you have to call the GetType method.
Private Sub getStuff(mClass As Class)
For Each prop In mClass.GetType().GetProperties()
System.Diagnostics.Debug.WriteLine(prop.Name & " " & prop.PropertyType.ToString)
Next
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
confused!
@James: Type Obj is not defined.
@Fernando: Yes this works, but I'm confused by the (Of T As Class)(ByVal tClass As T). Whats going on here?
@Kaufmed: It doesn't like the 'as Class' bit. Keyword does not name a type.
@James: Type Obj is not defined.
@Fernando: Yes this works, but I'm confused by the (Of T As Class)(ByVal tClass As T). Whats going on here?
@Kaufmed: It doesn't like the 'as Class' bit. Keyword does not name a type.
Ah, I think I understand what you are after now. Fernando Soto's post above (http:#a39839877) is what you are after; just remove the "As Class" from his snippet.
Public Sub GetStuff(Of T)(ByVal tClass As T)
...
ASKER
@nonzero I didn't refresh my page before I posted to see your response. This works also. Who to give the points to ?
While nonzero's approach does work, personally I would recommend against it. The reason is that now wherever you want to use this function the calling code is now responsible for calling the Gettype operator, whereas with a generic function (like Fernando Soto's) you only have to call GetType in one place: within the generic function. I'm lazy, so less typing is always a win for me = )
ASKER
Agreed tho I wish he would explain (Of T As Class)(ByVal tClass As T). I never seen anything like that in VB. It looks rather C#ish !
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hope that you are all ok with the points allocation. Lots of help and a long journey :)
Private Sub getStuff(mClass As Type)
For Each prop In GetType(mClass).GetPropert
System.Diagnostics.Debug.W
Next