Base classes using inherited values

  • Thread starter Thread starter gwoodhouse
  • Start date Start date
G

gwoodhouse

Good Morning,

Ive been programming in c# for a few months now, and one of the things
i havnt quite figured out is this:

I would like to have inherited classes with their own set of
variables. I would like to use an inherited method, but instead of
using the base's variables, i want the application to instead use the
inherited methods values.

Now i've played around with the "this" keyword but havn't gotten very
far - It's also one of those problems i find it hard to google!

Hopefully someone could help me out with this?

Thanks in advance!

Graeme Woodhouse
 
Good Morning,

Ive been programming in c# for a few months now, and one of the things
i havnt quite figured out is this:

I would like to have inherited classes with their own set of
variables. I would like to use an inherited method, but instead of
using the base's variables, i want the application to instead use the
inherited methods values.

You say variables, but I suspect that's not what you mean - if a base
class has a member variable accessible to descendants, its a simple
matter to assign whatever value to it. I think you might actually be
after something like this:

class BaseClass
{
....
public virtual int Foo { get { return 3; } }

public void WriteFoo()
{
Console.WriteLine("My Foo is {0}\n", this.Foo);
}
....
}

class DerivedClass : BaseClass
{
....
public override int Foo { get { return 7; } }
....
}

Elsewhere:

BaseClass bar1 = new BaseClass();
BaseClass bar2 = new DerivedClass();
bar1.WriteFoo();
bar2.WriteFoo();

The WriteFoo method is inherited fromBaseClass by DerivedClass, but the
value it prints is DerivedClass's Foo or BaseClass's Foo as appropriate.
 
Hi Graeme,

I have posted some code snippets here that demonstrate what I think you are
talking about. They are only intended to demonstrate the principle you
describe and do not represent what I would consider good OO practise. It's
interesting all the same....

Consider the following two classes:

class BaseClass
{
protected int baseInt = 100;

public virtual int BaseMethod()
{
return baseInt;
}
}

class DerivedClass:BaseClass
{

}

When you do something like:
DerivedClass classBase = new DerivedClass();
Console.WriteLine(classBase.BaseMethod());

You will of course get the value of the integer (100) as defined in the base
class.

It is, however, possible to override the BaseMethod in your DerivedClass and
also declare your intention to 'hide' the value of the integer in your base
class. If you change the DerivedClass definition to:

class DerivedClass:BaseClass
{
private new int baseInt = 200;

public override int BaseMethod()
{
return baseInt;
}
}

When you run the code now you will get 200 returned as defined in the
derived class. The 'new' keyword shows your intention to hide the base class
variable.

Hope this helps - wibberlet
Development blog at http://wibberlet.blogspot.com
 
Thanks Larry,

Thats exactly what i needed. Hi-ho Hi-ho, its off to rewrite a good
portion of my application we go. *whistles*

Graeme
 
Thanks to you too wibberlet,

Didnt solve my problem but good thing to know!

Graeme
 
Well, after all that my code isnt accepting the changes.

Heres my code:

namespace VACToolPrototype
{
[Serializable]
public class CVirageObject
{
private CVirageObject parent;

public CVirageObject Parent
{
get { return parent; }
set { parent = value; }
}
}

namespace VACToolPrototype
{
public class CPhysicalCamera : CVirageObject
{
#region variable declarations
private CWittwinObject Parent = new CWittwinObject;
}
}

Now, i would like to do:

CPhysicalCamera bob = new CPhysicalCamera();
if(bob.Parent is CWittwinObject)
MessageBox.Show("This worked");

unfoutunatly i get the error: " The modifier 'override' is not valid
for this item" when trying to add virtual and ovveride to the parent
variables.

Help!!
 
Well, after all that my code isnt accepting the changes.

Heres my code:

namespace VACToolPrototype
{
[Serializable]
public class CVirageObject
{
private CVirageObject parent;

public CVirageObject Parent
{
get { return parent; }
set { parent = value; }
}
}

namespace VACToolPrototype
{
public class CPhysicalCamera : CVirageObject
{
#region variable declarations
private CWittwinObject Parent = new CWittwinObject;
}
}

Now, i would like to do:

CPhysicalCamera bob = new CPhysicalCamera();
if(bob.Parent is CWittwinObject)
MessageBox.Show("This worked");

unfoutunatly i get the error: " The modifier 'override' is not valid
for this item" when trying to add virtual and ovveride to the parent
variables.

The access modifier 'private' makes a member visible within that class
*only* - not even descendants can see it. So the compiler won't let you
put 'override' on a private member, because it wouldn't make any sense.
Make it 'protected'.
 
protected virtual CVirageObject parent;

still gives "error CS0106: The modifier 'virtual' is not valid for
this item".
 
protected virtual CVirageObject parent;

still gives "error CS0106: The modifier 'virtual' is not valid for
this item".

Don't you want to make the property protected virtual not the member
variable? make parent private again and make Parent protected virtual.
 
Here is some code i wrote to simulate what im trying to get at.

After staring at this for a few hours im getting the feeling C# may
not be able to act in the way i want it to:

namespace InhertanceTesting
{
class Program
{
static void Main(string[] args)
{
inhertedClass ic = new inhertedClass();
Console.WriteLine(ic.StringObject);
}
}

class baseClass
{
private string stringObject = "Base's String";

public baseClass() { }

public string StringObject
{
get { return this.stringObject; }
}
}

class inhertedClass : baseClass
{
private string stringObject = "new string!";

public inhertedClass() { }
}
}

i want it to print "new string!" in the console.
 
Here is some code i wrote to simulate what im trying to get at.

After staring at this for a few hours im getting the feeling C# may
not be able to act in the way i want it to:

namespace InhertanceTesting
{
class Program
{
static void Main(string[] args)
{
inhertedClass ic = new inhertedClass();
Console.WriteLine(ic.StringObject);
}
}

class baseClass
{
private string stringObject = "Base's String";

public baseClass() { }

public string StringObject
{
get { return getStringObject; }
protected virtual string getStringObject()
{
return stringObject;
}
}

class inhertedClass : baseClass
{
private string inheritedClassStringObject = "new string!";
public inhertedClass() { }
protected virtual string getStringObject()
{
return inheritedClassStringObject;
}
 
I understand that i can simply override the method.

I dont /want/ to override the method, i inhert from the base class in
order to gain all the methods.

I just want the method to act as if it was written into the inherited
class. (in java/jsp programming i would use and include).
 
I understand that i can simply override the method.

I dont /want/ to override the method, i inhert from the base class in
order to gain all the methods.

I just want the method to act as if it was written into the inherited
class. (in java/jsp programming i would use and include).

Java and C# are almost exactly the same in terms of inheritance. If you
could give a short but complete Java example of what you want to do, I
can almost certainly translate it into C# for you. I'm afraid at the
moment though, I still don't really understand the issue.
 
Here is some code i wrote to simulate what im trying to get at.

After staring at this for a few hours im getting the feeling C# may
not be able to act in the way i want it to:

Here is a modified version

using System;

namespace InhertanceTesting
{
class Program
{
static void Main(string[] args)
{
inhertedClass ic = new inhertedClass();
Console.WriteLine(ic.StringObject);
}
}

class baseClass
{
protected string stringObject = "Base's String"; //made this
protected so that it is accessible from the child

public baseClass() { }

public string StringObject
{
get { return this.stringObject; }
}
}

class inhertedClass : baseClass
{
// private string stringObject = "new string!"; // get rid of this

public inhertedClass()
{
stringObject = "new string!";
}
}
}
 
Thanks for that bill,

Thats the way im going to have to do things. Guess im getting my mind
on non web programming.
 
Good morning? in a world of internationalization and localization?:)
Below is the code that should help you. I hope I have understood your
query properly.

class ty
{
public int i = 0;
}
class Program
{
static void Main(string[] args)
{
ti obj = new ti();
obj.meth();
}

}
class ti : ty
{
public ti()
{
i = 12;
}
private int i;
public void meth()
{
Console.WriteLine(i);
}

}

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

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

Back
Top