Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-02-06
13
Medium Priority
?
169 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 64

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

604 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