Accessing the base class

  • Thread starter Thread starter Robby White
  • Start date Start date
R

Robby White

How can access properties of an object lower down in the
hierarchy of my class structure? For example;

Company.Customers(0).Products(0).ShowCompanyName

In the above Show method I want to access the Name
property with the Company class.

Cheers, Robby
 
I'm not quite sure what you're trying to do here, but let me see if I
have the basic idea first...

class company
{
public:
char name[20];
...
};

class customer : public company
{
public:
char name[20]; // Overrides company::name
...
void ShowCompanyName( void); // Should display the name from class
company, not customer
};

When you call the ShowCompany function for an object of type company
(or any class that derrives from it), you will by default use the
most-derrived versions of the data members or member functions you
call. You can override this with the scope resolution operator (::).
That is what it's for. Consider this:

void company::ShowCompanyName( void)
{
cout << "Customer Name:\t" << customer::name << endl; // Use :: to
override scope, showing the customer information instead.

cout << "Company Name:\t" << name << endl; // By default, we use the
most-derrived version of the member, in this case the company name.

return;
}

If products inherits from company, but you do not define a name member
for the product class, then the above example would still work,
because the name defined for company would still be the most derrived
one in products. If products also has a name member, then you would
simply use the proper class and the scope resolution operator to get
the company and customer names, and then omit it for the product name.

I'm a C++ programmer, and I'm guessing that you're doing something
here with C# and inheritance, but if I'm wrong, please re-post and
I'll see if I can help you out. :) Good luck!

JIM
 
Sorry I should have explained better! Im not using
inheritance as these are distinct classes in there own
right. I think I have a workaround which basically
declares a Parent property for each level?
-----Original Message-----
I'm not quite sure what you're trying to do here, but let me see if I
have the basic idea first...

class company
{
public:
char name[20];
...
};

class customer : public company
{
public:
char name[20]; // Overrides company::name
...
void ShowCompanyName( void); // Should display the name from class
company, not customer
};

When you call the ShowCompany function for an object of type company
(or any class that derrives from it), you will by default use the
most-derrived versions of the data members or member functions you
call. You can override this with the scope resolution operator (::).
That is what it's for. Consider this:

void company::ShowCompanyName( void)
{
cout << "Customer Name:\t" << customer::name << endl; // Use :: to
override scope, showing the customer information instead.

cout << "Company Name:\t" << name << endl; // By default, we use the
most-derrived version of the member, in this case the company name.

return;
}

If products inherits from company, but you do not define a name member
for the product class, then the above example would still work,
because the name defined for company would still be the most derrived
one in products. If products also has a name member, then you would
simply use the proper class and the scope resolution operator to get
the company and customer names, and then omit it for the product name.

I'm a C++ programmer, and I'm guessing that you're doing something
here with C# and inheritance, but if I'm wrong, please re-post and
I'll see if I can help you out. :) Good luck!

JIM


How can access properties of an object lower down in the
hierarchy of my class structure? For example;

Company.Customers(0).Products(0).ShowCompanyName

In the above Show method I want to access the Name
property with the Company class.

Cheers, Robby

.
 
Hi Robby,

Thanks for your post. I believe it's a good idea to have a property
pointing to parent object.

Please feel free to let me know if there is any further I can help
regarding this issue.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
If you expose a parent property or a specific "child" property, make very
sure that it's going to have the intended use. If customers is a
collection, and products is a collection, then ShowCompanyName might be
different for each object, right? So Company.ShowCompanyName wouldn't
really apply to company (unless it is a different property with the same
name).

-mike
MVP
 
Back
Top