Home > front end >  Getting error "cannot convert from 'double' to 'System.ReadOnlySpan<char>&
Getting error "cannot convert from 'double' to 'System.ReadOnlySpan<char>&

Time:10-06

Hello I am new to C# and I am trying to store the value of a variable(total) into another variable(storetotal) but I am getting the following error: cannot convert from 'double' to 'System.ReadOnlySpan' where I have the following code

storetotal = int.Parse(total);

Here's the full method

public void GetCValue()
        {
            List<IWebElement> GetTotal= new List<IWebElement>(driver.FindElements(By.XPath("//div[@id='lbtW']//tbody/tr")));

            double CValue_Total;
            double total = 0;            
            double getFinalAmt;
 
            int i;
            for (i = 1; i < GetTotal.Count; i  )
            {
                double storetotal;
                
                var HighlightPath = driver.FindElement(By.XPath("//table[@id='PData']//tbody/tr["   i   "]/td[5]/input['"  i "']"));
                MyFunc.HighlightElement(driver, HighlightPath);

                var getval = driver.FindElement(By.XPath("//table[@id='PData']//tbody/tr["  i   "]/td[5]/input['"   i   "']")).GetAttribute("value");       
        
                double Value = double.Parse(getval);

                CValue_Total = total   Value;

                

                if (i < GetTotal.Count)
                {
                    storetotal = int.Parse(total); // getting error
                    getFinalAmt = storetotal   CValue_Total;
                }

            }

            
        }

Please help

CodePudding user response:

Your variable total is of type double. You can not parse doubles. You parse strings. Why do you try to convert your double into type int and then assign it to your double storetotal?

CodePudding user response:

int.Parse only has overloads for taking either a string or System.ReadOnlySpan. It can't handle a double as input.

Try:

storetotal = Convert.ToInt32(total)

Remember though that int has a smaller than the range of double. Convert.ToInt32(double) will throw an exception if it is out of range, which you might want to inside a try/catch.

CodePudding user response:

As total is double, then int.Parse(total) will select a nearest overload

public static int Parse (ReadOnlySpan<char> s, 
    System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer,
    IFormatProvider? provider = default);

(see documentation here).

But double can not be implicitly casted to ReadOnlySpan, so you got the error menioned.

If you just need to store your total somewhere, you can just assign storetotal=total as both are of the same double type

  • Related