Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2014-02-06
13
164 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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