inherit from class

N

Nathan Laff

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! :)
 
P

Peter Duniho

[...]
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
 
M

Michael C

Nathan Laff said:
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.
 
M

Mr. Arnold

Nathan Laff said:
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
 
M

Moty Michaely

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.
 
M

Moty Michaely

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
 
M

Moty Michaely

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
 
M

Moty Michaely

[...]
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
 
M

Moty Michaely

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
 
M

Moty Michaely

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
 
N

Nathan Laff

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.
 
M

Moty Michaely

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 :)
 

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