1

This is a part of UpdateGUI():

DisplayOptions choice = (DisplayOptions)comboBox1.SelectedIndex;
seatMngr.GetSeatInfoStrings(choice, out strSeatInfoStrings);
listBox1.Items.Clear();
listBox1.Items.AddRange(strSeatInfoStrings);

The compiler is complaining about this row (And the parameter of the last row):

seatMngr.GetSeatInfoStrings(choice, out strSeatInfoStrings);

What I'm trying to do is to take an array (strSeatInfoStrings) and put it inside a listbox.

Any ideas?

3
  • 1
    Is there any particular reason you need to use out instead of just returning the array from the method? Commented Nov 17, 2011 at 10:31
  • What error message do you get from the compiler? Commented Nov 17, 2011 at 10:31
  • Yes I need to use out... The error message I got is: #1"The name 'strSeatInfoStrings' does not exist in the current context" #2"The best overloaded method match for 'Assignment3.SeatManager.GetSeatInfoStrings(Assignment3.DisplayOptions, out string[])' has some invalid arguments" #3"Argument 2: cannot convert from 'out strSeatInfoStrings' to 'out string[]'" Commented Nov 17, 2011 at 10:57

2 Answers 2

2

You have to add the declaration of that variable before the call:

DisplayOptions choice = (DisplayOptions)comboBox1.SelectedIndex;
string[] strSeatInfoStrings;
seatMngr.GetSeatInfoStrings(choice, out strSeatInfoStrings); 
listBox1.Items.Clear(); 
listBox1.Items.AddRange(strSeatInfoStrings); 

The other oppinion is to change the signature of your method and return the values, so you can write this

DisplayOptions choice = (DisplayOptions)comboBox1.SelectedIndex;
listBox1.Items.Clear(); 
listBox1.Items.AddRange(seatMngr.GetSeatInfoStrings(choice)); 
Sign up to request clarification or add additional context in comments.

4 Comments

I have already decleared the "string[] strSeatInfoStrings;" in the GetSeatInfoStrings method inside the seatMngr object. When I tried the first suggestion i got "Nullreferenceexception was unhandled" And the second suggestion got me "Error 1 The best overloaded method match for 'System.Windows.Forms.ListBox.ObjectCollection.AddRange(System.Windows.Forms.ListBox.ObjectCollection)' has some invalid arguments C:\Users\Kalle\Desktop\Assignment4-hjälp\Assignment4\Assignment3\MainForm.cs 153 13 Assignment4" And some other problems..
If you declare it inside the method GetSeatInfoStrings then this is the wrong scope. Declare it in the way I showed you in my sample above and simply use the out parameter in your method GetSetInfoStrings and you will see - it works.
I did and it gave me the following two errors #1"Use of unassigned out parameter 'strSeatInfoStrings'" #2"The out parameter 'strSeatInfoStrings' must be assigned to before control leaves the current method"
You have to set the value of an out parameter before you leave the scope of your method. So in case there is a if-statement and you only set the out parameter there, you have to initialize it in the else part too.
0

This smells more like a code design issue. The method name GetSeatInfoStrings clearly expresses that it returns some strings. Based on your use of the method, it looks like it's declared like so:

public void GetSeatInfoStrings(string choice, out string[] result)

In my opinion, it would be far better to declare it like this instead:

public void IEnumerable<string> GetSeatInfoStrings(string choice)

...and just return the array from the method as you normally would. The primary uses for out, as I see it is when you need to return more than one value from the method. The Int32.TryParse method is a great example; the method returns a bool indicating success, and the out parameter will contain the result.

In your case it seems as if you have one result, so using out will only be confusing.

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.