Solved

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

Posted on 2014-02-06
13
158 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
Comment Utility
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
Comment Utility
getStuff(Catalog)

Open in new window


gives

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

:(
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
You need to pass a Type.

Try the following

Dim obj As New Catalog
getStuff(GetType(obj))
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Dodsworth
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
@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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
Comment Utility
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
Comment Utility
Hope that you are all ok with the points allocation.  Lots of help and a long journey :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now