Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

objects from vb class taking arguments 2

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
Hi,

Please assist!

I would like to know if we can pass parameters ro objects when we create an object from a vb class. I know we can do this in C++ where we can have several constructors like this:

Customer is a class.
object1, object2 are of type Customer.

object1 = new Customer()
object2 = new Customer(a,b,c,)


Thanks in advance.

yputopia




 
dear yputopia,


No you can't.

And I really miss it :-(

but you could define an init-method to call right after
instantiating.


regards Astrid





 
Hi Astrid,

Is this init-method declared in the module?
If it is in the class itself then we still have a problem of getting the parameters into the object.
If it is declared outside then we can not encapsulate in the class object.

regards,
yputopia
 
It cannot be done directly, as VB doesn't support constructors with parameters.

On the other hand, you could write a factory class that constructs you're desired object...

VB.NET will address this issue.


 
Dear yputopia,

the init method must be a PUBLIC Method of your class.
dim object1 as Customer.

set object1 = new Customer()
object1.init( args ,....)


You could create some kind of 'overloading' by using
OPTIONAL Arguments, which would be default in c++ I asume.


public sub init( arg1 as String, Optional arg2 as String = "Hugo")


' do something

end sub

Strongm's idea sounds good, I never thought about that way of doing it.
But it seems to me , that one would have a simular problem then, as the factory class needs to have a public method to pass the arguments too.

Yours Astrid
 
Well yes, it would need a public method - but it would be the same public method used to construct the target object. Here's a simple example to illustrate.

Create a new project with a form and add a command button to it. Add two empty classes, one called adder and one called factory.

Drop the following code into the factory class:
[tt]
' Simple factory class
Option Explicit

Public Function GetAdder(A As Long, B As Long, Optional C As Long) As Adder
Dim FactoryObject As Adder

Set GetAdder = New Adder
GetAdder.A = A
GetAdder.B = B
GetAdder.C = C
End Function
[/tt]
Drop the following into the adder class:
[tt]
' Example Adder class
Option Explicit

Private mvarA As Long
Private mvarB As Long
Private mvarC As Long

Public Function AddThem() As Long
AddThem = mvarA + mvarB + mvarC
End Function
Public Property Let C(ByVal vData As Long)
mvarC = vData
End Property

Public Property Get C() As Long
C = mvarC
End Property

Public Property Let B(ByVal vData As Long)
mvarB = vData
End Property

Public Property Get B() As Long
B = mvarB
End Property

Public Property Let A(ByVal vData As Long)
mvarA = vData
End Property

Public Property Get A() As Long
A = mvarA
End Property
[/tt]
Finally drop the following code into the form module:
[tt]
Option Explicit

Private Sub Command1_Click()
Dim myFactory As Factory
Dim myAdder As Adder

' Create a new object normally
Set myAdder = New Adder
MsgBox "Result with normal construction: " & myAdder.AddThem

' Create a new object via factory
Set myFactory = New Factory
Set myAdder = myFactory.GetAdder(3, 5, 2) ' Construct and initialise
MsgBox "Result with factory construction: " & myAdder.AddThem
End Sub
[/tt]
 
Dear Strongm,


You see me impressed


regards Astrid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top