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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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