1

While I am trying to debug this code (in C# WinForms), it shows an error

"use of unassigned local variable" at 'arrlist[i]'

Since I'm comparing it with a database variable, I cannot initialize the size of the array.

This is the code:

if (count != 0)
{
    OleDbCommand cmd1 = new OleDbCommand(
        "select seat_no, booking_date, show_time "+
        "from tickets "+
        "where ticket_no = (select max(ticket_no) from tickets)", c);
    OleDbDataReader oledb1 = cmd1.ExecuteReader();
    oledb1.Read();
    string retr_seats = oledb1.GetString(0);
    char comma = ',';
    string[] strarray = retr_seats.Split(comma);
    int ticket_length = strarray.Length;
    string[] arrlist;
    int i = 0;      
    foreach(var control in this.Controls)
    {
        if(control is Label)
        {           
            arrlist[i] = control.ToString();
            i++;
        }
   }       
   for(var j=0;j<=ticket_length;j++)
   {
       for (var k = 0; k <= i-1; k++)
       {
            if (arrlist[k].Contains(strarray[j]))
            {
                MessageBox.Show(strarray[j]);
            }
       }
   }
}

Please help me

3
  • Looks like you could just put string[] arrList = new arrList[this.Controls.Count]. Commented Jul 1, 2012 at 16:02
  • 2
    Actually, why not just have string[] arrList = this.Controls.Select(x => x.ToString()).ToArray();? Commented Jul 1, 2012 at 16:04
  • Possible duplicate of Why C# local variables must be initialized? Commented Nov 12, 2019 at 8:16

4 Answers 4

8

You need to initialize the variable arrlist. Change this line:

string[] arrlist;

To this:

string[] arrlist = new string[this.Controls.Count]; // Must be big enough.

Or better, use a dynamically sized container such as a List<string>.

List<string> arrList = new List<string>();
foreach(var control in this.Controls)
{
    if(control is Label)
    {
        arrlist.Add(control.ToString());
    }
}

Or use LINQ to get the result directly:

string[] arrlist = this.Controls
    .OfType<Label>()
    .Select(control => control.ToString())
    .ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

But length is not there. only Count.. any way it works... Thanks for the help!! :)
@MohanRajK: Thanks for the correction. Please also consider the other options because they are better.
Do you upvote my question?? I'm in urgent that I want to ask another question with image. But I don't have enough reputation points. Please.. I want one more point
1

Change your array to a list, and add values to the list. You can then index the list elements directly, or if you need an array, you can use .ToArray() on the list instance.

Also note that your for loop over j will go out of bounds on strarray unless you change the comparison to < ticket_length from <= ticket_length.

...

var arrlist = new List<string>();

foreach(var control in this.Controls) 
if(control is Label) 
{ 
    arrlist.Add(control.ToString()); 
} 

for(var j=0;j<ticket_length;j++)  
    for (var k = 0; k < arrlist.Count; k++)  
        if (arrlist[k].Contains(strarray[j]))  
             MessageBox.Show(strarray[j]);                                          

Comments

0

string[] arrlist; .... arrlist[i] = control.ToString();

you lost initialization like: arrlist = new string[count];

Comments

0

The problem is that arrlist is defined, but not initialized.

You need to initialize it, like this:

string[] arrlist = new arrlist[size];

If you don't know how big it will be, it is better to use a list:

List<string> arrlist = new List<string>();

and to add items: arrlist.add("some string");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.