By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,989 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,989 IT Pros & Developers. It's quick & easy.

What is wrong?

P: n/a

void establishAddress(out string [] address_line)
{
// do something
}

string[] address_to_print = new string[50];
address_to_print = establishAddress(out address_line);

Apart from the fact that I could pass out an array,
what is wrong with the above code?

Adrian.

Feb 4 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Try this:

void establishAddress(out string [] address_line)
{
address_to_print = new string[50];
// do something
}

string[] address_to_print;
establishAddress(out address_line);
"Adrian <" <no*@home.anywherewrote in message
news:45********************@dreader2.news.tiscali. nl...
>
void establishAddress(out string [] address_line)
{
// do something
}

string[] address_to_print = new string[50];
address_to_print = establishAddress(out address_line);

Apart from the fact that I could pass out an array,
what is wrong with the above code?

Adrian.

Feb 4 '07 #2

P: n/a
Hi,

Do you get any error?
The code looks fine.
It's not perfect though. you do not have to initialize the array outside the
method; that's the reason for the out modificator.

"Adrian <" <no*@home.anywherewrote in message
news:45********************@dreader2.news.tiscali. nl...
>
void establishAddress(out string [] address_line)
{
// do something
}

string[] address_to_print = new string[50];
address_to_print = establishAddress(out address_line);

Apart from the fact that I could pass out an array,
what is wrong with the above code?

Adrian.

Feb 4 '07 #3

P: n/a
Second try.
Again I realize that I could have passed out an array.
Is this a correct usage of the out option?
(By the way AddRange did not work
How shoud that be coded?)
Thanks,
Adrian.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int[] argu = new int[2];
doSomething(out argu);
listBox1.Items.Add(argu[0]);
listBox1.Items.Add(argu[1]);
listBox1.Items.Add(argu[2]);
}

void doSomething(out int [] argument)
{
int[] trial = { 1, 2, 3 };
argument = trial;
}
}
}
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi,

Do you get any error?
The code looks fine.
It's not perfect though. you do not have to initialize the array outside
the
method; that's the reason for the out modificator.

"Adrian <" <no*@home.anywherewrote in message
news:45********************@dreader2.news.tiscali. nl...

void establishAddress(out string [] address_line)
{
// do something
}

string[] address_to_print = new string[50];
address_to_print = establishAddress(out address_line);

Apart from the fact that I could pass out an array,
what is wrong with the above code?

Adrian.



Feb 4 '07 #4

P: n/a
----- Original Message -----
From: "Adrian <" <no*@home.anywhere>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Sunday, February 04, 2007 8:31 AM
Subject: What is wrong?

>
void establishAddress(out string [] address_line)
{
// do something
}

string[] address_to_print = new string[50];
address_to_print = establishAddress(out address_line);

Apart from the fact that I could pass out an array,
what is wrong with the above code?
First let me comment on the above code.

// This line declares a reference to an array of strings
// and assigns a new Array of size 50 to the reference
string[] address_to_print = new string[50];
// This line calls a method and attempts to set the reference
(address_to_print )
// to the return value of the method. Unfortunately the method has a
void
// return type so it is doomed to failure.
address_to_print = establishAddress(out address_line);
Try something like this instead
-------------
using System;

public class Test
{
private string[] ArrayCreatingMethod()
{
// Create and populate a new Array.
//
// [Over simplified example]
string[] data = new string[3];
data[0] = "Happy";
data[1] = "birthday";
data[2] = "to me";

// return a reference to that array
return data;
}

private void Run()
{
string[] mydata = ArrayCreatingMethod();
foreach(string str in mydata)
{
Console.WriteLine(str);
}
}

static void Main()
{
Test test = new Test();
test.Run();
}
}
---------------------

Feb 4 '07 #5

P: n/a
I do not have a problem with the code below,
I was asking how to put right the code using an out parameter.
In the mean time I have posted other code exemplifying
the use of the out parameter and asked whether that
newly posted code was ok, again saying that I am
quite aware that an array could be returned, but that
is not the point here.

Adrian.

Try something like this instead
-------------
using System;

public class Test
{
private string[] ArrayCreatingMethod()
{
// Create and populate a new Array.
//
// [Over simplified example]
string[] data = new string[3];
data[0] = "Happy";
data[1] = "birthday";
data[2] = "to me";

// return a reference to that array
return data;
}

private void Run()
{
string[] mydata = ArrayCreatingMethod();
foreach(string str in mydata)
{
Console.WriteLine(str);
}
}

static void Main()
{
Test test = new Test();
test.Run();
}
}
---------------------

Feb 4 '07 #6

P: n/a
It's not a case of "an array _could_ be returned". It's a case of "an
array _must_ be returned."

If you specify "out" then within the affected routine you must set the
parameter to some value, whether it's an array or null. Either way, it
must be set within the routine.

Your second example is fine because the method assigns an array to the
"out" parameter before terminating.

On Feb 4, 10:43 am, "Adrian <" <n...@home.anywherewrote:
I do not have a problem with the code below,
I was asking how to put right the code using an out parameter.
In the mean time I have posted other code exemplifying
the use of the out parameter and asked whether that
newly posted code was ok, again saying that I am
quite aware that an array could be returned, but that
is not the point here.

Adrian.
Try something like this instead
-------------
using System;
public class Test
{
private string[] ArrayCreatingMethod()
{
// Create and populate a new Array.
//
// [Over simplified example]
string[] data = new string[3];
data[0] = "Happy";
data[1] = "birthday";
data[2] = "to me";
// return a reference to that array
return data;
}
private void Run()
{
string[] mydata = ArrayCreatingMethod();
foreach(string str in mydata)
{
Console.WriteLine(str);
}
}
static void Main()
{
Test test = new Test();
test.Run();
}
}
---------------------- Hide quoted text -

- Show quoted text -

Feb 4 '07 #7

P: n/a
Adrian < <no*@home.anywherewrote:
I do not have a problem with the code below,
I was asking how to put right the code using an out parameter.
In the mean time I have posted other code exemplifying
the use of the out parameter and asked whether that
newly posted code was ok, again saying that I am
quite aware that an array could be returned, but that
is not the point here.
See http://pobox.com/~skeet/csharp/parameters.html for a detailed
explanation of C# parameters.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 4 '07 #8

P: n/a
"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
It's not a case of "an array _could_ be returned". It's a case of "an
array _must_ be returned."
Might you be missing the essence of my question?
Your second example is fine because the method assigns an array to the
"out" parameter before terminating.
Thank you.

Adrian.


Feb 4 '07 #9

P: n/a
On Feb 4, 5:31 am, "Adrian <" <n...@home.anywherewrote:
void establishAddress(out string [] address_line)
{
// do something

}

string[] address_to_print = new string[50];
address_to_print = establishAddress(out address_line);

Apart from the fact that I could pass out an array,
what is wrong with the above code?
Usually, the proper response to people not undestanding the essence of
your question is to restate it, but anyway...

Besides the fact that you didn't assign the "out" parameter in the
establishAddress method (an error that the compiler will catch) there
is another error, which is that you're trying to assign the result
from a void method (which doesn't return any result) to a variable
that you've already pointed to an array of 50 strings. The compiler
would catch that error, too.

Assuming that you meant to say:

string[] address_line = new string[5];
establishAddress(out address_line);

then the error _there_ would be that you instantiated an array of 50
strings and put its reference in address_line, only to have that
reference clobbered by another reference returned by establishAddress
via its out parameter. The compiler _wouldn't_ catch that, by the way.

Feb 5 '07 #10

P: n/a
void establishAddress(out string [] address_line)
{
// do something

}

string[] address_to_print = new string[50];
address_to_print = establishAddress(out address_line);
The most obvious thing is this:
Change that last line to:

establishAddress(out address_line);

The value of establishAddress(...) is void, not string. As a side effect,
it assigns its argument.
Feb 5 '07 #11

P: n/a
There are several things wrong with that code. To quickly learn what they
are, try to compile it. If it compiles, destroy that machine before it takes
over the world.

Your problem brings up another question: Can anyone make a case for writing
a void method that has no input parameters and a single output parameter?
How would this be an improvement over writing a parameter-less method that
returns the required type?

John Mobley

"Adrian <" wrote:
>
void establishAddress(out string [] address_line)
{
// do something
}

string[] address_to_print = new string[50];
address_to_print = establishAddress(out address_line);

Apart from the fact that I could pass out an array,
what is wrong with the above code?

Adrian.

Feb 5 '07 #12

P: n/a
If you go back in this thread you will see that
I have replaced the erroneous example by
another one and have learned that the second
example is correct. So continued discussion
of the first example has lost its relevance.

Many thanks anyway,
Adrian.
Feb 5 '07 #13

P: n/a
Second try.
Again I realize that I could have passed out an array.
Is this a correct usage of the out option?
(By the way AddRange did not work
How shoud that be coded?)

Thanks,
Adrian.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int[] argu = new int[2];
doSomething(out argu);
listBox1.Items.Add(argu[0]);
listBox1.Items.Add(argu[1]);
listBox1.Items.Add(argu[2]);
}

void doSomething(out int [] argument)
{
int[] trial = { 1, 2, 3 };
argument = trial;
}
}
}
This will work as expected (as I see it). There is one minor point:
instead of "int[] argu = new int[2];" in Form1_Load, you can use just
"int[] argu;". You don't need to assign something to argu, as that is
done by the "out".

Hans Kesting
Feb 5 '07 #14

P: n/a
Thank you very much.

Adrian.

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
Second try.
Again I realize that I could have passed out an array.
Is this a correct usage of the out option?
(By the way AddRange did not work
How shoud that be coded?)

Thanks,
Adrian.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int[] argu = new int[2];
doSomething(out argu);
listBox1.Items.Add(argu[0]);
listBox1.Items.Add(argu[1]);
listBox1.Items.Add(argu[2]);
}

void doSomething(out int [] argument)
{
int[] trial = { 1, 2, 3 };
argument = trial;
}
}
}

This will work as expected (as I see it). There is one minor point:
instead of "int[] argu = new int[2];" in Form1_Load, you can use just
"int[] argu;". You don't need to assign something to argu, as that is
done by the "out".

Hans Kesting


Feb 5 '07 #15

P: n/a
Thank you very much.
I tried to thank you earlier but I think my email went astray.

Adrian.

--
"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
Second try.
Again I realize that I could have passed out an array.
Is this a correct usage of the out option?
(By the way AddRange did not work
How shoud that be coded?)

Thanks,
Adrian.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int[] argu = new int[2];
doSomething(out argu);
listBox1.Items.Add(argu[0]);
listBox1.Items.Add(argu[1]);
listBox1.Items.Add(argu[2]);
}

void doSomething(out int [] argument)
{
int[] trial = { 1, 2, 3 };
argument = trial;
}
}
}

This will work as expected (as I see it). There is one minor point:
instead of "int[] argu = new int[2];" in Form1_Load, you can use just
"int[] argu;". You don't need to assign something to argu, as that is
done by the "out".

Hans Kesting


Feb 7 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.