Inheritance

  • Thread starter OldButStillLearning
  • Start date
O

OldButStillLearning

I have a class which really defines a particular record. All of the fields
of this record are the same for another record, but they have different rules
as to what can be in those fields. So I thought I should define a class, say
"Record_1" and it has a method call "Validate_Record" which looks at the
validates all of the fields. Then I would create another class, let's say it
is called "Record_2" and then override the "Validate_Record" method to
provide the unique field validations which are required for the "Record_2"
class.

My first issue when I do this is the constructor. I get a message of "no
overload method for "Record_1" takes '0' arguements when I have the
constructor for Record_2...

Public Record_2(string rec)

I am also getting errors on each of the properties defined for the fields of
the Record_1 class saying the field is inaccessible due to its protection
level. Both of these classes are in the same namespace, do I need another
value for "private" for each of these variables, I don't think I should be
using public.

Thanks in advance for your assistance!
 
T

Techno_Dex

It sounds like you should use an abstract class or an Interface if the data
is the same between the two objects and both your Record_1 and Record_2
should inherit from the abstract or implement the interface depending on
what your trying to do. Even a simple BaseObject would probably work just
fine.

public abstract class RecordBase
{
private int miKey;
private string msName;

public int Key
{
get {return miKey;}
set {miKey = value;}
}
public string Name
{
get {return msName;}
set {msName = value;}
}
}

public class Record1 : RecordBase
{
//Constructor
public Record1()
{

}

public void Validate()
{

}
}

public class Record2 : RecordBase
{
//Constructor
public Record2(string rec)
{

}

public void Validate()
{

}
}
 
F

Family Tree Mike

OldButStillLearning said:
I have a class which really defines a particular record. All of the fields
of this record are the same for another record, but they have different rules
as to what can be in those fields. So I thought I should define a class, say
"Record_1" and it has a method call "Validate_Record" which looks at the
validates all of the fields. Then I would create another class, let's say it
is called "Record_2" and then override the "Validate_Record" method to
provide the unique field validations which are required for the "Record_2"
class.

My first issue when I do this is the constructor. I get a message of "no
overload method for "Record_1" takes '0' arguements when I have the
constructor for Record_2...

Then it sounds like Record_2 should have something like this:

public class Record_2 : Record_1
{
// call the base constructor with a param
public Record_2(string s) : base (s)
{
}
}
Public Record_2(string rec)

I am also getting errors on each of the properties defined for the fields of
the Record_1 class saying the field is inaccessible due to its protection
level. Both of these classes are in the same namespace, do I need another
value for "private" for each of these variables, I don't think I should be
using public.

Thanks in advance for your assistance!

If you have properties, why are you touching the fields that back them?
That's a no-no. Access the properties, or, if you feel you must touch
the fields, make them protected, not private or public. Protected makes
them available in your subclass, but not to outside objects.
 
P

proxyuser

OldButStillLearning said:
I have a class which really defines a particular record. All of the fields
of this record are the same for another record, but they have different
rules
as to what can be in those fields. So I thought I should define a class,
say
"Record_1" and it has a method call "Validate_Record" which looks at the
validates all of the fields.

As Techno has pointed out, calling the method Validate_Record is a faux pas
in OO programming. The context is already a record, so it's redundant to
embed that name in the function name. It would be like saying "Bob, hi
Bob." You want to write code that looks like:

Record_1 record1 = new Record_1();
record1.Validate();
 

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