Hey folks.

I've just took up a course in C# and so this is part of a homework. I've tried various attempts but to no avail and so I thought I'd ask on here.

I have been asked to write a class to verify if a valid double has been entered in a textbox on a form. Once the string is entered in the main class, another class will be called to convert the given string into a double and if conversion, validate that the converted value is within a given range. I have to write various methods with different parameters (overload). Here is my attempt below:

namespace Assignment_3
{
    public class InputUtility
    {
        public static bool GetDouble(string stringToConvert, out double dblOutValue, double minLimit, double maxLimit)
        {
            if (double.TryParse(stringToConvert, out dblOutValue))
            {
                if ((dblOutValue >= minLimit) && (dblOutValue <= maxLimit))
                {
                    return true;
                }
            }
            return false;
        }


        public static bool GetDouble(string stringToConvert, out double dblOutValue, double minLimit)
        {
            if (double.TryParse(stringToConvert, out dblOutValue))
            {
                if ((dblOutValue >= minLimit))
                {
                    return true;
                }
            }
            return false;
        }

        public static bool GetDouble(string stringToConvert, out double dblOutValue)
        {
            if (double.TryParse(stringToConvert, out dblOutValue))
            {
                return true;                
            }
            return false;
        }

In my main form, I have a textbox called numText. Now when a user enters a number into this, I need it to initially be checked against the first and if parameters not applicable, the second method etc in the above namespace and class. If a valid input entry (ie. a double), I want the number to populate the numText.text field however if invalid, an error message to pop up.

I am happy with writing the error message etc but I'm just unsure how to cross reference the double entered by the user to another classname and the overloaded methods as above.

The class that the numText entry will be in is:-

private bool ReadAndValidatePrice(out double price)

Any suggestions?

Since they are static methods you just use the class name followed by the method:

InputUtility.GetDouble(...)

Since they are static methods you just use the class name followed by the method:

InputUtility.GetDouble(...)

Thanks Momerath.

How do I actually introduce the inputted string to the class though? I assume I have to assign a variable to my user input

eg: string numChoice = txtNumTxt.text

but how do I then assign that variable to be processed by the InputUtility.GetDouble method? In the InputUtility.GetDouble method, the variable number to be processed would appear to be assigned to the variable, stringToConvert. How do I get my user entered number in the different class to be processed as this (if that makes sense?)

The way I am thinking is that the user enters the number, it then gets assigned to a string variable in the main namespace and ReadAndValidatePrice(out double price) class and then from there, it is then processed via the GetDouble method in the InputUtility method class. If the string can be parsed, how does the GetDouble method then return the double value though which in turn is then passed to the variable, 'price' as outlined in my ReadAndValidatePrice(out double price) method. I assume I do no parsing at all in the ReadAndValidatePrice(out double price) method? I've tried a million and one different ways of passing this code but always seem to generate an error.

Thanks in advance again :)

Try this:

InputUtility.GetDouble(numChoice);   //Pass your text into the method

It's simply this.... CLASSNAME.METHODNAME() ...any parameters go in the paranthesis and are seperated by commas.

http://msdn.microsoft.com/en-us/library/f02979c7.aspx On the TryParse method...

http://www.java2s.com/Code/CSharp/Language-Basics/Illustratestheuseofoutparameters.htm On the use of "out" in method parameters

*Note: this confused me a good deal, never seen the out parameter before. Its confusing since it seems like it needs more than one parameter. Just remember that "out" means the method is only returning values, not taking them in.

Let me know how it goes! First time I have ever given advice, usually just on the receiving end.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.