Results 1 to 7 of 7

Thread: Who can help me with my HomeWork?

  1. #1

    Thread Starter
    Hyperactive Member Iron Skull's Avatar
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    325

    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

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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.
    Show Appreciation. Rate Posts.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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];

  4. #4
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Who can help me with my HomeWork?

    Quote 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.
    Show Appreciation. Rate Posts.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member Iron Skull's Avatar
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    325

    Re: Who can help me with my HomeWork?

    Quote 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

  7. #7
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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
  •  



Click Here to Expand Forum to Full Width