473,385 Members | 1,919 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,385 software developers and data experts.

Base classes using inherited values

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

Apr 2 '07 #1
16 2416
gw********@gmail.com wrote:
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.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Apr 2 '07 #2
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

"gw********@gmail.com" wrote:
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

Apr 2 '07 #3
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

Apr 2 '07 #4
Thanks to you too wibberlet,

Didnt solve my problem but good thing to know!

Graeme

Apr 2 '07 #5
Would also like to point people to:

http://msdn2.microsoft.com/en-us/library/ms173153.aspx

which helped me out.

Apr 2 '07 #6
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!!

Apr 2 '07 #7
gw********@gmail.com wrote:
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'.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
Apr 2 '07 #8
protected virtual CVirageObject parent;

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

Apr 2 '07 #9
On 2 Apr, 13:51, "gwoodho...@gmail.com" <gwoodho...@gmail.comwrote:
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.

Apr 2 '07 #10
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.

Apr 2 '07 #11
On 2 Apr 2007 07:27:16 -0700, "gw********@gmail.com"
<gw********@gmail.comwrote:
>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 want it to print "new string!" in the console.
Apr 2 '07 #12
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).

Apr 2 '07 #13
gw********@gmail.com <gw********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 2 '07 #14
<gw********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
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!";
}
}
}
Apr 2 '07 #15
Thanks for that bill,

Thats the way im going to have to do things. Guess im getting my mind
on non web programming.

Apr 3 '07 #16
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

*** Sent via Developersdex http://www.developersdex.com ***
Apr 3 '07 #17

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

Similar topics

1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
2
by: Josh Mcfarlane | last post by:
I'm doing recomposition of objects from binary streams, and the best way for me to write them out is to write base class data first, forward to inherited classes, pointer class values, etc. Now,...
8
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this?...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
5
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1"...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.