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
254 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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 …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now