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

Posted on 2006-07-12
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

Does setting the object cast it's type?

Is there a performance issue here?
Question by:jackjohnson44
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
  • 2
  • 2

Assisted Solution

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, 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.

Author Comment

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
LVL 14

Accepted Solution

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!


Assisted Solution

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.

Author Comment

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


Featured Post

Technology Partners: 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

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 (…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 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