array as out parameter

  • Thread starter Thread starter Hendri Adriaens
  • Start date Start date
H

Hendri Adriaens

Hi,

I want to write a method to read the first column from a textfile and output
them in 2 arrays to the main program. I don't know the length of the column
beforehand. I use out to avoid needing to assign the array a value, but
still, I get the following complaint:
Use of unassigned out parameter 'variabelen'.

My code is below. I don't use it actually, I want to assign it a value. I
hope you can help.

Thanks, best regards,
-Hendri Adriaens.

static void Main(string[] args)
{
string[] variabelen;
leesSchattingen("m1_IB_m.txt", out variabelen);
leesSchattingen("m1_IB_v.txt", out variabelen);
}
static void leesSchattingen(string bestandsNaam, out string[] variabelen)
{
try
{
TextReader tr = new StreamReader(bestandsNaam);
string regel;
int i = 0;
while ((regel = tr.ReadLine()) != null)
{
string[] cellen = regel.Split('\t');
if (cellen[0] != "")
{
Console.WriteLine(cellen[0] + '\t' + cellen[1]);
variabelen = cellen[0];
i++;
}
}
tr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Environment.Exit(1);
}
}
 
Hendri Adriaens said:
Use of unassigned out parameter 'variabelen'.

You forgot to initialize the array inside the method (that is, you try
to assign the elements without ever allocating memory to the array).

Since you don't know beforehand the size that you need, you can use a
List<string> to add the elements, and then convert it to an array of strings
before returning from the method:

using System.Collections.Generic;
....
static void leesSchattingen(string bestandsNaam, out string[] variabelen)
{
try
{
List<string> lstvariable = new List<string>();
TextReader tr = new StreamReader(bestandsNaam);
string regel;
while ((regel = tr.ReadLine()) != null)
{
string[] cellen = regel.Split('\t');
if (cellen[0] != "")
{
Console.WriteLine(cellen[0] + '\t' + cellen[1]);
lstvariable.Add(cellen[0]);
}
}
tr.Close();
variabelen = lstvariable.ToArray();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Environment.Exit(1);
}
}
 
Hi,

I want to write a method to read the first column from a textfile and
output them in 2 arrays to the main program. I don't know the length
of the column beforehand. I use out to avoid needing to assign the
array a value, but still, I get the following complaint:
Use of unassigned out parameter 'variabelen'.

You are trying to use the variable "variabelen" before it's been
assigned. Oddly enough. :)

The array doesn't exist until you create it. You never create it, but in
your loop you try to set elements of the array.

At a minimum, you need to have a line like "variabelen = new string[1]"
somewhere. Note that you need to choose the size of the array when you
create it. If your index "i" gets too large, you need to resize the array
to accomodate that.

For example (note that if you can change your type to be a List<string>
instead of a string[], the code gets a lot simpler, since List<> handles
the size adjustments for you):

static void leesSchattingen(string bestandsNaam, out string[] variabelen)
{
try
{
TextReader tr = new StreamReader(bestandsNaam);
string regel;
int i = 0;
variabelen = new string[16]; // gets assigned here :)
while ((regel = tr.ReadLine()) != null)
{
string[] cellen = regel.Split('\t');
if (cellen[0] != "")
{
Console.WriteLine(cellen[0] + '\t' + cellen[1]);
if (i >= variabelen.Length)
{
// Need to adjust the size of the array
Array.Resize(ref variabelen, variabelen.Length * 2);
}
variabelen = cellen[0];
i++;
}
}

// Trim the array to size
Array.Resize(ref variabelen, i);

tr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Environment.Exit(1);
}
}

Pete
 
Hendri Adriaens said:
I want to write a method to read the first column from a textfile and output
them in 2 arrays to the main program. I don't know the length of the column
beforehand. I use out to avoid needing to assign the array a value, but
still, I get the following complaint:
Use of unassigned out parameter 'variabelen'.

My code is below. I don't use it actually, I want to assign it a value. I
hope you can help.

I think the problem is that you don't really understand what "out" is
for.

The idea is it's an extra way of returning information, effectively. So
at the start of the method with the parameter (leesSChattingen in this
case) variablen is considered unassigned. That means you can't use it
until you've given it a value. You never give the variable a value, but
try to use index into it.

"out" parameters don't mean you can have dynamic arrays. If you want
something like that, I suggest you use a List<string> if you're on 2.0,
or ArrayList if you're on 1.1.
 
Hi,

You don't know how many lines are there in the file before reading all the
content, so yo ucannot use array for that.

What you can do is using an ArrayList to store the elements and then at the
end convert it to an array:
Btw, the error you are getting is cause the variable was not initiated.

The code could be like:

TextReader tr = new StreamReader(bestandsNaam);
string regel;
ArrayList ar = new ArrayList()

while ((regel = tr.ReadLine()) != null)
{
string[] cellen = regel.Split('\t');
if (cellen[0] != "")
{
Console.WriteLine(cellen[0] + '\t' + cellen[1]);
ar.Add( cellen[0] )

}
}
tr.Close();

string[] retval = ar.ToArray( typeof(string));
 
Since you don't know beforehand the size that you need, you can use a
List<string> to add the elements, and then convert it to an array of
strings before returning from the method:

Ok, thank you. You're code almost worked, but it complained that variabelen
should be assigned before leaving the method. I figured it was because of
the catch that it could possibly end up in and added a variabelen=new
string[0]; there and then it worked nicely.

Thanks for your help, also to the other people answering my question!

Best regards,
-Hendri.
 
Back
Top