Solved

Is it possible to pass a class to a subroutine as an arguement

Posted on 2014-02-06
13
163 Views
Last Modified: 2014-02-09
Does the title make sense ?

I currently have..

Private Sub getStuff()
        For Each prop In GetType(Catalog).GetProperties()
            System.Diagnostics.Debug.WriteLine(prop.Name & " " & prop.PropertyType.ToString)
        Next

Open in new window


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

Open in new window


Any ideas?
0
Comment
Question by:Dodsworth
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 39839396
Yes its possible..



Private Sub getStuff(mClass As Type)
        For Each prop In GetType(mClass).GetProperties()
            System.Diagnostics.Debug.WriteLine(prop.Name & " " & prop.PropertyType.ToString)
        Next
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39839629
getStuff(Catalog)

Open in new window


gives

'Catalog is a Type and cannot be used in an expression"

:(
0
 
LVL 40
ID: 39839649
You need to pass a Type.

Try the following

Dim obj As New Catalog
getStuff(GetType(obj))
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 39839877
Hi Dodsworth;

The following should give you what you need.

Public Sub GetStuff(Of T As Class)(ByVal tClass As T)
    For Each prop In GetType(T).GetProperties()
        System.Diagnostics.Debug.WriteLine(prop.Name & " " & prop.PropertyType.ToString)
    Next
End Sub

Open in new window

To call the above you can do the following.
' Get an instance of the class in this case Employee entity
Dim emp As Employee

' Then call GetStuff as follows
GetStuff(emp)

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39841027
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

Open in new window

0
 
LVL 19

Assisted Solution

by:Melih SARICA
Melih SARICA earned 100 total points
ID: 39841147
U need to pass type of catalog. not catalog itself..


private sub callingsub()
   getStuff(gettype(catalog))
end

Private Sub getStuff(mClass As Type)
        For Each prop In mclass.GetProperties()
            System.Diagnostics.Debug.WriteLine(prop.Name & " " & prop.PropertyType.ToString)
        Next
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39841723
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39841737
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)
...

Open in new window

0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39844124
@nonzero  I didn't refresh my page before I posted to see your response.  This works also.  Who to give the points to ?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39844139
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   = )
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39844153
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 !
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 39844219
The "Of XXX" part defines the method to be generic. It's not C#-ish; it's .NET-ish  ; )

All generic methods are are templates that you provide to the compiler that it can use to create a series of methods for you. Normally in .NET you have to specify the parameters and their types for each method you define. But say you have logic that is exactly the same, but only it works on different parameter types. Say:

Public Sub Display(ByVal value As Integer)
    Console.WriteLine(value)
End Function

Public Sub Display(ByVal value As String)
    Console.WriteLine(value)
End Function

Open in new window


The logic is exactly the same--write the value to the console--but the parameter coming in differs by type. Pretend you had to do that same bit for every data type in .NET...That's quite a few functions. Now, what if a requirement comes in that instead of writing to the console, you have to write to a file? How many places do you have to make the change?

This is where generic methods can be beneficial. If we genericize the above function:

Public Sub Display(Of T)(ByVal value As T)
    Console.WriteLine(value)
End Function

Open in new window


...we only have one function to write or modify. The compiler will take care of the tedious work of creating a function for each type. Now with our generic function we simply call it with whatever parameter we like:

Display("hello world!")
Display(1)
Display(False)

Open in new window


...and any of the above executes without error or warning.

In addition to generic functions, you can also have generic types. There are numerous generic types in .NET. You've probably used them without fully realizing it. List(Of T), Dictionary(Of T, S), and Stack(Of T) are all generic types. The link I referenced above discusses generic types as well.

Generics have been around since .NET 2.0, and they are definitely worth learning.
0
 
LVL 1

Author Closing Comment

by:Dodsworth
ID: 39845688
Hope that you are all ok with the points allocation.  Lots of help and a long journey :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question