Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

passing reference variable!!. out bug?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BERNARDO MEDINA - 12 May 2007 00:23 GMT
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 !!
gshzheng - 12 May 2007 01:20 GMT
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" <yapoinc@comcast.net> дÈëÏûÏ¢ÐÂÎÅ:OUDOTNClHHA.1604@TK2MSFTNGP04.phx.gbl...
> Hi somebody cant tellme what is wrong here
> If i do this !!! This is the code
[quoted text clipped - 52 lines]
>
> Thank !!
Bruce Wood - 12 May 2007 07:46 GMT
> 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:
>
[quoted text clipped - 8 lines]
>         {
>             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.

>         }
>     }
[quoted text clipped - 3 lines]
>                                         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:
[quoted text clipped - 10 lines]
>         {
>             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.
Christof Nordiek - 14 May 2007 10:07 GMT
>        public static void Main()
>        {
[quoted text clipped - 6 lines]
>            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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.