0

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?

  • Doing the textbox validation when the button is clicked is quite unelegant. It would be better to associate your validation rules directly with the textbox or with the viewmodel related to the textbox. How to do that? Maybe you can get inspiration from https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-implement-binding-validation?view=netframeworkdesktop-4.8 or from https://stackoverflow.com/questions/19539492/implement-validation-for-wpf-textboxes, for example. –  Oct 01 '22 at 21:42
  • These were my instructioncs: • When a user clicks Calculate, the program attempts to create a new piecework worker object. The validation of this worker will take place within the worker class – OGanunobyfan1629 Oct 01 '22 at 21:53
  • Calling `MessageBox.Show` and throwing exceptions are terrible ways to handle validation. Making the `Worker` class even know about the UI is terrible. It's not clean modular code. Also, you're not using standard C# naming conventions. – Enigmativity Oct 01 '22 at 22:39
  • According to your code, the logic of the check is not clear. What is the `messagesSent` variable, for example? Or are `Name` and `Messages` private fields, and are they accessed elsewhere? So I can't give an example. Please attach the complete code of the Worker class. Better in a simplified form, only the part that relates to the question. Also, for validation, one of the `IDataErrorInfo` interfaces (obsolete, but simpler) or `INotifyDataErrorInfo` (new, more advanced) must be implemented in the data. – EldHasp Oct 02 '22 at 04:40
  • In WPF you would usually use either binding validation or implement (property) validation in the data model (your Worker class). Since you want the validation logic to be in your Worker class, you should implement the INotifyDataErrorInfo interface. In both cases, when you bind to the validated properties, the GUI will give the user a visual error feedback and display custom messages to communicate the error. This is a build-in but customizable feature of WPF. [How to add validation to view model properties or how to implement INotifyDataErrorInfo](https://stackoverflow.com/a/56608064/3141792) – BionicCode Oct 02 '22 at 12:11
  • This is more convenient and visually appealing than showing the MessageBox. For both, the developer and the user. – BionicCode Oct 02 '22 at 12:15
  • 1
    Also always avoid throwing exceptions as they will make the application slow. Because the errors are input related and expected to be corrected by the user, exceptions are not a reasonable option. They are too heavy. – BionicCode Oct 02 '22 at 12:22

0 Answers0