Jump to content

I'm writing a class in C# that I have a function for that I want to check if a value is ok. If it's not, then it'll stop the function and give the user an Error message (Messagebox).

 

The class will look something like this (just sample code, not what I'm actually using):

public void SetValue (int a, int b)
{
    if (a==1)
    {
        //error
    }
    else
    {
        //continue with other code
    }
}

How would I go about instead of setting "bad" values, it will return an error message and let the user enter new values to be checked in the function.

This is in WFA/WPF, no consol application.

Thanks

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
https://linustechtips.com/topic/575197-c-class-function-return-error/
Share on other sites

Link to post
Share on other sites

From a design standpoint it depends what kind of validation you're doing here. If it's directly associated with the health of the class and something could break if value is bad, use exceptions (eg. throw an ArgumentException with a your own message). If it's something to do with overall data integrity and validity, this belongs in a separate validator class (SomethingSomethingValidator or so), you don't want setters doing things other than set values because that's not what setters do, let a validator class do validator things, you can make it return error messages depending on the data, and the event handlers in a form should handle the case where data is invalid, not letting the user proceed, showing a message box with a message and such, you know - handler things. All this will make your application easier to test, and I suggest you should really try getting a hang of TDD (Test Driven Development), you will feel when your class starts doing too much.

Link to post
Share on other sites

20 hours ago, madknight3 said:

You can throw an exception and have the calling code handle it with a try/catch


public void SetValue (int a, int b)
{
    if (a==1)
    {
        throw ...;
    }
    
    //continue with other code

}

 

Thanks, this worked just like I wanted it to! Didn't even think of doing something like this. I was more focused on how to make the class itself run a messagebox in the form.

20 hours ago, DevBlox said:

From a design standpoint it depends what kind of validation you're doing here. If it's directly associated with the health of the class and something could break if value is bad, use exceptions (eg. throw an ArgumentException with a your own message). If it's something to do with overall data integrity and validity, this belongs in a separate validator class (SomethingSomethingValidator or so), you don't want setters doing things other than set values because that's not what setters do, let a validator class do validator things, you can make it return error messages depending on the data, and the event handlers in a form should handle the case where data is invalid, not letting the user proceed, showing a message box with a message and such, you know - handler things. All this will make your application easier to test, and I suggest you should really try getting a hang of TDD (Test Driven Development), you will feel when your class starts doing too much.

Thank you, great explanation. I think it'll be fine, the if statement is only going to check whether the integer is between two numbers, if it's not, an error will be thrown. Otherwise it'll use the integers and set them to a pair of other integers in the class. I think it's ok to have the check in the Set function?

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×