return a class from a function

  • Thread starter Thread starter dwm
  • Start date Start date
D

dwm

Is it possible to return a class from a function?

I would like to call a function and based on the result of the function
return the class that I want to use.

Thanks for any help!

Something like the following:

private class aClass1
private sub testSub()
debug.print "This is aClass1"
end sub
end class

private class aClass2
private sub testSub()
debug.print "This is aClass2"
end sub
end class

private class testClass()

private sub FormLoad
dim x as object?

x = myFunc() ' Is this possible?

x.testSub() ' Do I have to do some type of cast here?


end sub


private function myFunc() as someClass

if aGlobal = 1 then
return aClass1
else
return aClass2
end function


end class
 
dwm said:
Is it possible to return a class from a function?

I would like to call a function and based on the result of the function
return the class that I want to use.

Thanks for any help!

Something like the following:

private class aClass1
private sub testSub()
debug.print "This is aClass1"
end sub
end class

private class aClass2
private sub testSub()
debug.print "This is aClass2"
end sub
end class

private class testClass()

private sub FormLoad
dim x as object?

x = myFunc() ' Is this possible?

x.testSub() ' Do I have to do some type of cast here?


end sub


private function myFunc() as someClass

if aGlobal = 1 then
return aClass1
else
return aClass2
end function


end class

First of all you never return a class. A class is simply the definition of
the properties and methods provided by the class. You return an instance of
the class.

Next read about interfaces (or inheritance). Since you have two classes
both with a method named testSub you can either create an interface which
has testSub as a member and then have each class implement the interface.
Your myFunc function would then return the interface.

LS
 
dwm said:
Is it possible to return a class from a function?

No, but an object.

I modified your code by deriving both classes from a common base class:

Public Class Form1

MustInherit Class someClass
MustOverride Sub testSub()
End Class

Private Class aClass1
Inherits someClass

Public Overrides Sub testSub()
Debug.Print("This is aClass1")
End Sub
End Class

Private Class aClass2
Inherits someClass

Public Overrides Sub testSub()
Debug.Print("This is aClass2")
End Sub
End Class

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

Dim x As someClass

x = myFunc()

x.testSub()

End Sub
Private Function myFunc() As someClass

If true Then
Return New aClass1
Else
Return New aClass2
End If
End Function

End Class



Armin
 
Not let me nitpick you.

aGlobal

Get rid of that global variable. I'm not talking about just the exercise,
I'm talking altogether.
Learn how to write a static (shared in vb.net) class and variable.



Pass a key into the factory. Global variables are very vb6'ish. Stop using
them.

...........

As previously stated by another person, you don't return "a class", you
return an INSTANCE of a class.



Private Function GetConcrete(myIntKey as int32) as IMyInterface

if myIntKey = 1 then
return new MyConcrete1()
end if

return new MyDefaultConcrete()

End Function



Note the inclusion of the word "new". You're returning a new instance.
 
Thanks all for the help! Much appreciated. btw sloan, it was simple example
code. I don't use globals, but I didn't want to take the time to define the
stuff in the code, so I made up a global.
 
The factory pattern is one of the most simplest of the Design
Patterns.....yet a very powerful one when you consider how it can
"un-spaghetti-up" your code.

You can check out www.dofactory.com (among alot of other places) for their
free design patterns.

Observer
Factory
Command
Singleton

I would check those out first,...they're the "easier ones to
grasp"...............but there are alot of patterns and it will help you
mature as a developer.

I wish someone had told me about them 10 years ago, instead of 4 years ago.
:<
 
Back
Top