473,788 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using OUT in a method

hello everybody !
I get this error 3 times : "The output parameter must be assigned before
the control leaves the current method" (dor dMin and dMax)
this one 1 time : Use of the parameter 'out' is not assigned 'dMax'

This is my first steps in C#: i can't manage(understa nd) this error.

I would like to test to variables which can be change on form1 by 2 numeric
updown controls before sending them to a UserControl which make some
calculation with these 2 numbers like this :

if (_genData.Numbe rMin < _genData.Number Max)
{

S = (int)(_rand.Nex t((int)_genData .NumberMin,
(int)_genData.N umberMax + 1) / 3) * 3;
}

else
{
//Choisir le nombre cible (_iS) compris dans la fourchette
et le rendre multiple de trois.
// Set the target number (_iS) in the range and make
multiple of three.
So if user modify the value and nudNumMin .Value becomes nudNumMax
..Value the calculation could send an error in calculating the value S.
That's why i have to test and correct the values before sending them to the
UserControl.

the code in form1 is :
#region min max check
string message = "Attention ! Le nombre maximal doit être au
nombre minimal. L'erreur sera corrigée";
string caption = "Erreur dans le choix de la taille des nombres.";
MessageBoxButto ns buttons = MessageBoxButto ns.OK;
DialogResult result;

// this method checks the values and shows an error message if
// they are not as expected and corrects them to a valid state

public bool CheckMinMaxValu es(out int dMin, out int dMax)
{

if ( nudNumMin .Value nudNumMax .Value )
{
result = MessageBox.Show (message, caption, buttons);
dMin = dMax - 1;
return false;
}
return true;
}
#endregion //min max check

and then :

private void nudNumMax_Value Changed( object sender, EventArgs e )
{
int dMin = (int)nudNumMin. Value;
int dMax = (int)nudNumMax. Value;
if (!CheckMinMaxVa lues(out dMin, out dMax))
{
//... do what ever is needed when the check fails
nudNumMin.Value = dMin;
nudNumMax.Value = dMax;
nudNumMax.Value = nudNumMin.Value + 1;
_currParam.Gene rationData.Numb erMax = (int) nudNumMin.Value
+ 1; // for example

}
_currParam.Gene rationData.Numb erMax = (int)nudNumMax. Value;
ValueChanged( true );
}

I hope you understand me....
and can help me !
pascal
--
http://www.scalpa.info

Jun 27 '08 #1
17 2237
On Apr 13, 12:58 am, "Pascal" <scalpaSansS... @scalpa.infowro te:
hello everybody !
I get this error 3 times : "The output parameter must be assigned before
the control leaves the current method" (dor dMin and dMax)
this one 1 time : Use of the parameter 'out' is not assigned 'dMax'

This is my first steps in C#: i can't manage(understa nd) this error.
The code you posted doesn't modify dMax, so dMax doesn't need to be an
out parameter. Remove the "out" keyword for dMax and it should work.

Jesse
Jun 27 '08 #2
Have a look at http://www.blackwasp.co.uk/CSharpMethodParameters.aspx for
details on output parameters.

--

BlackWasp
www.blackwasp.co.uk
"Pascal" <sc************ @scalpa.infowro te in message
news:66******** *************** ***********@mic rosoft.com...
hello everybody !
I get this error 3 times : "The output parameter must be assigned before
the control leaves the current method" (dor dMin and dMax)
this one 1 time : Use of the parameter 'out' is not assigned 'dMax'

This is my first steps in C#: i can't manage(understa nd) this error.

I would like to test to variables which can be change on form1 by 2
numeric updown controls before sending them to a UserControl which make
some calculation with these 2 numbers like this :

if (_genData.Numbe rMin < _genData.Number Max)
{

S = (int)(_rand.Nex t((int)_genData .NumberMin,
(int)_genData.N umberMax + 1) / 3) * 3;
}

else
{
//Choisir le nombre cible (_iS) compris dans la fourchette
et le rendre multiple de trois.
// Set the target number (_iS) in the range and make
multiple of three.
So if user modify the value and nudNumMin .Value becomes nudNumMax
.Value the calculation could send an error in calculating the value S.
That's why i have to test and correct the values before sending them to
the UserControl.

the code in form1 is :
#region min max check
string message = "Attention ! Le nombre maximal doit être au
nombre minimal. L'erreur sera corrigée";
string caption = "Erreur dans le choix de la taille des nombres.";
MessageBoxButto ns buttons = MessageBoxButto ns.OK;
DialogResult result;

// this method checks the values and shows an error message if
// they are not as expected and corrects them to a valid state

public bool CheckMinMaxValu es(out int dMin, out int dMax)
{

if ( nudNumMin .Value nudNumMax .Value )
{
result = MessageBox.Show (message, caption, buttons);
dMin = dMax - 1;
return false;
}
return true;
}
#endregion //min max check

and then :

private void nudNumMax_Value Changed( object sender, EventArgs e )
{
int dMin = (int)nudNumMin. Value;
int dMax = (int)nudNumMax. Value;
if (!CheckMinMaxVa lues(out dMin, out dMax))
{
//... do what ever is needed when the check fails
nudNumMin.Value = dMin;
nudNumMax.Value = dMax;
nudNumMax.Value = nudNumMin.Value + 1;
_currParam.Gene rationData.Numb erMax = (int) nudNumMin.Value
+ 1; // for example

}
_currParam.Gene rationData.Numb erMax = (int)nudNumMax. Value;
ValueChanged( true );
}

I hope you understand me....
and can help me !
pascal
--
http://www.scalpa.info
Jun 27 '08 #3
Pascal wrote:
hello everybody !
I get this error 3 times : "The output parameter must be assigned
before the control leaves the current method" (dor dMin and dMax)
this one 1 time : Use of the parameter 'out' is not assigned 'dMax'

This is my first steps in C#: i can't manage(understa nd) this error.

I would like to test to variables which can be change on form1 by 2
numeric updown controls before sending them to a UserControl which make
some calculation with these 2 numbers like this :

if (_genData.Numbe rMin < _genData.Number Max)
{

S = (int)(_rand.Nex t((int)_genData .NumberMin,
(int)_genData.N umberMax + 1) / 3) * 3;
}

else
{
//Choisir le nombre cible (_iS) compris dans la
fourchette et le rendre multiple de trois.
// Set the target number (_iS) in the range and make
multiple of three.
So if user modify the value and nudNumMin .Value becomes nudNumMax
.Value the calculation could send an error in calculating the value S.
That's why i have to test and correct the values before sending them to
the UserControl.

the code in form1 is :
#region min max check
string message = "Attention ! Le nombre maximal doit être au
nombre minimal. L'erreur sera corrigée";
string caption = "Erreur dans le choix de la taille des nombres.";
MessageBoxButto ns buttons = MessageBoxButto ns.OK;
DialogResult result;

// this method checks the values and shows an error message if
// they are not as expected and corrects them to a valid state

public bool CheckMinMaxValu es(out int dMin, out int dMax)
{

if ( nudNumMin .Value nudNumMax .Value )
{
result = MessageBox.Show (message, caption, buttons);
dMin = dMax - 1;
return false;
}
return true;
}
#endregion //min max check

and then :

private void nudNumMax_Value Changed( object sender, EventArgs e )
{
int dMin = (int)nudNumMin. Value;
int dMax = (int)nudNumMax. Value;
if (!CheckMinMaxVa lues(out dMin, out dMax))
{
//... do what ever is needed when the check fails
nudNumMin.Value = dMin;
nudNumMax.Value = dMax;
nudNumMax.Value = nudNumMin.Value + 1;
_currParam.Gene rationData.Numb erMax = (int)
nudNumMin.Value + 1; // for example

}
_currParam.Gene rationData.Numb erMax = (int)nudNumMax. Value;
ValueChanged( true );
}

I hope you understand me....
and can help me !
pascal
If you use the "out" keyword for the parameters, you must assign the
value to it before go out of method. The "out" keyword requires that.
In your method CheckMinMaxValu es(), the value of two "out" parameters
will not be set if the "if" statement is false, so the compiler didn't work.

--
Thanks,
Duy Lam Phuong
Jun 27 '08 #4
On Apr 13, 1:12 am, "BlackWasp" <nospam@pleasew rote:
Have a look athttp://www.blackwasp.c o.uk/CSharpMethodPar ameters.aspxfor
details on output parameters.
That article and its predecessor propagate the "objects are passed by
reference" myth which confuses people who know what pass-by-reference
semantics really mean.

See http://pobox.com/~skeet/csharp/parameters.html for a more accurate
description.

Jon
Jun 27 '08 #5
BlackWasp wrote:
Have a look at http://www.blackwasp.co.uk/CSharpMethodParameters.aspx
for details on output parameters.
Eugh.

While it's always good to have more reference material out there for
people to learn from, I think you need to read the C# specification and
then go back and correct some of the bugs.

In particular:

"So far in the C# Fundamentals tutorial, we have created methods that
accept parameters and return a single value. These parameters are known
as value parameters. This is because when used with value types such as
the numeric types and structures, a copy of the value of the parameter
is passed to the method."

This seems to imply that reference types are not passed by reference.
This is incorrect. They are passed by value (that value is a reference).

"An output parameter is declared using the out keyword before the
parameter type and name. To return a value using the parameter, the
variable is simply assigned a value."

An output parameter doesn't ever 'return' a value. It just represents
the same storage location as the variable used as the argument in the
method invocation.

"When an object based upon any class is passed to a method as a
parameter, the object is passed by reference"

This is wrong.
Reference types passed into value parameters are not passed by reference
- they're passed by value.

"It is possible to create the same behaviour for value types by
declaring a parameter to be a reference parameter"

The behaviour is very different, and you can pass reference types by
reference in the same way.

Alun Harford
Jun 27 '08 #6
Thanks for your help, it took me a while to understand every thing ....
Thanks for the link too.

Jun 27 '08 #7
I hear what you are saying but remember that this is a _fundamentals_
tutorial for beginners. Some would say that the semantics are very
important. In my experience they may be but beginners can get too wrapped
up in them and end up confused rather than developing.

eg. Yes, out parameters don't return a value but to beginners, that is the
effect.

It's swings and roundabouts but I am going to tidy up a few sentences.

Cheers!

--

BlackWasp
www.blackwasp.co.uk
"Alun Harford" <de*****@alunha rford.co.ukwrot e in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
BlackWasp wrote:
>Have a look at http://www.blackwasp.co.uk/CSharpMethodParameters.aspx for
details on output parameters.

Eugh.

While it's always good to have more reference material out there for
people to learn from, I think you need to read the C# specification and
then go back and correct some of the bugs.

In particular:

"So far in the C# Fundamentals tutorial, we have created methods that
accept parameters and return a single value. These parameters are known
as value parameters. This is because when used with value types such as
the numeric types and structures, a copy of the value of the parameter is
passed to the method."

This seems to imply that reference types are not passed by reference. This
is incorrect. They are passed by value (that value is a reference).

"An output parameter is declared using the out keyword before the
parameter type and name. To return a value using the parameter, the
variable is simply assigned a value."

An output parameter doesn't ever 'return' a value. It just represents the
same storage location as the variable used as the argument in the method
invocation.

"When an object based upon any class is passed to a method as a parameter,
the object is passed by reference"

This is wrong.
Reference types passed into value parameters are not passed by reference -
they're passed by value.

"It is possible to create the same behaviour for value types by declaring
a parameter to be a reference parameter"

The behaviour is very different, and you can pass reference types by
reference in the same way.

Alun Harford
Jun 27 '08 #8
On Apr 18, 10:11 am, "BlackWasp" <nospam@pleasew rote:
I hear what you are saying but remember that this is a _fundamentals_
tutorial for beginners.
I would say that correctly differentiating between passing a reference
by value and passing a parameter by reference is quite fundamental. It
affects the whole mental model of how objects are stored.
Some would say that the semantics are very
important. In my experience they may be but beginners can get too wrapped
up in them and end up confused rather than developing.
I would personally rather beginners took a little bit longer to learn
the fundamentals, than came away with a broken mental model which is
much harder to undo later on.

Jon
Jun 27 '08 #9
On Apr 18, 1:41 pm, "BlackWasp" <nospam@pleasew rote:
you may be right. There are always two approaches to a problem. As I said
to Alun, I am making some minor changes. I am on the side of the fence that
believes that overall concepts are more appropriate for beginners than
finite details. I can understand that some people disagree with this
approach while some support it.
There's a difference between omitting fine details and stating
outright falsehoods, however. I'm fine with the idea of leaving a sign
saying "here be dragons" around particularly difficult bits, and
perhaps coming back to them later - but anything which encourages a
beginner to believe something which is actually untrue is a bad thing,
IMO - simply because "unlearning " is difficult.

I'm actually of the belief that the truth about the nature of
references and objects is easier to grasp than we think - it's just
*very* hard to write about clearly. The concept itself isn't that
hard, but explaining it is difficult. Personally I like the analogy of
a piece of paper for value types (to create a copy, you photocopy the
piece of paper) vs a web page for reference types (to tell a friend
about the page, you just give him a copy of the URL, rather than the
page itself).

The bonus of this is that once this fundamental concept has been
grasped, all kinds of things become *much* easier to explain -
assignment, parameter passing, garbage collection, memory allocation
etc.

Jon
Jun 27 '08 #10

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

Similar topics

4
2072
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it looked like about 20:20 split on the following syntaxes: 1 def func(arg1, arg2, arg3) : function... 2 def func(arg1, arg2, arg3): function...
0
6705
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft Visual Basic .NET version of this article, see 308049. For a Microsoft Visual C++ .NET version of this article, see 310071. For a Microsoft Visual J# .NET version of this article, see 320627. This article refers to the following Microsoft .NET...
11
6602
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
3
2005
by: flat_ross | last post by:
For anyone who is just getting into VB.NET and/or is starting to work with inheritance I would like to point out a potential pitfall. We found this confusion recently when code-reviewing an application. If you have not used the keyword 'Overridable' then read on for sure... If you setup a child class and you want to override a method in your base class, you may see the squiggles under your child's method name. The pop-up/build error says...
7
2583
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be stupid or missing the point. I used DrawString( ) as a simple test but I cannot get it to work at all unless I handle my custom controls Paint event.
3
3202
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what the set piece of the Name property gets compiled to, which I am getting from the stack trace, then the attributes are not returned. For example, Class Person has a property called "Name" which has a custom attribute decorating it. Inside the set...
6
3681
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) { List<TimeSpantimes = new List<TimeSpan>(); DateTime start; DateTime end; for (int j = 0; j < 1000; j++)
19
248267
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect this data is essential to any developer. This article is a basic tutorial on how to user HTML Forms, the most common method of data collection. Assumptions - Basic HTML knowledge. - Basic PHP knowledge. HTML Forms A common and simple way of...
65
3929
by: Arjen | last post by:
Hi, Form a performance perspective, is it wise to use the ref statement as much as possible? Thanks! Arjen
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10373
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10118
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7519
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.