Reputation: 75
I am relevantly new to C# coding. I am creating a Asp + C# website for C# learning and fun.I am stuck in a strange error.
What I am doing. Getting username from a textbox, Converting the name to the char arrary, and then trying to join add arrays as per their value described on the switch statement.
but, it is showing me red squiggly lines in all my switch statement, showing me , The Name Does not Exists in the Current Context .
Any clue, how to fix it?
name = TextBoxName.Text.ToString();
var number = 0;
char[] arr = name.ToCharArray();
foreach (var nameChar in arr)
{
switch (nameChar)
{
case A:
number += 1;
case B:
number += 2;
case C:
number += 3;
case D:
number += 4;
case E:
number += 5;
case F:
number += 5;
case G:
number += 7;
case H:
number += 8;
case I:
number += 9;
case J:
number += 10;
case K:
number += 11;
case L:
number += 12;
case M:
number += 13;
case N:
number += 14;
case O:
number += 15;
case P:
number += 16;
case Q:
number += 17;
case R:
number += 18;
case S:
number += 19;
case T:
number += 20;
case U:
number += 21;
case V:
number += 22;
case W:
number += 23;
case X:
number += 24;
case Y:
number += 25;
case Z:
number += 26;
default:
}
};
Upvotes: 1
Views: 3931
Reputation: 75
Thanks, everyone, thanks for your valuable inputs and suggestions.
It is solved, and the issues were.
missing break;
Need to add ''
in A: 'A'
In foreach loop I have mentioned var nameChar
and I have changed it
to char namechar
and need to add .ToUpper
in the textbox.Text
When we learning something new, mistakes do happen :) and thanks for correcting them.
Upvotes: 1
Reputation: 176896
For Switch you can check on msnd over here : switch (C# Reference)
you need to do like this, my mean to say you misssed quote here
case 'A':
and you also missed break statment here so it will be like
case 'A':
number += 1;
break;
case 'B':
number += 2;
break;
Upvotes: 4
Reputation: 13484
If you are using string use "A"
case "A":
number += 1;
break;
or simply
case 'A':
number += 1;
break;
Upvotes: 0
Reputation: 24212
Others have pointed out your error, but please don't use code like that. Try something like this instead:
const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] arr = name.ToUpper().ToCharArray();
foreach (var nameChar in arr)
{
number += letters.IndexOf(nameChar) + 1;
};
Upvotes: 4
Reputation: 1162
You are giving potential variable names not characters as the value is expected so tr 'A' instead of A etc
Upvotes: -2
Reputation: 6366
To use chars in C# you need to use the apostrophies. And to separate different cases between each other you need to you "break" at the end of each case:
case 'A':
number += 1;
break;
Upvotes: 3