?
Solved

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

Posted on 2014-02-06
13
Medium Priority
?
167 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
[X]
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
  • 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1000 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 400 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 600 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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

777 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