Neel
Neel

Reputation: 21

How can I fix these errors in C#?

Well this is the code I'm having errors with:

this.terminchar = Convert.ToChar(8080);    
List<string> source = new List<string>(this.base64Decode(parse_value).Split(new char[] { this.terminchar }));

if ((source) % 2) == 0)
{
  for (int i = 0; i < source; i++)
  {
    this.keys.Add(source[i], source[++i]);
  }
}

I get 3 errors with this code, first one:

Error 1 Operator '<' cannot be applied to operands of type 'int' and 'System.Collections.Generic.List'

Second one:

Error 2 Operator '%' cannot be applied to operands of type 'System.Collections.Generic.List' and 'int'

Third one:

Error 3 Invalid expression term '=='

I'm fairly new to C# and this is my friends source code which I'm just looking at to understand the syntax but I have no idea what to do. Any help given would be appreciated, thanks.

Upvotes: 0

Views: 1003

Answers (5)

WaltiD
WaltiD

Reputation: 1952

You are doing some operations on a list. I'm quite sure, you should your lines as follows...

if ((source.Count) % 2) == 0)  

and

for (int i = 0; i < source.Count; i++)

instead

Upvotes: 3

obviously, you can use there in for loop. use i<source.Count and also (source.Count) % 2 instead

Upvotes: 1

Nathan Q
Nathan Q

Reputation: 1902

You need to use the .Count on source to get the count of items in the List

List<string> source = new List<string>(this.base64Decode(parse_value).Split(new    char[] { Convert.ToChar(8080) }));
string Command = source[0];
source.RemoveAt(0);
if ((source.Count) % 2) == 0)
{
    for (int i = 0; i < source.Count; i++)
    {
        this.keys.Add(source[i], source[++i]);
    }
}

Upvotes: 0

abatishchev
abatishchev

Reputation: 100268

source is List<string> - a container for strings, see MSDN.

Operators < and % can be applied to int. So your code is missing something.

Upvotes: 0

leppie
leppie

Reputation: 117240

You probably looking for the .Count property in both cases.

So use source.Count.

Upvotes: 6

Related Questions