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

Adam
  • 15,537
  • 2
  • 42
  • 63
Mohan Raj K
  • 65
  • 3
  • 11
  • Looks like you could just put `string[] arrList = new arrList[this.Controls.Count]`. – benjer3 Jul 01 '12 at 16:02
  • 2
    Actually, why not just have `string[] arrList = this.Controls.Select(x => x.ToString()).ToArray();`? – benjer3 Jul 01 '12 at 16:04
  • Possible duplicate of [Why C# local variables must be initialized?](https://stackoverflow.com/questions/4182666/why-c-sharp-local-variables-must-be-initialized) –  Nov 12 '19 at 08:16

4 Answers4

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();
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • But length is not there. only Count.. any way it works... Thanks for the help!! :) – Mohan Raj K Jul 01 '12 at 16:07
  • @MohanRajK: Thanks for the correction. Please also consider the other options because they are better. – Mark Byers Jul 01 '12 at 16:08
  • 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 – Mohan Raj K Jul 01 '12 at 17:16
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]);                                          
Monroe Thomas
  • 4,962
  • 1
  • 17
  • 21
0

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

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

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
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");

Tibi
  • 4,015
  • 8
  • 40
  • 64