473,396 Members | 2,055 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,396 software developers and data experts.

Parameters byref

I hace a function that needs to return two integers.
I know that objects can be passed by reference and updated in a function.
Can I do the same thing with primitive value type parameters?

--
Arne Garvander
(I program VB.Net for fun and C# to get paid.)
Jun 23 '06 #1
3 57947
Arne Garvander wrote:
I hace a function that needs to return two integers.
I know that objects can be passed by reference and updated in a function.
Can I do the same thing with primitive value type parameters?


Yes, just define the param as a ref int.
--
Tom Porterfield
Jun 23 '06 #2
Arne Garvander wrote:
I hace a function that needs to return two integers.
I know that objects can be passed by reference and updated in a function.
Can I do the same thing with primitive value type parameters?


Arne,

You can pass in integers by reference. In C#:

private void UpdateInts(ref int one, ref int two)
{
}

However, depending on the purchase of your function, you may want to
consider returning an integer array with both integers.

Another option is to use output parameters. In C#:
private void UpdateInts(int something, out int one, out int two)
{
}

This will output two new values instead of updating the referenced integer.

Hope this helps.

Dan Manges
Jun 23 '06 #3
Arne Garvander wrote:
I hace a function that needs to return two integers.
I know that objects can be passed by reference and updated in a function.
Can I do the same thing with primitive value type parameters?


Hi Arne,

C# has two parameter modifiers to allow you to assign values to referenced
variables. Those modifiers are 'out' and 'ref'. There is a subtle
difference in that 'ref' is used to update the value of a parameter, i.e.
it MUST be assigned a value when it is passed in to the method, and the
method doesn't necessarily have to update it. The 'out' modifier is used
when a value SHOULDN'T be assigned when you pass the parameter in, and the
method MUST assign a value to it before it returns:

///
private void MyTestMethod ( )
{
int a, b;

MyOutMethod( out a, out b );
MyRefMethod( ref a, ref b );

Console.WriteLine( "A: {0}\nB: {1}", a, b );
}

private void MyRefMethod ( ref int a, ref int b )
{
a++;
b--;
}

private void MyOutMethod ( out int a, out int b )
{
a = 5;
b = 10;
}
///

I've totally just blitzed out that code without testing, but if all goes
according to plan, the output should be:

A: 6
B: 9

Hope this helps,
-- Tom Spink
Jun 23 '06 #4

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

Similar topics

8
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef...
10
by: Logico | last post by:
Hi everybody, I've tried to use the byref keyword for passing arguments to subroutines and functions in my ASP pages with VBScript, but it seems that both byref and byval are irrilevant, as simple...
6
by: Cc | last post by:
hi, is there a way to use byref on property set , because i would like to pass the value into the variable byref ?
7
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
19
by: Rob Panosh | last post by:
Hello, Ok here is the senerio: ..... Dim myArrayList as New ArrayList(0) me.Test_A( myArrayList )
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
3
by: tinman | last post by:
Hi.... Assume Function A in an application calls Function GetSomeData in another assembly..... which then is the prefered method to return the SqlDatareader object back to Function A (and why...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
9
by: Samuel Shulman | last post by:
Hi I wander there is a way to return ByRef just like passing ByRef What I want to achieve is the following: Call a method that returns an object Use that call as an argument to a ByRef...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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.