473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

use of unassigned local variable

5
i am having some trouble trying to extract my custID from a SQL database as i keep getting a "use of unassigned local variable " on the custID i have underlined.. can someone help me please!!!
Expand|Select|Wrap|Line Numbers
  1. String[] custID;
  2.             while (thisReader.HasRows)
  3.             {
  4.                 while (thisReader.Read())
  5.                 {
  6.  
  7.                     custID[z] = thisReader.GetString(thisReader.GetOrdinal("CustId"));
  8.                 }
  9.                 z++;
  10.             }
  11.             thisReader.Close();
  12.             thisConnection.Close();
  13.  
  14.  
  15.             for (int j = 1; j < custID.Length - 1; j++)
  16.             {
  17.                 Label1.Text = custID[j];
  18.  
  19.             }
  20.  
  21.  
thanks a million!!!
Jul 19 '10 #1

✓ answered by pankajvtcse

@zeejay
The error is due to, you are trying to use the UNASSIGNED variable. custID string array is assigned value inside the WHILE block, this is not sure for the compiler that the code where you are assigning the value to the variable will execute. But the line 15 will surely executed and will generate Error cos the custID might be null.
To avoid this condition you can use default values to be assigned to the variables at the time of declaration.
Modify the code Line 1 to Line 10:

String[] custID = new String[0];
int z = 0;
while (thisReader.HasRows)
{
while (thisReader.Read())
{
Array.Resize<String>(ref custID,custID.Length + 1);
custID[z] = thisReader.GetString(thisReader.GetOrdinal("CustId "));
}
z++;
}

This will surely work for you.
But instead of using String[] you can also use List<String> this will help you to reduce the code and also List provides extra functionality like sort, search.


List<String> custID = new List<String>();
while (thisReader.HasRows)
{
while (thisReader.Read())
{
custID.Add(thisReader.GetString(thisReader.GetOrdi nal("CustId")););
}
}

1 3239
@zeejay
The error is due to, you are trying to use the UNASSIGNED variable. custID string array is assigned value inside the WHILE block, this is not sure for the compiler that the code where you are assigning the value to the variable will execute. But the line 15 will surely executed and will generate Error cos the custID might be null.
To avoid this condition you can use default values to be assigned to the variables at the time of declaration.
Modify the code Line 1 to Line 10:

String[] custID = new String[0];
int z = 0;
while (thisReader.HasRows)
{
while (thisReader.Read())
{
Array.Resize<String>(ref custID,custID.Length + 1);
custID[z] = thisReader.GetString(thisReader.GetOrdinal("CustId "));
}
z++;
}

This will surely work for you.
But instead of using String[] you can also use List<String> this will help you to reduce the code and also List provides extra functionality like sort, search.


List<String> custID = new List<String>();
while (thisReader.HasRows)
{
while (thisReader.Read())
{
custID.Add(thisReader.GetString(thisReader.GetOrdi nal("CustId")););
}
}
Jul 19 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike P | last post by:
I keep getting the error 'Use of unassigned local variable' in my code, which I have used before and it works fine : SqlTransaction Trans1, Trans2; SqlConnection...
5
by: Mike P | last post by:
I am instantiating a class in a switch statement as there are a number of different overloads depending upon the data entered by the user. The problem I have is that after instantiating my class,...
3
by: John Smith | last post by:
In the following (pseudo)code, why is it that C# returns an "unassigned local variable" error for nVar? It seems I have to declare the variable outside foo() for the error to dissapear. void...
2
by: Doug | last post by:
I am trying to work with an object and create it like so: EXTRA.Sessions oSessions = new EXTRA.Sessions(); This doesn't work because I get this error: 'Cannot create an instance of the...
9
by: tshad | last post by:
I am getting an error: Use of unassigned local variable 'postDateRow' But it is assigned. Here is the code: int payDateRow; int postDateRow;
22
by: Laura T. | last post by:
In the following example, c# 2.0 compiler says that a3 and ret are used before assigned. as far as I can see, definite assignment is made. If I add finally { ret = true; a3 = "b3";
3
by: Hegel | last post by:
Some body please help me. I finds this code really correct. However I am a beginner The following code yields Use of unassigned local variable 'fout' error in my pgm please help using...
2
by: plasmay | last post by:
Hi, I am new in learning c#, and have recently encountered a problem: static float ComputeAvg(float a) { float sum; int i; for (i = 0; i...
3
by: lenniekuah | last post by:
Hi Good Guy, I am very surprised of the coding generate error message Error 1 Use of unassigned local variable 'strSql' even though the strSQL variable was declared. Not sure what is wrong...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.