setting member variable in child class

A

Altman

I have a class that I want many others inherited off of. I have created a
variable in the parent class that I want the inherited classes to set. But
I do not want to set the variable in the constructor. Ex.

Class A
protected myVar as string
END Class

Class B
inherits A
'set the myVar Variable
myVar = "Class B"

End Class

I need Class A to have access to the myVar so I can't use shadows and
redeclare it. This seems like it should be trivial but I can't figure it
out. The reason I don't want to use a constructo in class B is because I
built the overloaded contructors in class A and do not want to override each
one of them everytime I inherit class A.
 
L

Larry Lard

Altman said:
I have a class that I want many others inherited off of. I have created a
variable in the parent class that I want the inherited classes to set. But
I do not want to set the variable in the constructor. Ex.

Class A
protected myVar as string
END Class

Class B
inherits A
'set the myVar Variable
myVar = "Class B"

End Class

I need Class A to have access to the myVar so I can't use shadows and
redeclare it. This seems like it should be trivial but I can't figure it
out. The reason I don't want to use a constructo in class B is because I
built the overloaded contructors in class A and do not want to override each
one of them everytime I inherit class A.

You're going to have to do that though. You can't inherit constructor
implementations. You're going to have to have overloaded constructors
for each class dervied from A - these can call MyBase.New(whatever), if
you like. Initialize myVar in them.
 
A

Altman

I was hoping I wouldn't have to do that because I have 4 different
constructors for class A and didn't want to have to in turn make 4 different
constructors for class b just so it can set the myvar and then call
mybase.new().
 
C

Chris

Altman said:
I was hoping I wouldn't have to do that because I have 4 different
constructors for class A and didn't want to have to in turn make 4 different
constructors for class b just so it can set the myvar and then call
mybase.new().


You're going to have to do that though. You can't inherit constructor
implementations. You're going to have to have overloaded constructors
for each class dervied from A - these can call MyBase.New(whatever), if
you like. Initialize myVar in them.

Why doesn't Class A require that the myVar is set in the 4 Class A
constructors?

Chris
 
A

Altman

OK I came up with a work around if anyone cares.
create a mustoverride method in class A ex (fillClassVariables). In each of
my constructors for class A I will call that fillClassVariables. In class
b, in the fillClassVariables method, fill in the variables I need. This
makes it so I do not need to recreate my constructors.

example

Class A
protected myVar as string
protected mustoverride function fillClassVariables
public sub new()
fillClassVariables
'do stuff
end sub
public sub new(var1)
fillClassVariables
'do stuff
end sub
public sub new(var1,var2)
fillClassVariables
'do stuff
end sub
END Class

Class B
inherits A
protected overrides function fillclassvariables
myVar = "Class B"
end function

End Class
 
A

Altman

OK so this is something I didn't realize. The overloaded constructor does
not carry down to the inherited class. Is this right? Is this the same in
C# or J#?
 
A

Altman

Why doesn't Class A require that the myVar is set in the 4 Class A
constructors?

Because I have methods of class A that use the myVar but I they are
different depending on the inherited class. Therefore the inherited class
needs to set the value of these variables.
 
C

Chris

Altman said:
OK I came up with a work around if anyone cares.
create a mustoverride method in class A ex (fillClassVariables). In each of
my constructors for class A I will call that fillClassVariables. In class
b, in the fillClassVariables method, fill in the variables I need. This
makes it so I do not need to recreate my constructors.

example

Class A
protected myVar as string
protected mustoverride function fillClassVariables
public sub new()
fillClassVariables
'do stuff
end sub
public sub new(var1)
fillClassVariables
'do stuff
end sub
public sub new(var1,var2)
fillClassVariables
'do stuff
end sub
END Class

Class B
inherits A
protected overrides function fillclassvariables
myVar = "Class B"
end function

End Class



You're going to have to do that though. You can't inherit constructor
implementations. You're going to have to have overloaded constructors
for each class dervied from A - these can call MyBase.New(whatever), if
you like. Initialize myVar in them.

After reading that I was wondering if knew you didn't have to "recreate"
your constructors, you just a have to copy the definitions of the
contrutors.

Public Class A
sub new(1, 2, 3)
....
end sub
sub new(1, 2, 3, 4)
....
end sub
end class

Public Class B
sub new(1, 2, 3)
mybase.new(1,2,3)
do soemthing else
end sub
sub new(1, 2, 3, 4)
mybase.new(1,2,3,4)
do soemthing else
end sub
end class
 
G

Guest

Altman,

That's right, instructors are not inherited.

The first line of code in an inherited class's constructor must call the
appropriate constructor in the base class: Mybase.New (...)

Kerry Moorman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top