473,386 Members | 1,924 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,386 software developers and data experts.

is it possible to use out and ref keyword together???

hi,
currently iam working on c#
i need to pass a parameter inside a function which is a OUT parameter
and i need to pass it as a reference how to do this?
please help me regarding this issue........

regards
bharathi

Nov 24 '06 #1
6 1487
Hi Selva,
OUT parameter works exactly as reference parameter. The only difference
is that you do not need to initialize the OUT variable before passing
the variable to the method.

if you want to force the method user to pass some data to the method,
you can use ref, if not use OUT. In both the cases data changes to the
param would reflect back to the calling method.

Thanks
-Cnu

selva wrote:
hi,
currently iam working on c#
i need to pass a parameter inside a function which is a OUT parameter
and i need to pass it as a reference how to do this?
please help me regarding this issue........

regards
bharathi
Nov 24 '06 #2
You might also wish to consider an alternative design to using OUT or
ref and
instead return single "values" or return an immutable structure if you
need to
return more than one "value".

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 24 '06 #3

if you are use a ref type ,you didn't need to use the ref/out :)

On Nov 24, 2:23 pm, "selva" <kselvaaku...@yahoo.co.inwrote:
hi,
currently iam working on c#
i need to pass a parameter inside a function which is a OUT parameter
and i need to pass it as a reference how to do this?
please help me regarding this issue........

regards
bharathi
Nov 24 '06 #4
In 99.9% of cases. There are (rare) occasions when you genuinely intend to
reassign a reference that was passed in, and you can't (for some reason)
return the new reference in the return value.

But yes; in many cases "ref" on a reference type parameter is due to
misconceptions. In contrast, "out" is IMO becoming more mainstream and more
useful that "ref", largely due to adopting the TryGetValue and TryParse
signatures from the 2.0 classes.

Marc
Nov 24 '06 #5

Does anyone know why C# does not support "const" keyword on parameter
to avoid reassign the reference? That keyword makes me feel much more
confident when passing reference-type argument.

It is so rare to see reference-type passed by ref (except for
developers who just switched from some other language). But "out", I
think, is OK. It indicates that the author just wants something out,
and don't need to instantiate the variable before passing (null is OK).
Without "out", it suggests that the author wants to gather some
additional info into and existing variable.

Thi
http://thith.blogspot.com

Marc Gravell wrote:
In 99.9% of cases. There are (rare) occasions when you genuinely intend to
reassign a reference that was passed in, and you can't (for some reason)
return the new reference in the return value.

But yes; in many cases "ref" on a reference type parameter is due to
misconceptions. In contrast, "out" is IMO becoming more mainstream and more
useful that "ref", largely due to adopting the TryGetValue and TryParse
signatures from the 2.0 classes.

Marc
Nov 24 '06 #6
Marc... I agree that using a single out parameter is useful as in
TryParse, but a more object oriented approach is simpler and less error
prone for multiple parameters. For instance:

public Mortgage.Calculation Calculate(Mortgage.Parameters m)
{
return Calculate(m.Principal, m.Interest, m.Months, m.Payment,
m.Target);
}

Is easier to use and less error prone than:

private bool TryCalculate(Mortgage.Parameters p,out double
principal,out double interest,out int months,out double payment,out int
target)
{
Mortgage.Calculation result= this.Calculate(p.Principal,
p.Interest, p.Months, p.Payment, p.Target);
principal = result.Principal;
interest = result.Interest;
months = result.Months;
payment = result.Payment;
target = result.Target;

return result.IsValid();
}

Regards,
Jeff
>But yes; in many cases "ref" on a reference type parameter is due to
misconceptions. In contrast, "out" is IMO becoming more mainstream and
more useful that "ref", largely due to adopting the TryGetValue and
TryParse signatures from the 2.0 classes.<
*** Sent via Developersdex http://www.developersdex.com ***
Nov 26 '06 #7

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

Similar topics

6
by: Roy Smith | last post by:
I've got a function that takes a couple of optional keyword arguments. I want to check to make sure I didn't get passed an argument I didn't expect. Right now I'm doing: conversion = None drop...
3
by: Jonathan | last post by:
Hi all! For a match schedule I would like to find all possible combinations of teams playing home and away (without teams playing to themselves of course). I now the simple version works...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
7
by: Rakesh Rajan | last post by:
Hi, I find that when i define a delegate, it gets derived from MulticastDelegate, which provides all funtionality that events would provide (like registering new handlers etc.). Then, apart from...
15
by: Ben | last post by:
Hey, Not sure if this is the right group to post this on, so sorry in advance if it isn't. I'm using the VS.NET 2005 beta, and am trying to fill one grid, based on what is selected in the...
8
by: Bryan Parkoff | last post by:
int has two bytes or four bytes. long has four bytes or eight bytes. I can't be sure to choose int or long keyword because I don't trust to get the wrong size. I always check by using...
11
by: vbgunz | last post by:
Hello all, I am just learning Python and have come across something I feel might be a bug. Please enlightenment me... The following code presents a challenge. How in the world do you provide an...
1
by: Sandra-24 | last post by:
I've always wondered why I can't do: def foo(a,b,c): return a,b,c args = range(2) foo(*args, c = 2) When you can do:
3
by: Redbeard | last post by:
Hi All this is my first time post, be gentle. I am looking at creating a keyword search that searches multiple fields in a Form and then filters records that match the keyword. The Form...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.