passing reference variable!!. out bug?

  • Thread starter Thread starter BERNARDO MEDINA
  • Start date Start date
B

BERNARDO MEDINA

Hi somebody cant tellme what is wrong here
If i do this !!! This is the code

Sample 1:

using System;
using System.Collections.Generic;
using System.Text;
namespace test001
{
public class Program
{
public static void Main()
{
int var01;
var01 = 90;
mr(out var01);
}
static void mr(out int varpass)
{
Console.WriteLine(varpass);
}
}
}

This code give me a error : Use of unassigned out parameter 'varpass'
The out parameter 'varpass' must be
assigned to before control leaves the current method

But this is correct
Sample 1:

using System;
using System.Collections.Generic;
using System.Text;

namespace test001
{
public class Program
{
public static void Main()
{
int var01;
var01 = 90;
mr(out var01);
}
static void mr(out int varpass)
{
varpass = 10;
Console.WriteLine(varpass);
}
}

This is a bug? or it supose to happen

Thank !!
 
Hi,

Out-parameter must be assigned some value in the method,
and donot care its any assignment before call the method,
this means you neednot give it a value before.

And,Ref-parameter should be employed if you just want to
pass a refrence to a method.

But,in your simple code,Neither of two decoration need be used.

gshzheng
20070512
 
Hi somebody cant tellme what is wrong here
If i do this !!! This is the code

I've inserted comments in-line with your code, to show you what the
problem is.
Sample 1:

using System;
using System.Collections.Generic;
using System.Text;
namespace test001
{
public class Program
{
public static void Main()
{
int var01;

The line "var01 = 90" at this point is completely superfluous. It does
nothing, because of the next line....
var01 = 90;

What the "out" means here is that "var01 will be set by the mr method;
when mr returns, var01 will have a new value." Not only will it have a
new value, it _must_ have a new value. So, the value going in to this
method doesn't matter. That's why assigning it the value 90 before
hand has no useful effect.
mr(out var01);
}
static void mr(out int varpass)
{

The following line will generate one error. Remember that the "out"
declaration says that "varpass" _must_ be assigned a value before the
method returns, and that the value coming into the method doesn't
matter. From the compiler's point of view, what's happening here is
that you've said that varpass may have any value at all coming in, or
_no value at all_. It may be unassigned. So, the compiler is
complaining that you're trying to use a parameter that in fact may
have no value.
Console.WriteLine(varpass);

The other error that you'll get here is that varpass was never set to
anything in your method, whereas you "promised" with the "out" keyword
that you would set it to something.
}
}
}

This code give me a error : Use of unassigned out parameter 'varpass'
The out parameter 'varpass' must be
assigned to before control leaves the current method

One way to solve the problem is to change the "out"s to "ref"s, which
says that you _may_ choose to change the value of var01 / varpass
within the mr method, or you may not, and that the value coming in is
meaningful. However, since you never bother changing varpass, you can
just strip the "out" keywords entirely, and the errors will go away.
But this is correct
Sample 1:

using System;
using System.Collections.Generic;
using System.Text;

namespace test001
{
public class Program
{
public static void Main()
{
int var01;

Again, setting var01 to a value before calling the mr method has no
useful effect.
var01 = 90;
mr(out var01);
}
static void mr(out int varpass)
{

You've solved both problems now with the following line of code:
you've set varpass to some value within the method (which is what you
promised you'd do when you used the "out" keyword), and you've done so
before you tried to use it in the Console.WriteLine(). So, both errors
go away.
varpass = 10;
Console.WriteLine(varpass);
}
}

This is a bug? or it supose to happen

This is the way it's supposed to work.
 
public static void Main()
{
int var01;
var01 = 90;
mr(out var01);
}
static void mr(out int varpass)
{
Console.WriteLine(varpass);
}

Your using an outparameter, as if it were a ref parameter.
How should this code work different than using ref instead of out?
This is a bug? or it supose to happen

It's not a bug, it's a feature. out parameters are meant to be assigned
inside of the method, not before by the caller.

Christof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top