473,465 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Owner and base

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
Mar 14 '07 #1
7 1954
On Mar 14, 1:25 pm, "MarkJ" <dvs_...@sbcglobal.netwrote:
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.

<snip>
// 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

Mar 14 '07 #2
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

"MarkJ" <dv*****@sbcglobal.netwrote in message
news:rJ******************@newssvr14.news.prodigy.n et...
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


Mar 14 '07 #3
Hi,

"MarkJ" <dv*****@sbcglobal.netwrote in message
news:rJ******************@newssvr14.news.prodigy.n et...
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?
Mar 14 '07 #4
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

"Simon Tamman" <i_**********************************@NOSPAMhotmai l.com>
wrote in message news:H5*************@newsfe5-win.ntli.net...
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

"MarkJ" <dv*****@sbcglobal.netwrote in message
news:rJ******************@newssvr14.news.prodigy.n et...
>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



Mar 14 '07 #5
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
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Mar 14, 1:25 pm, "MarkJ" <dvs_...@sbcglobal.netwrote:
>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.

<snip>
>// 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

Mar 14 '07 #6
MarkJ <an*******@yahoo.comwrote:
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);
--
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
Mar 14 '07 #7
Thank you very much jon

MarkJ

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
MarkJ <an*******@yahoo.comwrote:
>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);
--
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

Mar 14 '07 #8

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

Similar topics

3
by: Wojciech Trelak | last post by:
Hi, Does anyone know how to change an owner of a control inherited from the base form? In code it's simple, but the real question is how to force designer to do this? Is it possible? I tried...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
7
by: Dave Coate | last post by:
Hi everyone, I am looking for a way to 'override' file security and read the Owner of a file to which I have no access. I am a system administrator, as such I have administrative rights to all...
3
by: Dave Coate | last post by:
Hello again, I am going to re-post a question. I got some excellent suggestions from Rob and Mattias on this but their ideas did not solve the problem. Here is the original post: ...
1
by: Mike Mascari | last post by:
While migrating to 7.4, which performs quite nicely btw, I must have performed some sequence of the migration incorrectly. Now, when I use pg_dump on a database for backup, I get: pg_dump:...
4
by: Nayan | last post by:
The base process owns this thread. But the visible window is owned by the thread. How do I get the owner Process ID from a Thread ID? To understand, look at this "<<--" pointer in the...
7
by: Laurence | last post by:
Hi folks, Who knows how to retrieve the owner of SQL objects, such as SCHEMA, TABLE etc.? Is GRANTOR within catalog view owner? Thanks, Laurence
2
by: levimc | last post by:
I know that that topic may be old to you but I looked at other more- than-two-year-old topics related to mine. However, I didn't find them working for my project at all because its errors return...
10
by: SimeonD | last post by:
Hi Is there a way I can fnd the Owner of a folder, using vb.net? I know how to find the permissions, but I can't figure how to find the owner. Thanks SimeonD
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...
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.