I have created a read/write property for name input validation inside my class. Right now to get my code working I'm just validating inside my main window but am require to use the my class property instead. How would I reference this in place of the validation already inside my main window? These are the properties in my Worker class:
//Validates if anything has been entered for name
internal string workName
{
get
{
return Name;
}
set
{
if (string.IsNullOrEmpty(workerName))
MessageBox.Show("You must enter something for name.", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
else
Name = value;
}
}
//Validates range and returns the work messages for calculation
internal int workMessages
{
get
{
return Messages;
}
set
{
if (messagesSent <= 0)
throw new ArgumentOutOfRangeException("You must enter a number greater than 0.");
else
Messages = value;
}
}
This is where I want to replace the validation in my main. As you can see I have to make a new set of variables and validation.
private void btnCalculate_Click(object sender, RoutedEventArgs e)
{
string strName = txtName.Text.Trim();
int intMessages;
if (string.IsNullOrEmpty(strName))
{
MessageBox.Show("You must enter something for name.", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (!int.TryParse(txtMessages.Text.Trim(), out intMessages)|| intMessages < 0){
MessageBox.Show("You must enter a number greater than 0 for messages sent.","ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
else
{
//Increment each time a worker is submitted
Worker.totalWorkers++;
//Add and store each time messages sent is valid
int totalMessages = int.Parse(txtMessages.Text.Trim());
Worker.totalMessages += totalMessages;
}
I want to leave the else statement in there but keep validation inside my class. How would my if statements look in order to do this?