Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
297 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 800 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 1200 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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline
Suggested Courses

773 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