Results 1 to 4 of 4

Thread: Use of unassigned local variable <resolved>

  1. #1

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319

    Question Use of unassigned local variable <resolved>

    This is driving me insane. I'm sure the mistake is rediculously simple to see but I can't figure it out. Maybe you guys can see what I'm doing wrong...

    The error:
    Code:
    error CS0165: Use of unassigned local variable 'carType'
    error CS0165: Use of unassigned local variable 'carMake'
    error CS0165: Use of unassigned local variable 'carColor'
    The code:
    Code:
    string carColor;
    string carMake;
    string carType;
    
    switch(carList[lstMadeCars.SelectedIndex].GetType().Name)
    {
    	case "Car":
    	{
    		carColor = ((Car)carList[lstMadeCars.SelectedIndex]).aCarColor.ToString();
    		carMake = ((Car)carList[lstMadeCars.SelectedIndex]).aCarMake.ToString();
    		carType = ((Car)carList[lstMadeCars.SelectedIndex]).aCarType.ToString();
    		break;
    	}
    	case "SportsCar":
    	{
    		carColor = ((SportsCar)carList[lstMadeCars.SelectedIndex]).aCarColor.ToString();
    		carMake = ((SportsCar)carList[lstMadeCars.SelectedIndex]).aCarMake.ToString();
    		carType = ((SportsCar)carList[lstMadeCars.SelectedIndex]).aCarType.ToString();
    		break;
    	}
    }
    
    lblResult.Text = carType + "\n" + carMake + " " + carColor;  //carType, carMake, and carColor where all underlined by VS on this line
    Am I missing something here? Thanks for any help in advance.
    Last edited by Comreak; Apr 2nd, 2005 at 03:39 PM.
    C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: Use of unassigned local variable

    When you declare a variable without assigning a value (e.g. string x, it is essentially points to absolutely nothing - thus the compilation error.

    Easiest way to get rid of those compile errors would be to initialise your variables prior to entering the case statement:
    Code:
    string carColor = string.Empty;
    string carMake = string.Empty;
    string carType = string.Empty;

  3. #3

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319

    Re: Use of unassigned local variable

    I figured it was something like that. So neither value types nor ref types are automatically given a value when declared within a function?
    Last edited by Comreak; Apr 2nd, 2005 at 03:43 PM.
    C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Quote Originally Posted by Comreak
    I figured it was something like that. So neither value types nor ref types are automatically given a value when declared within a function?
    That's right.

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