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

inherit from class

I have a class called Company. I now want to make for one particular app a
CompanyEx class, which will inherit from Company.

So

class CompanyEx: Company

how do I set the base? kind of confused on this thing.

basically in the constructor it seems like I would do something like

public CompanyEx(Company company)
{
base = company
}

but that obviously doesn't work. but now you can see what i'm trying to do.
please help! :)

May 16 '07 #1
11 1594
On Wed, 16 May 2007 15:27:51 -0700, Nathan Laff <re******@hotmail.com
wrote:
[...]
how do I set the base? kind of confused on this thing.

basically in the constructor it seems like I would do something like

public CompanyEx(Company company)
{
base = company
}

but that obviously doesn't work. but now you can see what i'm trying to
do. please help! :)
You can call the base constructor by putting ": base(<parameter list>)"
after the constructor declaration. For example:

public CompanyEx(Company company) : base(company)
{
}

Note that the above does a very specific thing: it initializes a new
CompanyEx instance using an existing Company instance. For that to work,
your Company class must have a constructor that takes as a parameter an
existing Company instance.

It's important to understand that the Company-inherited parts of the new
CompanyEx instance are new just as the CompanyEx is new. That is, you can
copy stuff from the existing Company instance into the new CompanyEx
instance, using the Company class constructor that takes an existing
Company instance. But the instance passed in will not have any long-term
relationship with the newly created CompanyEx instance unless you write
additional code that would implement that. In particular, if all you're
doing is cloning the passed-in Company instance into the new Company
instance (itself a part of the new CompanyEx instance), then changing the
passed-in Company instance later isn't going to change the new Company
instance.

Pete
May 16 '07 #2
"Nathan Laff" <re******@hotmail.comwrote in message
news:02**********************************@microsof t.com...
>I have a class called Company. I now want to make for one particular app a
CompanyEx class, which will inherit from Company.

So

class CompanyEx: Company

how do I set the base? kind of confused on this thing.

basically in the constructor it seems like I would do something like

public CompanyEx(Company company)
{
base = company
}

but that obviously doesn't work. but now you can see what i'm trying to
do. please help! :)
You can't "set the base". If you have an existing Company object you cannot
turn it into a CompanyEx, you have to create a CompanyEx class from the
start.
>

May 17 '07 #3

"Nathan Laff" <re******@hotmail.comwrote in message
news:02**********************************@microsof t.com...
>I have a class called Company. I now want to make for one particular app a
CompanyEx class, which will inherit from Company.

So

class CompanyEx: Company

how do I set the base? kind of confused on this thing.

basically in the constructor it seems like I would do something like

public CompanyEx(Company company)
{
base = company
}

but that obviously doesn't work. but now you can see what i'm trying to
do. please help! :)
Here is an example.

http://support.microsoft.com/kb/307205

May 17 '07 #4
On May 17, 1:27 am, "Nathan Laff" <rev23...@hotmail.comwrote:
I have a class called Company. I now want to make for one particular app a
CompanyEx class, which will inherit from Company.

So

class CompanyEx: Company

how do I set the base? kind of confused on this thing.

basically in the constructor it seems like I would do something like

public CompanyEx(Company company)
{
base = company

}

but that obviously doesn't work. but now you can see what i'm trying to do.
please help! :)
Dear Nathan,

Unlike some languages, C# does not provide a way for what you are
asking. If you create a new object (and therefor an instance of its
base classes) and want to copy the values from an existing base class,
you have to write the appropriate method yourself.

// For example
public CompanyEx(Company company)
{
base.name = company.name;
base.id = company.id
}

Cheers,
Moty.

May 17 '07 #5
On May 17, 1:27 am, "Nathan Laff" <rev23...@hotmail.comwrote:
I have a class called Company. I now want to make for one particular app a
CompanyEx class, which will inherit from Company.

So

class CompanyEx: Company

how do I set the base? kind of confused on this thing.

basically in the constructor it seems like I would do something like

public CompanyEx(Company company)
{
base = company

}

but that obviously doesn't work. but now you can see what i'm trying to do.
please help! :)
Dear Nathan,

Unlike some languages, C# does not provide a way to achieve what you
want. If you create a new object (and therefor instances of its base
class) and want to copy the values from an existing base class, you
have to write the appropriate method yourself.

// For Example
public CompanyEx(Company company)
{
base.id = company.id;
base.name = company.name;
// etc.
}

Hope this helps.

Cheers,
Moty

May 17 '07 #6
On May 17, 1:27 am, "Nathan Laff" <rev23...@hotmail.comwrote:
I have a class called Company. I now want to make for one particular app a
CompanyEx class, which will inherit from Company.

So

class CompanyEx: Company

how do I set the base? kind of confused on this thing.

basically in the constructor it seems like I would do something like

public CompanyEx(Company company)
{
base = company

}

but that obviously doesn't work. but now you can see what i'm trying to do.
please help! :)
Dear Nathan,

Unlike some languages, C# does not provide a native way to achieve
what you want. If you create a new object and want to copy the values
from an existing object, you have to write the appropriate method
yourself.

There is another way to do it but it depends on if you have access to
the Company code: each Company will implement a "Copy
Constructor" (Not native, but again, a method of your self), and
create a constructor for the CompanyEx that passes to it's base class
the instance of the base class you want.

public CompanyEx(Company company) : base(company)
{
}

be ware that the company reference passed to base class will just be
used to copy the properties and not to set the CompanyEx base class
instance to the passed value!

Hope this helps.

Cheers,
Moty

May 17 '07 #7
On May 17, 1:44 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 16 May 2007 15:27:51 -0700, Nathan Laff <rev23...@hotmail.com>
wrote:
[...]
how do I set the base? kind of confused on this thing.
basically in the constructor it seems like I would do something like
public CompanyEx(Company company)
{
base = company
}
but that obviously doesn't work. but now you can see what i'm trying to
do. please help! :)

You can call the base constructor by putting ": base(<parameter list>)"
after the constructor declaration. For example:

public CompanyEx(Company company) : base(company)
{
}

Note that the above does a very specific thing: it initializes a new
CompanyEx instance using an existing Company instance. For that to work,
your Company class must have a constructor that takes as a parameter an
existing Company instance.

It's important to understand that the Company-inherited parts of the new
CompanyEx instance are new just as the CompanyEx is new. That is, you can
copy stuff from the existing Company instance into the new CompanyEx
instance, using the Company class constructor that takes an existing
Company instance. But the instance passed in will not have any long-term
relationship with the newly created CompanyEx instance unless you write
additional code that would implement that. In particular, if all you're
doing is cloning the passed-in Company instance into the new Company
instance (itself a part of the new CompanyEx instance), then changing the
passed-in Company instance later isn't going to change the new Company
instance.

Pete
Pete,

you prefaced me :)

Moty

May 17 '07 #8
On May 17, 1:27 am, "Nathan Laff" <rev23...@hotmail.comwrote:
I have a class called Company. I now want to make for one particular app a
CompanyEx class, which will inherit from Company.

So

class CompanyEx: Company

how do I set the base? kind of confused on this thing.

basically in the constructor it seems like I would do something like

public CompanyEx(Company company)
{
base = company

}

but that obviously doesn't work. but now you can see what i'm trying to do.
please help! :)
Dear Nathan,

Unlike some languages, C# does not provide a native way to achieve
what you want. If you create a new object and want to copy the values
from an existing object, you have to write the appropriate method
yourself.

There is another way to do it but it depends on if you have access to
the Company code: each Company will implement a "Copy
Constructor" (Not native, but again, a method of your self), and
create a constructor for the CompanyEx that passes to it's base class
the instance of the base class you want.

public CompanyEx(Company company) : base(company)
{
}

be ware that the company reference passed to base class will just be
used to copy the properties and not to set the CompanyEx base class
instance to the passed value!

Hope this helps.

Cheers,
Moty

May 17 '07 #9
On May 17, 1:27 am, "Nathan Laff" <rev23...@hotmail.comwrote:
I have a class called Company. I now want to make for one particular app a
CompanyEx class, which will inherit from Company.

So

class CompanyEx: Company

how do I set the base? kind of confused on this thing.

basically in the constructor it seems like I would do something like

public CompanyEx(Company company)
{
base = company

}

but that obviously doesn't work. but now you can see what i'm trying to do.
please help! :)
Dear Nathan,

Unlike some languages, C# does not provide a native way to achieve
what you want. If you create a new object and want to copy the values
from an existing object, you have to write the appropriate method
yourself.

There is another way to do it but it depends on if you have access to
the Company code: each Company will implement a "Copy
Constructor" (Not native, but again, a method of your self), and
create a constructor for the CompanyEx that passes to it's base class
the instance of the base class you want.

public CompanyEx(Company company) : base(company)
{
}

be ware that the company reference passed to base class will just be
used to copy the properties and not to set the CompanyEx base class
instance to the passed value!

Hope this helps.

Cheers,
Moty

May 17 '07 #10
Thanks everyone for your replies.

That's basically what I figured, that there was no native way to do it, but
wanted to make sure.

May 17 '07 #11
On May 17, 7:44 pm, "Nathan Laff" <rev23...@hotmail.comwrote:
Thanks everyone for your replies.

That's basically what I figured, that there was no native way to do it, but
wanted to make sure.
Sorry for the duplicate posts :)

May 17 '07 #12

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

Similar topics

3
by: nikolai.onken | last post by:
Hello, I got following problem trying to inherit a variable from another class class Core { public $obj; function initialize($obj) {
7
by: bobsled | last post by:
For class A to reuse functionality of a class B, A could either contains B or inherits from B. When should pick which? In a book the author says that "Don't inherit from a concrete class." Does...
4
by: Slavyan | last post by:
(I just started to learn C#.NET) What's the syntax in c# for a class to inherit more than one class. I know following syntax: public class MyClass : MyOtherClass { } but I need to inherit...
6
by: Mohammad-Reza | last post by:
I wrote a component using class library wizard. In my component i want to in order to RightToLeft property do some works. I can find out if user set this property to Yes or No, But if He/She set it...
5
by: john conwell | last post by:
I'm trying to make a class that already inherits from a base class, also inherit from an interface in managed C++. so my interface looks like so: __gc interface ITask { __property String*...
2
by: ad | last post by:
We can inherit a class. But if the class is the code behind, when we inherit it , can we inherit it's aspx ahead together ?
7
by: Frank | last post by:
Hi, a question probably asked before, but I can't find the answers. Base class X, classes A, B and C inherit class X. In class A I do not want to inherit property (or function or method) P1....
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
3
by: J | last post by:
I tried to inherit 'Shot' class from 'Image' class, only to fail. It gives me the CS0122 error, which says that it can't access 'System.Drawing.Image.Image()'. What am I missing? using...
2
by: Joe | last post by:
Is it possible to inherit from a UserControl? If I try my user control class is not recognized. Thanks, Joe
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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:
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.