473,441 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

setting member variable in child class

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

Altman

Nov 23 '05 #1
8 1333

Altman wrote:
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.

--
Larry Lard
Replies to group please

Nov 23 '05 #2
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().
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Altman wrote:
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.

--
Larry Lard
Replies to group please

Nov 23 '05 #3
Altman wrote:
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().
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Altman wrote:
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.

--
Larry Lard
Replies to group please



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

Chris
Nov 23 '05 #4
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

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Altman wrote:
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.

--
Larry Lard
Replies to group please

Nov 23 '05 #5
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#?
"Altman" <No******@SickOfSpam.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.
--
TIA

Altman

Nov 23 '05 #6
>
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.
Nov 23 '05 #7
Altman wrote:
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

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Altman wrote:
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.

--
Larry Lard
Replies to group please



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
Nov 23 '05 #8
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
"Altman" wrote:
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#?
"Altman" <No******@SickOfSpam.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.
--
TIA

Altman


Nov 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
5
by: g18c | last post by:
I am trying to have a pointer to a member variable, however i will be deriving a number of classes from a base class. Whilst the code below works, i am wondering if there is a better way of doing...
5
by: Bob | last post by:
I want to find a way to detect the existance of the private member of a particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields()...
3
by: Fett | last post by:
Problem: My member variables in the child class are always null. I don't know why this happens! I have a base class for handling a user flow in my web, CWebUseCase, the class is declared like:...
11
by: alex | last post by:
Hi, how can I access / set properties of controls in other forms. i.e. mdi parent of mdi child mdi child of mdi child mdi child of mdi parent Thanks Alex
8
by: pauldepstein | last post by:
I am writing a program which looks at nodes which have coordinates which are time-dependent. So I have a class called node which contains the private member declarations int date; int month; ...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
3
by: Osama Aouda | last post by:
Hi All, If you have the following class: class foo { private: int offset; public: void SetOffset(int InOffset); };
16
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.