I have a question that relates more to OO theory rather than C# specifically. What I want to know is that if I have created a parent class and the child class should the child class override the parent's methods or should it use the parent's methods. I will post my 2 classes below as I know what I'm trying to say, but I don't think I'm articulating it correctly.
Parent Class
public abstract class AppFormData
{
//Variables
private String errorMsg;
private int fieldErrorNum;
//No argument constructor
public AppFormData()
{
}
//Getters and setters
protected void setErrorMsg(String msg)
{
errorMsg = msg;
}
private String getErrorMsg()
{
return errorMsg;
}
protected void setFieldErrorNum(int num)
{
fieldErrorNum = num;
}
private int getFieldErrorNum()
{
return fieldErrorNum;
}
/// <summary>
/// Returns an error message that has been set by the checkPage()
/// method.
/// </summary>
/// <returns></returns>
public String getErrorMessage()
{
return getErrorMsg();
}
/// <summary>
/// Returns an integer indicating at which input field an
/// error occurred.Integers from 0. Eg if the error occurred
/// at the first input field, this method would return 0, if
/// the error occurred at the 3rd input field, this method
/// would return 2.
/// </summary>
/// <returns></returns>
public int fieldNumWhereErrorOccurred()
{
return getFieldErrorNum();
}
/// <summary>
/// Method that validates all of the input fields on the
/// page to see if they are valid and conform to the
/// business rules. Returns true if all fields are valid
/// and returns false if any of the fields are invalid.
/// Triggers the setting of the errorMessage and the
/// fieldErrorNum valiables.
/// </summary>
/// <returns></returns>
public Boolean isPageDataValid()
{
return checkPage();
}
/// <summary>
/// Protected method that needs to be written by the
/// child class. It checks each input field by instantiating
/// the ValidateUserInput and DateCheck classes. It also
/// sets the errorMessage and sets the fieldErrorNum if an
/// error occurs. Returns true if all input fields pass
/// validation, returns false if any input fields fail
/// the validation.
/// </summary>
/// <returns></returns>
protected abstract bool checkPage();
//Protected method that checks if the input is
//null or empty. If true it sets the setter to "0"
//which is a rule of the PeopleSoft system. Else
//it sets the setter to the field input value.
protected String setStringData(String input)
{
string setter;
input = input.Trim();
if (input == "" || input == null)
{
setter = "0";
}
else
{
setter = input;
}
return setter;
}
}
Child Class
public class PersonDetails1 : AppFormData
{
//Variables
String userTitle;
String fName;
String lName;
String mName;
String prevName;
String dob;
String gender;
String prevSID;
ValidateUserInput userData = new ValidateUserInput();
DateCheck birthday = new DateCheck();
//Constructor
public PersonDetails1(String title, String firstName, String middleName, String lastName,
String previousName, String dateBirth, String gender, String previousStudentID)
{
setUserTitle(title);
setFName(firstName);
setMName(middleName);
setLName(lastName);
setPrevName(previousName);
setDob(dateBirth);
setGender(gender);
setPrevSID(previousStudentID);
}
//Setters and Getters
private void setUserTitle(String title)
{
userTitle = title;
}
private String getUserTitle()
{
return userTitle;
}
private void setFName(String firstName)
{
fName = setStringData(firstName);
}
private String getFName()
{
return fName;
}
private void setLName(String lastName)
{
lName = setStringData(lastName);
}
private String getLName()
{
return lName;
}
private void setMName(String middleName)
{
mName = setStringData(middleName);
}
private String getMName()
{
return mName;
}
private void setPrevName(String pName)
{
prevName = setStringData(pName);
}
private String getPrevName()
{
return prevName;
}
private void setDob(String dateBirth)
{
dob = setStringData(dateBirth);
}
private String getDob()
{
return dob;
}
private void setGender(String sex)
{
gender = setStringData( sex);
}
private String getGender()
{
return gender;
}
private void setPrevSID(String sid)
{
prevSID = setStringData( sid);
}
private String getPrevSID()
{
return prevSID;
}
//Methods
/// <summary>
/// returns any error messages received from the validation process
///
/// </summary>
/// <returns></returns>
public String getErrorMessage()
{
return getErrorMsg();
}
/// <summary>
/// Returns an integer from 0 to 7 indicating in order which input field
/// caused the error. eg if Title caused the error 0 would be returned,
/// if Date of Birth caused the error, 5 would be returned
/// </summary>
/// <returns></returns>
public int fieldNumWhereFirstErrorOccurred()
{
return getFieldErrorNum();
}
/// <summary>
/// returns a boolean value indicating if the data entered
///is valid data.Returns true if the page contains valid data or false
///if the data is invalid.
/// </summary>
///<returns></returns>
public Boolean isPageDataValid()
{
return (checkPage());
}
//Helper methods
//Provides the checking and validation for each page field.
//Overridden abstract method from Parent, AppFormData.
private override Boolean checkPage()
{
bool valid = true;
if (!(userData.isXMLDropDownValid(getUserTitle(), "TestTitle")))
{
valid = false;
setErrorMsg("Please select your title eg Mr, Mrs");
setFieldErrorNum(0);
}
if (valid == true)
{
if (!(userData.isStringSafe(getFName())))
{
valid = false;
setErrorMsg("Please enter your first name");
setFieldErrorNum(1);
}
}
if (valid == true)
{
if (getMName() != "0")
{
if (!(userData.isStringSafe(getMName())))
{
valid = false;
setErrorMsg("Please enter your middle name or leave the field blank if you " +
"do not have a middle name");
setFieldErrorNum(2);
}
}
}
if (valid == true)
{
if (!(userData.isStringSafe(getLName())))
{
valid = false;
setErrorMsg("Please enter your last name");
setFieldErrorNum(3);
}
}
if (valid == true)
{
if (getPrevName() != "0")
{
if (!(userData.isStringSafe(getPrevName())))
{
valid = false;
setErrorMsg("Please enter your previous or maiden name or leave the field blank " +
"if you do not have a middle name");
setFieldErrorNum(4);
}
}
}
if (valid == true)
{
if (!(userData.isStringSafe(getDob())))
{
valid = false;
setErrorMsg("Please select your date of birth from the calendar");
setFieldErrorNum(5);
}
}
if (valid == true)
{
if (!(birthday.checkOver15(getDob()) > 0))
{
valid = false;
setErrorMsg(birthday.getErrorMessage());
setFieldErrorNum(5);
}
}
if (valid == true)
{
if (!(userData.isRadioButtonValid(getGender())))
{
valid = false;
setErrorMsg(birthday.getErrorMessage());
setFieldErrorNum(6);
}
}
if (valid == true)
{
if (getPrevSID() != "0")
{
if (!(userData.isStringSafe(getPrevSID())))
{
valid = false;
setErrorMsg("Please enter your previous student number. If you do not remember your " +
"student number please leave this field blank");
setFieldErrorNum(7);
}
}
}
return valid;
}
}
I guess what I'm trying to ask is should I use the isPageDataValid() method from the Parent class or do I write it again and use it from the Child class. I'm just a little confused.
Cheers
Bec
This post has been edited by becstribe: 27 January 2010 - 06:24 PM

New Topic/Question
Reply



MultiQuote




|