I'm new to programming and having a problem with the following code:
private string alphaCoords(Int32 x)
{
char alphaChar;
switch (x)
{
case 0: alphaChar = 'A'; break;
case 1: alphaChar = 'B'; break;
case 2: alphaChar = 'C'; break;
case 3: alphaChar = 'D'; break;
case 4: alphaChar = 'E'; break;
case 5: alphaChar = 'F'; break;
case 6: alphaChar = 'G'; break;
case 7: alphaChar = 'H'; break;
case 8: alphaChar = 'I'; break;
case 9: alphaChar = 'J'; break;
}
return alphaChar.ToString();
}
The compiler says: Use of unassigned local variable 'alphaChar'
But I'm assigning it in my switch block.
I'm sure this is my fault as I dont know enough about programming.
Please advise.
Thanks.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
You're assigning it if x is 0-9. What would you expect it to do if x
were 123 though? While you may know that only values between 0 and 9 will be passed in, the compiler doesn't - so it needs to consider what would happen otherwise.
One way to avoid this is to have a default
case in your switch statement, which you can use to throw an exception if the value isn't in the expected range:
switch (x)
{
case 0: alphaChar = 'A'; break;
case 1: alphaChar = 'B'; break;
case 2: alphaChar = 'C'; break;
case 3: alphaChar = 'D'; break;
case 4: alphaChar = 'E'; break;
case 5: alphaChar = 'F'; break;
case 6: alphaChar = 'G'; break;
case 7: alphaChar = 'H'; break;
case 8: alphaChar = 'I'; break;
case 9: alphaChar = 'J'; break;
default: throw new ArgumentOutOfRangeException();
}
Here's a slightly simpler alternative though, which removes your switch statement completely:
if (x < 0 || x > 9)
{
throw new ArgumentOutOfRangeException();
}
char alphaChar = (char)('A' + x);
Note that you do need to exercise care when using arithmetic like this. In Java and C# the underlying representation is guaranteed to be Unicode, which makes life a lot easier. I believe it's fine for things like this (and hex parsing/formatting) but when you venture into more exotic scenarios it would fail. Then again, that's true for a lot of code simplification techniques... if they're applied inappropriately, you end up with a mess.
The compiler is complaining because alphaChar is possibly undefined -- if it is not one of the values in your switch
then it will not have been defined. You can do one of the following things:
Before its first use local variable must be definitely assigned (according to C# specification rules). In this particular case switch construct doesn't guarantee that alphaChar will be definitely assigned thus compiler error. You can provide initial value to alphaChar and thus it will be definitely assigned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With