|
-
Sep 3rd, 2006, 05:31 AM
#1
Thread Starter
Hyperactive Member
Who can help me with my HomeWork?
I was dong my C# HomeWork when I fell on a little problem.
I can't fix it my self..
Who can help me with my HomeWork?
The Error I'll get with the code under here..
Use of unassigned local variable 'ValidKey'
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Huiswerk
{
class Program
{
static void ShowProfile()
{
Console.WriteLine("Hello World..");
}
static void Say(string Input)
{
Console.WriteLine(Input);
}
static void Main(string[] args)
{
//Declaration of Variables
string ValidKey;
int RandomKey = 1;
string TempStr;
//Start Program
Say("Type the Following Text: ");
switch (RandomKey)
{
case '1':
ValidKey = "qwerty";
break;
case '2':
ValidKey = "hello";
break;
case '3':
ValidKey = "enter";
break;
}
Say(ValidKey);
TempStr = Console.ReadLine();
if (TempStr == ValidKey)
{
Say("Welcome User..\n\t Please Enter..");
//Show Profile
ShowProfile();
}
else
{
Say("Invalid!!");
}
}
}
}
thx already
506C65617365205261746520506F7374732E2E2E
-
Sep 3rd, 2006, 06:03 AM
#2
Re: Who can help me with my HomeWork?
1) RandomKey is of type int, so remove the ''s from the case keyword
Code:
case 1:
ValidKey = "qwerty";
break;
case 2:
ValidKey = "hello";
break;
case 3:
ValidKey = "enter";
break;
since you are using Switch statement to assign to a variable, it is always recommended to use a default or atlest initialise it with an empty string, so the Switch should look something like this:
Code:
//either initialise
//string ValidKey = null;
//or
switch (RandomKey)
{
case 1:
ValidKey = "qwerty";
break;
case 2:
ValidKey = "hello";
break;
case 3:
ValidKey = "enter";
break;
default:
ValidKey = "";
break;
}
hope it helps you.
-
Sep 3rd, 2006, 06:09 AM
#3
Re: Who can help me with my HomeWork?
Long switch constructs are almost always a sign of bad logic. Use an array instead, it's neater as you don't have to repeat so much code.
Code:
string ValidKey = (new string[3] {
"qwerty",
"hello",
"enter"
})[RandomKey];
-
Sep 3rd, 2006, 06:16 AM
#4
Re: Who can help me with my HomeWork?
 Originally Posted by penagate
Long switch constructs are almost always a sign of bad logic. Use an array instead, it's neater as you don't have to repeat so much code.
Code:
string ValidKey = (new string[3] {
"qwerty",
"hello",
"enter"
})[RandomKey];
Never knew about it.
-
Sep 3rd, 2006, 08:41 AM
#5
Re: Who can help me with my HomeWork?
The actual reason that you're getting that error is because it is possible for your code to follow a path that does not assign a value to the ValidKey variable before it's used, which is illegal. Looking at this section of code:
Code:
string ValidKey;
int RandomKey = 1;
string TempStr;
//Start Program
Say("Type the Following Text: ");
switch (RandomKey)
{
case '1':
ValidKey = "qwerty";
break;
case '2':
ValidKey = "hello";
break;
case '3':
ValidKey = "enter";
break;
}
Say(ValidKey);
ignoring the fact that your case expressions are the wrong type, if RandomKey is not 1, 2 or 3 then ValidKey will not be assigned a value when you get to the line that calls Say(). The rules of C# dictate that all variables must have been assigned a value before appearing on the right-hand side of an assignment (read: equals sign) or in a parameter list. To fix this issue you must ensure that no matter what path of execution your code follows it will assign a value to ValidKey by that line. There are two ways that you'd consider doing this. You can either add a 'default' case to your switch statement or else just initialise the ValidKey variable to a value when you declare it. Note that null is a valid value. This is what you'd do if you know for sure that RandomKey will be either 1, 2 or 3.
-
Sep 3rd, 2006, 10:41 AM
#6
Thread Starter
Hyperactive Member
Re: Who can help me with my HomeWork?
 Originally Posted by penagate
Long switch constructs are almost always a sign of bad logic. Use an array instead, it's neater as you don't have to repeat so much code.
Code:
string ValidKey = (new string[3] {
"qwerty",
"hello",
"enter"
})[RandomKey];
I had to Include a Switch so thats why I did it this way...
Thank you all..
506C65617365205261746520506F7374732E2E2E
-
Sep 5th, 2006, 07:04 AM
#7
Fanatic Member
Re: Who can help me with my HomeWork?
You see that alot... Someone needs help with homework, you look at it and say you could do this part here in a "much more compact and robust manner" and they look at you and say but I have to use that - it's part of the assignment. ARGG!! Dang teachers and their required constructs!
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|