-1

The line of code with the issue:

memory.WriteFloat(tp_x, this.X)

this.X is fine and works with no issues. For the value tp_x, it is a STRING. I am taking this from a text file and parsing it to an exact value. I have tried several times to convert this string to Int32/64, float, long, etc. But the memory.WriteFloat() is not taking it without an error. Picture of code

I have tried to use

memory.ReadFloat(tp_X)

But I get the same error. Cannot convert from 'string' to 'long'. I have tried so much different codes to convert it and use it, but nothing has worked or changed so I am asking a question here. I wouldn't be asking if I had no idea. Thank you, please let me know!

EDITED for pm100 Here is my new code: Picture 1 Where I get my error Picture 2 So using your code @pm100 as seen in the pictures, I get no problems and I can compile perfectly. Although, when I execute this I get error

I get the error System.FormatException: Input string was not in a correct format. at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type) at System.Number.ParseInt64(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info) at System.Int64.Parse(String s)

My code: int counter = 0; // location is the selected index of the combobox that the user selected

foreach (string line in System.IO.File.ReadLines(@"path"))
                {
                    if (counter == location)
                    {
                        string[] words = line.Split(",");
                        int tempcounter = 1;
                        string tp_x = "";
                        string tp_y = "";
                        string tp_z = "";
                        foreach (var word in words)
                        {
                            if (tempcounter == 1)
                            {
                                tp_x += word;
                            }
                            if (tempcounter == 2)
                            {
                                tp_y += word;
                            }
                            if (tempcounter == 3)
                            {
                                tp_z += word;
                            }
                            tempcounter += 1;
 //everytime works perfect above, now below 
                            long l = System.Int64.Parse(tp_x);
                            long l2 = System.Int64.Parse(tp_y);
                            long l3 = System.Int64.Parse(tp_z);
                            tp_xx = l;
                            tp_yy = l2;
                            tp_zz = l3;


                            for (int i = 0; i < address.vz.Length; i++)
                            {
                                mem.WriteFloat(tp_xx, this.X);
                                mem.WriteFloat(tp_yy, this.Y);
                                mem.WriteFloat(tp_zz, this.Z);
                            }
                            
                            
                        }
                        return false;
                    }
                    counter += 1;
                }
  • 2
    “tp_x, it is a `string`”; so convert it to a `long`, or as a search phrase “C# how to convert string to long”. – user2864740 Jan 29 '22 at 22:48
  • See https://stackoverflow.com/questions/6330306 , https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number , https://blog.elmah.io/csharp-how-to-convert-a-string-to-int/ etc. – user2864740 Jan 29 '22 at 22:50
  • Perhaps tp_x should be a `long` already (ie. `long tp_x;`), with any conversions occurring before it is assigned. Then the value of tp_x would not need to be converted as the usage sites. – user2864740 Jan 29 '22 at 22:53
  • @user2864740 Tried that several times. I tried so many different things online to convert etc. No luck! :) – newcsharpcoder Jan 30 '22 at 01:06
  • @newcaharpercoder Then the trying was incorrectly applied. It’s not practical to attempt to provide more input, given a lack of a clear minimal reproduction example. See https://stackoverflow.com/help/minimal-reproducible-example – user2864740 Jan 30 '22 at 01:12
  • @user2864740 I did mention at the bottom. :) – newcsharpcoder Jan 30 '22 at 01:19
  • And also, long tp_x; won't work etc. As I am reading from a text file, so I will have to use string and when I do long tp_x; I will get cannot convert str to long error. – newcsharpcoder Jan 30 '22 at 01:20
  • `long tp_x` *will* work, when following through with the *full* suggestion of “with any conversions occurring before it is assigned”. So, when reading in text as a string, **convert the read `string` to a long, and assign that value to the `long tp_x` variable**. Then `tp_x` (now a `long`) can be used as shown, as it is already the correct type. See https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number for methods of conversion. In the MSDN code, `string input` and `string stringInput`, eg, represents the text value read. – user2864740 Jan 30 '22 at 01:30
  • Copy the console texts and paste here, not in images. See [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/995714) – phuclv Jan 30 '22 at 12:03
  • @newcsharpcoder you didn't show your new code **in texts**. Never put code in comments, no one will read that – phuclv Jan 30 '22 at 12:11
  • @phuclv Apologies. I edited my message with pastebin if that's ok. – newcsharpcoder Jan 30 '22 at 12:28
  • @newcsharpcoder no that's not allowed because external links can be rotten at any time, rendering the question invalid. Please see [ask], [Questions linking to external web sites instead of showing code](https://meta.stackexchange.com/q/80978/230282). Also read [tour] to see how this site works. It's not a Q&A website as one might think. And as I said, **never post code in comments**. Edit everything inside the question itself. You need to create a [mcve] as user2864740 said – phuclv Jan 30 '22 at 12:33
  • Sorry. I added to message – newcsharpcoder Jan 30 '22 at 12:38
  • this message is saying that the string doesnt contain a number. Pause in the debugger and examine the value you are passing to the parse function – pm100 Jan 30 '22 at 18:10

1 Answers1

1

You need Int64.Parse

     string s="1234";
    long l = System.Int64.Parse(s);

Or if you are not sure that the string contains a valid number use

bool success = Int64.TryParse(s, out long l);
pm100
  • 48,078
  • 23
  • 82
  • 145
  • 1
    Or just `long.Parse`/`long.TryParse`. Whichever you prefer, really. – Etienne de Martel Jan 29 '22 at 22:48
  • @pm100 I got the error ************** Exception Text ************** System.FormatException: Input string was not in a correct format. at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type) at System.Number.ParseInt64(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info) at System.Int64.Parse(String s) – newcsharpcoder Jan 30 '22 at 01:16
  • @pm100 Using the code bool success = Int64.TryParse(s, out long l); works iwith no errors. But doesn't update the value for when I change memory.WriteFloat – newcsharpcoder Jan 30 '22 at 01:18
  • post the code that you have thats calling tryparse (edit the question) – pm100 Jan 30 '22 at 01:25
  • @newcsharpcoder Reading [the documentation](https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryparse) can save a lot of time.. while TryParse did not throw an exception, it did not “work” to convert the non-parseable string value. The result will be `false` and the `out` value will be set to 0 if the conversion fails: “The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents a number less than MinValue or greater than MaxValue.” – user2864740 Jan 30 '22 at 07:06
  • @user2864740 Tried to use that. But got the error Argument 1: cannot convert from 'void' to 'long'. Code: mem.WriteFloat(TryToParse("25.3"), this.X); Also. I read the docs – newcsharpcoder Jan 30 '22 at 11:54
  • @pm100 Sorry, now I edited my message with your code and the error etc. Thank you – newcsharpcoder Jan 30 '22 at 12:07