Solved

vb.net return a class from a function, but don't know what kind of class gets returned

Posted on 2006-07-12
5
257 Views
Last Modified: 2010-04-23
I have a vb6 app that calls a function which returns a class

                Dim stepToBeProgrammed As Object
                Set stepToBeProgrammed = getClassOrFormFromStep(stepNameLong, "class")

The function getClassOrFormFromStep, is set up to return an object, in this case a class.

Is this how I should do it in vb.net?

Does setting the object cast it's type?

Is there a performance issue here?
0
Comment
Question by:jackjohnson44
  • 2
  • 2
5 Comments
 
LVL 8

Assisted Solution

by:Shakti109
Shakti109 earned 200 total points
ID: 17090189


When the object returns, it gets cast as whatever class it gets passed (late bound).

You could use this in vb.net, but if you used the explicit and strict options in the compiler you would get errors for having a late bound object and yes this becomes a performance issue. It is not a world-ending type of performance hit, but it is less than optimal.

When you say you dont know what class is being returned, is this due to the possibility of multiple types being passed back to your call?

If so you can define the object that gets the return item all at one time.

As an example :

   dim ClassTest = returnclass(1)

   Public Function returnclass(ByVal mu As Integer) As MyNewClass
      Return New MyNewClass
   End Function

 Public class MyNewClass
   'functions, logic, etc here
 end class


If you were to use gettype on ClassTest  (something like : console.writeline(classtest.gettype), you would see that it "became" an instance of MyNewClass.
0
 

Author Comment

by:jackjohnson44
ID: 17090228
Thanks, that will definitely work for dim'ing the variable.

I don't know what type of class will be returned, is it ok to have my function return type object?
Or is there a better way to do it?

   Public Function returnclass(ByVal mu As Integer) As object
      Return New MyNewClass
   End Function
0
 
LVL 14

Accepted Solution

by:
PockyMaster earned 300 total points
ID: 17090415
If you don't know which kind of class you will return, do you also not know which kind of base class your returning objects will share? If yes, return a type of that base class. If you want to use the returned object in other functionality than implemented in the base class, you will have to cast it. So someway or another, you are gonna have to know which type you are returning/requesting.

Like shakti said, you can use the gettype to determine the returned object type. combine that with casting when needed e.g.

But in my opinion, if you want to get an object, you are getting it because you need it for some reason. So probably in one way or another you will know what kind of object you will get!

0
 
LVL 8

Assisted Solution

by:Shakti109
Shakti109 earned 200 total points
ID: 17090463

Sure, you can return as "object", when it actually does return it will "become" whatever it was passed.

Your function :
   Public Function returnclass(ByVal mu As Integer) As object
      Return New MyNewClass
   End Function

Will work for returning an unknown class.
0
 

Author Comment

by:jackjohnson44
ID: 17090585
Instead of object, I set the variable to the base class.

Thanks
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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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