Hello,

I am trying to use a TryParse as a validation for a float in a windows form temperature converter web app. I am getting the error No overload for method 'TryParse' takes 1 arguments.
The error shows here (float.TryParse(txtInitialTemp.Text)) and I am guessing this means that I am missing something?
Could someone please explain how to use TryParse with a float?

Thank you:).

public float inputMethod() // this is the method for calculating the conversion OBTAIN initialTemp and OBTAIN Conversion type
        {
            float temp;
            if (float.TryParse(txtInitialTemp.Text))// get initial temp (converts string representation of number to float)
            {
               return temp;
            }
            else //if something other than a number entered
            {
            txtInitialTemp.Clear();
            MessageBox.Show("Please enter a number in the account number section ");
            }
        }

try

float.TryParse(txtInitialTemp.Text, out temp)

try

float.TryParse(txtInitialTemp.Text, out temp)

Thanks gerard but if I try that I get 3 errors. Two want brackets around the conditional statement, and the other points to the name of the method (inputMethod) and says 'TemperatureConvertorPractice.Form1.inputMethod()': not all code paths return a value.

If I try brackets like this

public float inputMethod() // this is the method for calculating the conversion OBTAIN initialTemp and OBTAIN Conversion type
        {
            float temp;

            if (float.TryParse(txtInitialTemp.Text, out temp))
            {
               return temp;
            }
            else //if something other than a number entered
            {
            txtInitialTemp.Clear();
            MessageBox.Show("Please enter a number in the account number section ");
            }
        }

I only get one error under the method name again 'TemperatureConvertorPractice.Form1.inputMethod()': not all code paths return a value.:-/

Just tried this and it works..

using System;

namespace testit
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			Console.WriteLine( inputMethod() );
		}
		
		public static float inputMethod()
        {
            float temp;

            if (float.TryParse("1234.45".ToString(), out temp))
            {
               return temp;
            }
            else 
            {
            	return 0.0f;
           	}
        }
	}
}

I only get one error under the method name again 'TemperatureConvertorPractice.Form1.inputMethod()': not all code paths return a value.:-/

This would be an error in your inputMethod() code, not in the code you've posted. What it is telling you is that it is possible for the code to reach the end of the method and not have encountered a return statement.

your inputMethod()expects a return, and the return statement looses its scope outside the if else condition. so there should be a method level scope that you should specify.

public static float inputMethod()
            {
                float temp = 0.0f;
 
                if (float.TryParse("1234.45".ToString(), out temp))
                {
                    return temp;
                }
                return temp;
            }

Just tried this and it works..

Hi - thank you all again for the help.

I tried your suggestions and it does work, but it returns the zero value into the text box that should show the temperature conversion after it has shown my message box saying that a number should be entered. This is misleading because it looks like the conversion result is zero. Also, if I select my Fahrenheit to Celcius or Celcius to Fahrenheit radio buttons I get a number in the result text box ... which is even more misleading as it looks like it has been converted.

So, I am at the stage where I am thinking the answer lies in Momeraths question

"What it is telling you is that it is possible for the code to reach the end of the method and not have encountered a return statement".

My answer to that is nothing so far! I am saying that I am returning temp in the method, so I am guessing that I want an alternative, but when I experiment with bool or null I am not getting very far. For example, if I try

else if (temp == null)//if something other than a number entered
            {
            txtInitialTemp.Clear();
            MessageBox.Show("Please enter a number in the account number section ")     

            }

I am back to the 'not all code paths return a value' error because ... as it says they don't all return a value. I have been told and read that TryParse is a better validation in this case but cannot seem to get it to work!:-/

@john :
its working for me, the out temp will have the value 1234.45 after the tryparse. and it returns temp which has the value to which it is being converted, may be there is a problm else where, why dnt you debug and come out with the problm..

@john :
its working for me, the out temp will have the value 1234.45 after the tryparse. and it returns temp which has the value to which it is being converted, may be there is a problm else where, why dnt you debug and come out with the problm..

Hi ... I'm still a beginner ... so it is very possible I am missing something basic. I have tried using the code provided and it does not validate my web application and for the reasons explained above, for example outputting a zero to a text box when the user asks for a temperature conversion, I think it may confuse the user as they could mistake this as the answer! Also it does not provide the correct conversion when placed in my code.

If I debug my code I do not get a problem .. it works ... so I cannot 'come out with the problem'. All I am trying to do is validate it using a float.TryParse as I was told this was the best method. Perhaps it is not possible, but I think it probably is? Everything works with my code below - but I return a zero in the text box when validation occurs, and it shouldn't be returning anything ... hope this makes my above response clearer.

I am trying to follows Momeraths suggetsion of writng the method so it allows me to return nothing ... or not to have a return statement here.

Thanks again ... John.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            float inputTemp = inputMethod(); //CALL Input_function  
            float calcTemp = calculateMethod(inputTemp); //CALL Calc_function / newTemp becomes calcTemp (the answer)
            displayMethod(calcTemp); // CALL Display-fuction and display answer
        }


    
        public float inputMethod() // this is the method for calculating the conversion OBTAIN initialTemp and OBTAIN Conversion type
        {
            float temp;

            if (float.TryParse(txtInitialTemp.Text, out temp))  // get initial temp (converts string representation of number to float)
            {
               return temp;
            }
            else 
//if something other than a number entered
            {
            txtInitialTemp.Clear();
            MessageBox.Show("Please enter a number in the account number section ");
            return 0.0f;
          

            }
        }

   

        public float calculateMethod(float workingTemp)// (working temp takes value of inputTemp)this is the method for calculating the conversion
        {
            try
            {//open try
                if (rbtnFtoC.Checked == true) //if you are converting from F to C
                {
                    float newTemp = (workingTemp - 32) * 5 / 9;//Do Calculation- formula for F to C       
                    return newTemp; //Return Result     Working temp comes out as newTemp variable            
                }

                else if (rbtnCtoF.Checked == true)//if you are converting from C to F
                {
                    float newTemp = (workingTemp * 9 / 5) + 32;//Do Calculation- formula for C to F 
                    return newTemp;//Return Result               
                }
            }//close try
            catch (Exception e)
            {
                MessageBox.Show("Please enter a number because " + e.Message);//Mesage Box if letter entered
                txtAnswerTemp.Clear();//clears Celsius text box if you enter a letter
                txtInitialTemp.Clear();//clears Fahrenheit text box if you enter a letter
            }

                return 0;           
        }
            

        public void displayMethod(float answerTemp)// sending the variable calcTemp to the display which is received as answer temp 
        {
            txtAnswerTemp.Text = answerTemp.ToString();
           //no return  because it is just being displayed
        }

        
      }

    }

This would be an error in your inputMethod() code, not in the code you've posted. What it is telling you is that it is possible for the code to reach the end of the method and not have encountered a return statement.

 public float inputMethod() 
       {           
        float temp;             

         if (float.TryParse(txtInitialTemp.Text, out temp))  
         {               
           return temp;        
         }           

         else //if something other than a number entered 
         {          
          txtInitialTemp.Clear();          
          MessageBox.Show("Please enter a number in the account number section ");            
          return 0.0f;   //CAN I JUST REPLACE THIS ???         
         }     
   }

Hello ... sorry the code brackets and colours are not working ... could you tell me if I am somewhere near with this or heading in totally the wrong direction?

Sure you can - if its in any help to use 0.0 (maybe for some comparison), but better like this:

private void yourMethod()
{
    float = inputMethod(textBox1.Text);
    if(floaf == 0.0)
    {
         MessageBox.Show("Please enter a number in the account number section "); 
    }
}
public float inputMethod() 
{ 
    float temp; 
    if (!float.TryParse(txtInitialTemp.Text, out temp))    
       return 0.0F;
    else
       return temp;
}

Sure you can - if its in any help to use 0.0 (maybe for some comparison), but better like this:

private void yourMethod()
{
    float = inputMethod(textBox1.Text);
    if(floaf == 0.0)
    {
         MessageBox.Show("Please enter a number in the account number section "); 
    }
}
public float inputMethod() 
{ 
    float temp; 
    if (!float.TryParse(txtInitialTemp.Text, out temp))    
       return 0.0F;
    else
       return temp;
}

Thanks to everyone for the help - it really is appreciated.

For some reason - if I have the Fahrenheit to Centigrade radio button selected and enter a string the message box appears and when I click OK I get -17.77778 in the result text box

if I have the Centigrade to Fahrenheit radio button selected and enter a string the message box appears and when I click OK I get 32 in the result text box

So, it has to be something to do with 0.0F - but I don't know why

If I try and bypass this by clearing both text boxes (input and output) as below, it says that this is unreachable code

public float inputMethod() // this is the method for calculating the conversion OBTAIN initialTemp and OBTAIN Conversion type
        {
            float temp;
            if (!float.TryParse(txtInitialTemp.Text, out temp))
            {
                MessageBox.Show("Please enter a number in the account number section ");
                txtInitialTemp.Text ="";
                txtAnswerTemp.Clear();
                
                return 0.0F;
            }

            else
            {
                return temp;
            }
}

you can do this, no problem with the code, and you dont have to clear the text box, just display an error message, let the user know why the error is occuring...

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.