0

I have declared three variables namely

TemplateData tData; TaskInstance tInstance;int tID;

in my program. Though I have clearly declared them, I keep getting an error "Use of unassigned local variable" I am completely baffled as to why my compiler is giving this error.

user1463269
  • 11
  • 1
  • 5
  • 1
    It's not complaining that you haven't *declared* them. It's complaining that you haven't *assigned* to them. – AakashM Jun 18 '12 at 11:32
  • Well as the error says you have not assigned any value – V4Vendetta Jun 18 '12 at 11:33
  • As for *why* the compiler requires you to assign them, [read this](http://stackoverflow.com/questions/8931226/are-c-sharp-uninitalized-variables-dangerous/8933935#8933935) – AakashM Jun 18 '12 at 11:35

6 Answers6

1

You have declared them but you have not assigned a value to them. You must, as a minimum, assign a value of null before you can use those variables, otherwise you would be passing an undefined value to ProcessInput.

ean5533
  • 8,884
  • 3
  • 40
  • 64
1

Compiler is absolutely correct. Although you have declared them but Compiler did not tells you that "Use of undeclared local variable". It tells you "Use of unassigned local variable"..

There is difference between declaration and assignments of variables...

Assign the values to variables. In your code it should be

TemplateData tData = null;
TaskInstance tInstance = null;
int tID = 0;

OR

TemplateData tData = new TemplateData();
TaskInstance tInstance = new TaskInstance();
int tID = 0;
Talha
  • 18,898
  • 8
  • 49
  • 66
0

You have not assigned a value to the variables. that's what the compiler error says. It says "use of unassigned local variable". Note the difference between declaring and assigning a variable. Just set them all to null or 0 (or some other appropriate default value) before using them:

TemplateData tData = null;
TaskInstance tInstance = null;
int tID = 0;

The compiler complains because using a variable without assigning it some value before can often be the cause of bugs.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
0

You need to initialize these values prior to passing them in a method:

   TemplateData tData = new TemplateData();                 
   TaskInstance tInstance = new TaskInstance();                 
   int tID = 0; 
Ebad Masood
  • 2,389
  • 28
  • 46
0

just initialize those variables;

TemplateData tData = null; TaskInstance tInstance = null; int tID = 0;
Nighil
  • 4,099
  • 7
  • 30
  • 56
-1

You should always assign default or null values when u declare vars.

            TemplateData tData = null;
            TaskInstance tInstance = null;
            int tID = 0;
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42