-2

I am trying to convert a string I made in a class into a double so I can use it in arithmetic however I am getting errors, I have tried the following:

/* Calling class to use number for conversion
      double LocalCentToInch = Convert.ToDouble(CentI);
      double YourAnswer = MyConvert * LocalCentToInch;
      */
      //Ignore these lines just testing type converison and failing
      //Console.WriteLine(CentI);
      //Console.WriteLine(CentI.ToDouble());
      double LocalCentToInch;
      LocalCentToInch = Convert.ToInt32(CentI);
      Console.WriteLine(LocalCentToInch);

I am calling from a class named convert one and it looks like this:

/////////////////////centimetres to inches///////////////////////////////
public class ConvertOne 
{
  public ConvertOne()
  {
    Centtoinch = "0.3937008";
  }

  public ConvertOne(string centtoinch)
  {                                   //make new string with var data
    Centtoinch = centtoinch;
  }

  public string Centtoinch   { get; }


  public override string ToString()
  {
    return Centtoinch;
  }


}
///////////////////////end of centimetres to inches////////////////////////////

The main method is where the math is going down and it looks like this currently:

  double YourAnswer = MyConvert * 0.3937008;
  Console.WriteLine("\nYour answer is:");
  Console.WriteLine(YourAnswer);

I do not want to use 0.39377008 though, I want to call this string from it's class and convert it into a double so that it can be used in the "YourAnswer" part.

As you can see I have tried different approaches although I'm not sure what I should try next.

The error I'm getting is this:

Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
  at System.Convert.ToInt32 (System.Object value) [0x00003] in <71d8ad678db34313b7f718a414dfcb25>:0 
  at testConverter.Main () [0x000dc] in <93d359b0b3204d6482ac66dd3e4b0858>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Specified cast is not valid.
  at System.Convert.ToInt32 (System.Object value) [0x00003] in <71d8ad678db34313b7f718a414dfcb25>:0 
  at testConverter.Main () [0x000dc] in <93d359b0b3204d6482ac66dd3e4b0858>:0 
exit status 1
J. Rhodes
  • 1
  • 1
  • 1
    It would help if you described the actual problem. I should note, however, that you're converting a string to an Int32 (an int) not a double in your code above. What's wrong with using `Convert.ToDouble()`? – Erik Funkenbusch Dec 11 '18 at 19:39
  • 2
    Can you please post the specific error and the location where it occurs? "I am getting errors" does not tell us much. Also can you please remove the commented-out portion of your code example, and limit it to the code required to demonstrate the problem? It is very confusing as written. Please see [ask] and [mcve]. – John Wu Dec 11 '18 at 19:39
  • 1
    From what I can see, it would be better to make Centtoinch a Double and convert the value to a string when it is needed as a string. – Andrew Morton Dec 11 '18 at 19:43
  • 1
    Why use a string to store the conversion factor, instead of the double it needs to be? – Hans Kesting Dec 11 '18 at 19:50
  • Look into double.TryParse for safe checking of strings before you parse them – Jae Yang Dec 11 '18 at 20:08
  • 1
    also consider culture if it's relevant https://stackoverflow.com/a/5109871/1462295 – BurnsBA Dec 11 '18 at 20:09

1 Answers1

0

this is how to convert a string to a double

var str = "1.23";
var dble = Convert.ToDouble(str);

or

var str = "1.23";
var dble = Double.Parse(str);

in fact Convert.ToDouble, simple calls the second method - see https://referencesource.microsoft.com/#mscorlib/system/convert.cs,d98fcd7819f63d0f

pm100
  • 48,078
  • 23
  • 82
  • 145