3

We are getting an error when trying to run this code ideally changes e.Message from string to a double or float array. Please help us understand what the error means, or how to debug the code.

We also tired a float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat); method, which also resulted in an error.

private void ClientReceiveData(object sender, ConnectedClient.NetDataEventArgs e)
{
    if (string.IsNullOrEmpty(e.Message) == false)
    {
        if (e.ID == 0)
        {
            //float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat);
            Array.ConvertAll(e.Message.Split(','), Double.Parse);
            Trace.WriteLine(String.Join(Environment.NewLine, e.Message));
        }

        if (e.ID == 1)
        {
            //float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat);
            Array.ConvertAll(e.Message.Split(','), Double.Parse);
            Trace.WriteLine(String.Join(Environment.NewLine, e.Message));
        }
    }

INPUT (e.Message):

0.0160163, -0.3207418, -0.2747217, 0.9856872, -0.3428816, 0.02380935, 0.8472792, -0.3562718, 0.3080477, 0.6931204, -0.4135765, 0.2335226, 0.6617407, -0.2861188, 0.278595, 0.685502, -0.3337714, 0.06315409, 0.5661756, -0.5338272, 0.1274863, 0.6014508, -0.6020046, 0.149146, 0.6165056, -0.2287867, 0.108688, 0.753313, -0.2801398, 0.1555204, 0.5711947, -0.4046459, 0.3197014, 0.6477473, -0.4134837, 0.3147845, 0.6426268, -0.3393434, -0.242843, 0.9667573, -0.3945386, -0.2511228, 0.5462032, -0.4459318, -0.2613428, 0.2010812, -0.43355, -0.2804166, 0.09203719, -0.2818186, -0.2890762, 0.9426621, -0.2850094, -0.2625419, 0.5789056, -0.2990395, -0.2414854, 0.1805503, -0.2872439, -0.2554213, 0.06843466, -0.3543724, 0.2399589, 0.7335942, -0.5984071, 0.1476627, 0.6094998, -0.5848852, 0.1458522, 0.6007382, -0.4560334, 0.3629844, 0.6141819, -0.4130909, 0.2523528, 0.57725,

error

9
  • We need a minimal reproducible example included in your question. Include your actual input, not a screenshot from your IDE Commented Aug 24, 2018 at 16:58
  • 3
    Paste the entire contents of e.Message as plain text. There is something in there it doesn't like, and we can't see the entire thing. Commented Aug 24, 2018 at 17:01
  • 5
    BTW, ConvertAll returns a new array so if you don't assign the output to anything it won't be very useful. Commented Aug 24, 2018 at 17:05
  • 4
    You have a trailing comma, which is resulting in a blank entry from split. Use the RemoveEmptyEntries option. This is a good example of why we discourage screenshots of error messages. Commented Aug 24, 2018 at 17:09
  • 1
    @JayTailor The trailing comment is likely the problem, add the option StringSplitOptions.RemoveEmptyEntries Commented Aug 24, 2018 at 17:09

2 Answers 2

10

The last entry is an empty string which will not parse with Double.Parse.

You may use:

var parsed = Array.ConvertAll(
    e.Message.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries),
    Double.Parse);

Also note that Double.Parse depends on the current CultureInfo for the interpretation of periods . in the strings.

Sign up to request clarification or add additional context in comments.

1 Comment

That depends. If the event handling method needs to run twice before you can find the "vector sum" final, you may have double[] answer; and double[] result; as fields (i.e. variable on the class-level, outside methods). Then you can calculate final from that after both fields have been assigned.
1

You need to be able to catch the offending value, and should ignore "empty" entries like the one created by the trailing comma:

private void ClientReceiveData(object sender, ConnectedClient.NetDataEventArgs e)
{
    double[] result = null; 
    if (string.IsNullOrEmpty(e.Message) == false)
    {
        if (e.ID == 0)
        {
            //float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat);
            result = Array.ConvertAll(e.Message.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries), ParseWithError);
            Trace.WriteLine(String.Join(Environment.NewLine, e.Message));
        }

        if (e.ID == 1)
        {
            //float.Parse(e.Message, CultureInfo.InvariantCulture.NumberFormat);
            result = Array.ConvertAll(e.Message.Split( new [] {','}, StringSplitOptions.RemoveEmptyEntries), ParseWithError);
            Trace.WriteLine(String.Join(Environment.NewLine, e.Message));
        }
    }
}

double ParseWithError(string value) 
{
     double result;
     if (!double.TryParse(value, out result))
     {
         throw new ApplicationException("Error parsing value " + value,e);
     }
     return result;
}

1 Comment

More nice in my opinion: double ParseWithError(string value) { if (double.TryParse(value, out var dbl)) { return dbl; } throw new ApplicationException("Error parsing value " + value); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.