I'm relatively new/inexperienced to c# and I am trying to write a bool method to validate if the user input in a windows form is empty, which returns as true or false, as well as change the errorMessage Variable to have new text if it returns false.
public static bool IsPresent(string value)
{
if (value == "")
{
errorMessage = "all textboxes and combo boxes must be filled";
return false;
}
else
{
errorMessage = "";
return true;
}
}
I get compile time error on errorMessage, saying
"An object reference is required for the nonstatic field, method, or property 'member'".
I declared the errorMessage variable at the top of my file and made it public.
I have tried getting rid of the static aspect of the method, which does fix it, but causes more errors elsewhere in my code.
Do you know how I can go about fixing this?
CodePudding user response:
You can declare public static field and do it like this of course:
static class Helper
{
public static string errorMessage;
public static bool IsPresent(string value)
{
if (value == "")
{
errorMessage = "all textboxes and combo boxes must be filled";
return false;
}
else
{
errorMessage = "";
return true;
}
}
}
But this's got a few problems, one of them is that errorMessage can be now modified by some foreign class, so it should be changed to something else, for example:
private static string errorMessage;
public static string GetMessageCopy()
{
return errorMessage;
}
You can change this to property, which does the same thing, but it's easier to read:
public static string ErrorMessage { get; private set; } // property should always start with a big letter
Now only Helper class can modify ErrorMessage, but everyone can read it.
Next problem is that name of your parameter "value" is a C# keyword, which is used for different things, so please, think of renaming it.
CodePudding user response:
You need to declare variable first inside the static function. If you have declared it outside the method's scope I recommend removing the static keyword so it should look like this:
string errorMessage;
bool IsPresent(string value)
{
if (value == "")
{
errorMessage = "all textboxes and combo boxes must be filled";
return false;
}
else
{
errorMessage = "all textboxes and combo boxes must be filled";
return true;
}
}
CodePudding user response:
You should read up on static. You could start with static (C# Reference).
However, there are solutions that don't require a member variable (what you called at the top of my file) and avoid the static/non-static issue. For example, let's consider this solution:
(bool IsValid, string? ErrorMessage) IsPresent(string? s)
{
if (string.IsNullOrEmpty(s))
{
return(IsValid: false, ErrorMessage: "All textboxes and combo boxes must be filled");
}
else
{
return(IsValid: true, ErrorMessage: null);
}
}
Notes:
Please note I used
string.IsNullOrEmptyrather than== "". You may want to usestring.IsNullOrWhiteSpaceto make the requirement even stronger.You may need to change
(bool IsValid, string? ErrorMessage) IsPresent(string? s)to(bool IsValid, string ErrorMessage) IsPresent(string s)if you're not using a fresh version of .NET.
If we think about a bit more we can notice that IsValid is redundant. Checking if the error is return is enough:
string? CheckValid(string? s)
{
if (string.IsNullOrWhiteSpace(s))
{
return "All textboxes and combo boxes must be filled";
}
//if ( ... some other requirement )
//{
// return "Some error";
//}
return null;
}
