I am having trouble getting a method working and I am not sure I am doing it the correct way. What I want to do is to send a string from a form by a button_click into a class and process it there for errors(tryparse method) and the send a boolvalue back to then either report and error to the user or to print input in an listbox.
This is the form code I have that is supposed to send the string into the class.
private void btnOK_Click(object sender, EventArgs e)
{
Errorcheck.GetDouble(numChoice);
}
and then the Errorcheck class:
public static bool GetDouble(string numChoice, out double value, double minLimit, double maxLimit)
{
while (!double.TryParse(numChoice, out value))
{
if ((value >= minLimit) && (value <= maxLimit))
{
return true;
}
}
return false;
}
How do I retrieve the bool value from the Errorcheck class? if it was ok or not. Am I doing the right way or is there a quicker way to go about it?
myresultwill contain the result returned by GetDouble eithertrue(value could be parsed and is within limits) orfalse(value could be parsed but was out of limits or failed to parse in the first place). Also,foowill contain 0 if parsing failed, otherwise the parsed value (which, may or may not!) be within the limits.I do, however, have some problems with your code. First; why is your class named
Errorcheckwhile, in fact, it doesn’t do error-checking (what is that anyway?) but parse a value.Second, why use the while() construct?
Third; GetDouble() doesn’t actually “get” a “double”. It checks if a value is parseable as a double and, if so, is within bounds. It returns a bool for Pete’s sake.
EDIT Scratch that; I missed the
outon the method signature. It returns a bool and also the value.…and then some more but I might be nitpicking 😛
And, last but not least, I don’t see what this has to do with “how bool values and variables get sent between classes/forms”?
Take my, well intended, advice and get a good C# or general book on programming and brush up your basic skills a little. You will only benefit and soon you’ll see why your code is, no flame intended, “bad” in many ways.