473,467 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

variable trouble!

Hey

The code below generates this compile error:
Use of unassigned out parameter 'x'

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

namespace ConsoleApplication1
{
class Program
{
public static void A(out int x)
{
Console.WriteLine(x);
x = 1;
}

public static int Main(string[] args)
{
int y = 3;
A(out y);
return 0;
}
}
}

I don't understand why I get that error. In Main I give y the value 3, then
I send y into A... y becomes x in A so it should work.... but no don't
work.. I get this compile error mention above....

Is it so that a parameter sent (using out) into a method always must be
given a value before it can be used in the method? That sounds very unlikely
to me.. but then again I'm just trying to learn C# :-)

I would appreciate if some of you could give a comment on why I get that
compile error!

Jeff
Nov 26 '06 #1
4 1369
The problem is this line:

Console.WriteLine(x);

Because out parameters are NOT required to be initialized prior to being
passed (the only difference between out and ref parameters is that ref
parameters ARE required to be initialized before being passed), there is a
chance that your "x" variable won't have been initialized when you hit your
Console.WriteLine(x); statement.

"Jeff" <it************@hotmail.com.NOSPAMwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
Hey

The code below generates this compile error:
Use of unassigned out parameter 'x'

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

namespace ConsoleApplication1
{
class Program
{
public static void A(out int x)
{
Console.WriteLine(x);
x = 1;
}

public static int Main(string[] args)
{
int y = 3;
A(out y);
return 0;
}
}
}

I don't understand why I get that error. In Main I give y the value 3,
then I send y into A... y becomes x in A so it should work.... but no
don't work.. I get this compile error mention above....

Is it so that a parameter sent (using out) into a method always must be
given a value before it can be used in the method? That sounds very
unlikely to me.. but then again I'm just trying to learn C# :-)

I would appreciate if some of you could give a comment on why I get that
compile error!

Jeff


Nov 26 '06 #2
"Scott M." wrote:
(the only difference between out and ref parameters is that ref
parameters ARE required to be initialized before being passed)
Not quite - out parameters, like results, must be set in all execution
paths through the method. Ref parameters don't have to be set.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Nov 26 '06 #3
Right, I was getting my initialize and my assign confused. The problem with
the OP's code is as I stated though (the Definate Assignment Rule).
"Jon Shemitz" <jo*@midnightbeach.comwrote in message
news:45***************@midnightbeach.com...
"Scott M." wrote:
>(the only difference between out and ref parameters is that ref
parameters ARE required to be initialized before being passed)

Not quite - out parameters, like results, must be set in all execution
paths through the method. Ref parameters don't have to be set.

--

.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.

Nov 26 '06 #4
Thanks

"Jon Shemitz" <jo*@midnightbeach.comwrote in message
news:45***************@midnightbeach.com...
"Scott M." wrote:
>(the only difference between out and ref parameters is that ref
parameters ARE required to be initialized before being passed)

Not quite - out parameters, like results, must be set in all execution
paths through the method. Ref parameters don't have to be set.

--

.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.

Nov 26 '06 #5

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

Similar topics

12
by: Bill | last post by:
For my personal use I am accessing a railway timetable page, parsing its contents and then sending brief relevant information as the subject line of an email to be read on a mobile phone. The...
2
by: Fernando Rodriguez | last post by:
Hi, I have a parameter defined in a module, called PREVIEW. Many functions use it's value to modify their behavior. A function called dispatch checks the user arguments in sys.argv and calls...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
by: Marc Missire | last post by:
Hi, I have an issue below I'd love help with, involving a static variable, Application_Start, and a background thread. In global.asax.cs I have a static variable (outside any method) with a...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
22
by: James Brodie | last post by:
I just wanted to get some advice on what are the better practices: 1) Should variables be declared at the beginning of a function or just before they are required? 2) Should all variabled be...
4
by: Alan | last post by:
I`m having trouble figuring out the correct syntax for accessing an element of a struct variable (t_list) passed by reference to a function. The compiler does not like the code below when I added...
9
by: Pyenos | last post by:
Approach 1: class Class1: class Class2: def __init__(self):self.variable="variable" class Class3: def method():print Class1().Class2().variable #problem Approach 1.1:
6
by: gara.matt | last post by:
Hi, I was wondering, I've been reading some C++ code written by others, usually libraries and stuff, and I've come across what strikes me as a distinctive style that pervades most of the code...
1
by: pretzelboy | last post by:
Hi, I last wrote software 13years ago in the pascal, dbase, clipper days. I have recently built a Ubuntu Box and with C++ (and help from the web) setup a serial barcode reader program using Mysql...
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
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...
1
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.