Owner and base

M

MarkJ

him im kinda new to c-sharp...
to reference up the class chain, how do i reference
the super class (parent)
example
class mybase
{
protected int abc=0
}
classs myclass:mybase
{
}
class another:myclass
{
public another()
{
// how would i reference up the inherit
int efg
efg=base:base:abc
}
}
// 2nd Question
ownership
if i instantiate a class from a top level of the app
how would i assign a owner to the class
example
class App
{
myclass oClass = new Myclass
// how can i assign App class as owner of myclass
}
thanks
MarkJ
 
S

Simon Tamman

Not 100% sure if I fully understood the question but i'll give the reply my
best shot.

"to reference up the class chain, how do i reference the super class
(parent)?"

In your example:
mybase->myClass->Another

An instance of "another" IS also a class of MyClass and MyBase. There is no
need to reference these classes because the class you are in IS the same
class.
Let us extend the example a little.

public class MyBase
{
private int m_Number = 1;

protected int Number
{
get{return m_Number;}
}
}

If the base class was defined like this you could easily do the following.

public class MyClass:MyBase
{
public MyClass()
{
}

public void WriteNumber()
{
Console.WriteLine(Number);
}
}

Because MyClass has access to all of the public and protected members of
MyBase.

The base keyword is only used in the following circumstances:

a) In constructor logic
b) In overrides or "news"

e.g.

public class MyBase
{
private int m_Number;

protected int Number
{
get{return m_Number;}
}

public MyBase()
{

}
public MyBase(int number)
{
m_Number = number;
}
}

public class MyClass:MyBase
{
// we pass the argument down to the base constructor
// that accepts an int
public MyClass(int number):base(number)
{

}
}

Override usage:

public class MyBase
{
public MyBase()
{
}

protected virtual void WriteText()
{
Console.WriteLine("I'm the base class");
}
}

public class MyClass:MyBase
{
public MyClass()
{
}

protected override void WriteText()
{
Console.WriteLine("I'm NOT the base class");
Console.WriteLine("This is the output of the base class:");
// here we access the method we have overridden
base.WriteText();
}
}


I don't fully understand your second question.
For a class to "own" another class, do you mean that they have an instance
of the other class as a member or they are the ONLY class that can create an
instance of the other class?

If you only need an instance then declaring it as a member (as you did in
your example) is correct.
If you want ClassX to be the only class that can use ClassY then do this.

public class ClassX
{
private ClassY cy = new ClassY();

public ClassX()
{
}

private ClassY
{
public ClassY()
{
}
}
}

Because ClassY is nested within ClassX and is private it cannot be created
by any other class but ClassX

HTH

Simon
 
J

Jon Skeet [C# MVP]

him im kinda new to c-sharp...
to reference up the class chain, how do i reference
the super class (parent)

<snip>

Just use "abc" and it will use the inherited version. If you have
another member called abc (which I'd advise against for readability -
it's more likely if you have an overridden method which needs to call
the base version, of course) just use base.abc.

// 2nd Question
ownership
if i instantiate a class from a top level of the app
how would i assign a owner to the class
example
class App
{
myclass oClass = new Myclass
// how can i assign App class as owner of myclass}

There's no such concept as an "owner". What are you trying to achieve?

Jon
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

MarkJ said:
him im kinda new to c-sharp...
to reference up the class chain, how do i reference
the super class (parent)
example


You do not have to do nothing especial, just use "abc" from the derived
class.
Now, another escenario would be if you have a method in the base class that
you redefine in your derived class, if you need to refer to the parent
method you use base.MethodName()
example:

class MyWinForm: System.Windows.Form
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); // call the parent

// Do your stuff
}
}


// 2nd Question
ownership
if i instantiate a class from a top level of the app
how would i assign a owner to the class
example

not sure what you mean, could you put another example?
 
M

MarkJ

i forgot to place a better example....
base class has method called one()
inherited class of base class had method called one()
reason for this is to handle some information
then hand it back to the super.Class
Method..of the same name ...
MarkJ
 
M

MarkJ

What i was talking about owner
class1 instiantiates Class2 how does class 2 reference class1
example
class app
{
class1 thisclass = new class1()
// oClass2 is a property of Class1
// how can Class2 Reference ThisClass if there is no
//reference to ThisClass (class1)
thisclass.oClass2=new Class2()
//in another language i sent the instatinting class
// into child
//class as a parameter and it was known as
//owner ochild class
//hope this helps a bit



}
//small example
public Class1
{
public class1()
{
//do somthing
}
}
.........
public class2
{
private object oOwner;
public class2(object owner)
{
this.oOwner=owner
}
}
somthing like the above
again im new to csharp
thanks
MarkJ
 
J

Jon Skeet [C# MVP]

MarkJ said:
What i was talking about owner
class1 instiantiates Class2 how does class 2 reference class1

Pass a reference into the constructor. Your class2 example was right,
but you'd do:

oClass2 = new Class2(class1);

or if you want the instance in which the code is running:

oClass2 = new Class2(this);
 
M

MarkJ

Thank you very much jon

MarkJ

Jon Skeet said:
Pass a reference into the constructor. Your class2 example was right,
but you'd do:

oClass2 = new Class2(class1);

or if you want the instance in which the code is running:

oClass2 = new Class2(this);
 

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