Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have to arrays to which I am adding constructor objects and convert the arrays to string arrays.

AFter that when I am trying to use substraction operator on elements of string array.

I am getting some error

C#
Error	1	Operator '-' cannot be applied to operands of type 'string' and 'string'


My code:

C#
String[] myArr1 = (String[])arrList1.ToArray(typeof(string));
            String[] myArr2 = (String[])arrList2.ToArray(typeof(string));
            int index = 0;
            List<int> reds = new List<int>();
            foreach (var c in myArr1)
            {
                if (c == "CANBusRed")
                {
                    reds.Add(index);
                }
                index++;
            }
            List<double> times = new List<double>(); 
         

            foreach (var i in reds)
                if ((i < (index - 1)) && myArr1[i + 1] == "CANBusYellow")
                {
                    arrList3.Add(myArr2[i + 1] - myArr2[i]);
                }
            double av = times.Sum() / times.Count;



Can anyone suggest something about this

Thanks
John
Posted
Comments
Ron Beyer 28-Dec-13 10:03am    
If you have data in your array like CANBusYellow, what do you expect the subtraction to do? Strings are not numerical data, how do you subtract one from the other? What are you really trying to do here?
Member 10408451 28-Dec-13 10:12am    
No, myArr1 is not numerical data. It contains strings
But myArr2 contains numerical data. It contains double values.
ArrayList myArr2= new ArrayList();

I am giving the condition below
if ((i < (index - 1)) && myArr1[i + 1] == "CANBusYellow")

if ((i < (index - 1)) && myArr1[i + 1] == "CANBusYellow")

when it is true I need to subtract the corresponding elements of 2nd array list(myArr2)

I tried without converting 2nd array to string array.
Then I got error like this
Error 1 Operator '-' cannot be applied to operands of type 'object' and 'object'

So, I tried converting to string
Can you suggest something about this???
Ron Beyer 28-Dec-13 10:14am    
myArr2 is defined as an array of strings, if you are storing doubles in there, why did you declare it as a string array? Redefine it as doubles.
CHill60 28-Dec-13 10:18am    
Why are you converting the numerical data to string ... can you not just use arrList2[i + 1] - arrList2[i]); ... or make myArr2 an array of some numerical type

1 solution

I think you need to stop, take a step back and think about what you are doing - because you currently appear to be trying things at random and hoping they will work.

"But myArr2 contains numerical data. It contains double values."
No, it doesn't:
C#
String[] myArr2 = (String[])arrList2.ToArray(typeof(string));

That declares MyArr2 as an array of strings - it doesn't matter that teh values you put into it are the string representation of double values: C# is strongly typed and will not convert them for you. If you say "I want strings" then strings you get, and strings they stay until you explicitly change them to something else - which you can't do with a cast, you need to parse them as a number and convert them that way.

And you can't subtract strings, because it makes no sense: what is teh value of "Hello" minus "Goodbye"?

This may sound like a stupid restriction when you know they are numeric values, but it's actually one of the strengths of C# over weakly typed languages like VB: you are in control and you get what you ask for, not what the compiler thinks you might be trying to get./ Which means that problems like accidentally trying to subtract "Goodbye" from "Hello" always get caught at compile time, not at run time - and are a lot easier to find and fix.

Now, try something a bit different: instead of having two different arrays (or Lists, or - Gawd help us - ArrayLists) and keeping then all in step, why not look at doing it properly, and constructing a class which holds the string "CANBusYellow" and the double value together and have a single List of the class which means that the string and it's value are kept together, so if you need one, you have the other? It's really, really, not difficult to do...
 
Share this answer
 
Comments
BillWoodruff 28-Dec-13 17:02pm    
+5 Indeed !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900