473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

passing reference variable!!. out bug?

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 !!
May 11 '07 #1
3 1408
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

"BERNARDO MEDINA" <ya*****@comcast.netдÈëÏûÏ¢ÐÂÎÅ:OU**************@ TK2MSFTNGP04.phx.gbl...
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 !!


May 12 '07 #2
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.

May 12 '07 #3
"BERNARDO MEDINA" <ya*****@comcast.netschrieb im Newsbeitrag
news:OU**************@TK2MSFTNGP04.phx.gbl...
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
May 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
4
by: mexican_equivalent | last post by:
X-no-archive: yes Newbie C++ programmer reads the following statement from the C++ 'Deitel & Deitel' textbook: "Reference variables can be used as local aliases within a function. They must...
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
4
by: Edward Diener | last post by:
My undersstanding of using a ref parameter is to be able to pass initialized value variables as references. Is there any purpose to using a ref paraneter with a reference variable ? If one does it...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
7
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
3
by: James Robertson | last post by:
I am new to the ASP and VB thing so be kind. Question I have is that I have created an ASPX web site to use as an E-Mail page. But I want to use this for a lot of users. Can I create the link on...
1
by: I.am.the.Buddha | last post by:
I am still learning asp and sql. I am having trouble with passing a variable to within an sql statement. I am sure it is something simple like misuse of quotes. Since it may help if i say what i...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.