On May 11, 4:23 pm, "BERNARDO MEDINA" <yapo...@comcast.netwrote:
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.