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

I have 3 list strings.

2 list strings contains double values.

I want to subtract 2 list string objects and lace them into new list string

C#
for (int c = 0; c < yellowTimeStampRYcmnMsgIds.Count; c++)
                {
                    gatewaytimeRYcmnMsgIds[c] = (yellowTimeStampRYcmnMsgIds[c] - redTimeStampRYcmnMsgIds[c]);
                   
                } 


I am getting index out of rage exceptionwhile trying like this

Thanks
John
Posted
Comments
bowlturner 9-Jan-14 9:07am    
I was wondering why you need 3 lists to kept in sync? Why not make an object that holds the three values and put that in a list?

"I have 3 list strings. 2 list strings contains double values."

If your lists are lists of strings where each string is a representation of a double value: you must convert the strings to doubles first, then do the subtraction, and then convert the result to a string, and then assign that to your new List of strings !

"Subtracting" a string from a string makes no sense (and wouldn't compile in the first place) ... in C#, anyhow.

Here's a code example that takes two Lists of strings, where each item in each list is assumed to be a string representation of a double, subtracts the items in one list from the other list, takes the resulting double and converts into a string, and stores it in the list it returns.

Note this example tests to make sure that the for-loop attempts to compare the two lists using the shorter of the two lists ... if the two lists are not of the same length.

It also checks each item in each list to make sure they are doubles:
XML
private List<string> subtractStringDouble(List<string> list1, List<string> list2)
{
    List<string> list3 = new List<string>();

    double d1, d2;

    // use the shorter of the two lists
    // you'll note that you can evaluate inside
    // a for-loop declaration
    for (int i = 0; i < ((list1.Count < list2.Count) ? list1.Count : list2.Count); i++)
    {
        if(double.TryParse(list1[i], out d1))
        {
            if(double.TryParse(list2[i], out d2))
            {
                list3.Add((d1 - d2).ToString(format:"F"));
            }
        }
    }

    return list3;
}
Here's a test:
XML
List<string> test1 = new List<string>{ "5.33", "59.123", "1.9"};
List<string> test2 = new List<string> { "2.33", "56.123", "-1.1" };

List<string> test3 = subtractStringDouble(test1, test2);
Each of the three items in 'list3 should be: "3.00"
 
Share this answer
 
Comments
bowlturner 8-Jan-14 11:42am    
+5 I felt too lazy to go into all that. On top of that I was wondering why he needs 3 lists that he 'keeps' in sync? Why not make an object that holds the three values and put that in the list?...
BillWoodruff 9-Jan-14 2:40am    
I'm still working on my own laziness at age 70; if I find a cure, I'll let you know :)

"On top of that I was wondering why he needs 3 lists that he 'keeps' in sync? Why not make an object that holds the three values and put that in the list?..."

Good questions: why not ask the OP ?
bowlturner 9-Jan-14 9:06am    
I will! ;)
Probably, you have less elements in your redTimeStampRYcmnMsgIds list than in your yellowTimeStampRYcmnMsgIds list, so you should check whether you can take the element from redTimeStampRYcmnMsgIds at the specific position:
C#
for (int c = 0; c < yellowTimeStampRYcmnMsgIds.Count; c++)
{
    if (redTimeStampRYcmnMsgIds.Count >= c) // use Length instead of Count if redTimeStampRYcmnMsgIds is an array and not a List
    {
      gatewaytimeRYcmnMsgIds[c] = (yellowTimeStampRYcmnMsgIds[c] - redTimeStampRYcmnMsgIds[c]);
    }
    else
    {
       // there are no elements in the redTimeStampRYcmnMsgIds list anymore
    }    
} 
 
Share this answer
 
v2
Make sure gatewaytimeRYcmnMsgIds and redTimeStampRYcmnMsgIds are the same size of elements as yellowTimeStampRYcmnMsgIds.
 
Share this answer
 
These are Lists, not Arrays. This is closer to what you want. gatewattimeRYcmmMsgIds is 0 length. you need to add each double.


C#
for (int c = 0; c < yellowTimeStampRYcmnMsgIds.Count; c++)
                {
                    gatewaytimeRYcmnMsgIds.Add(yellowTimeStampRYcmnMsgIds[c] - redTimeStampRYcmnMsgIds[c]);
                   
                }
 
Share this answer
 
Comments
bowlturner 8-Jan-14 13:21pm    
I find it interesting when the actual answer to question is down-voted. Is it just so someone else's 'solution' will float to the top? This is the 2nd or 3rd time this week it's happened. Why would this deserve a 2?
Thomas Daniels 8-Jan-14 13:35pm    
Downvoting a accepted solution won't make sure that a non-accepted solution will float to the top, accepted answers are always on the top. I don't know why your answer deserves a 2, it solves the actual problem, so you got my 5.
bowlturner 8-Jan-14 14:53pm    
Thanks. I do wonder what the point is other than being a jerk.

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