473,787 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string parameters

A.M
Hi,

Are string parameters in functions by refrence or by value ?

Thanks,
Ali
Nov 15 '05 #1
8 2214
It's by value unless the specify the param is by ref

----- A.M wrote: ----

Hi

Are string parameters in functions by refrence or by value

Thanks
Al

Nov 15 '05 #2
Value

Parameters are passed in .NET by value unless you use a ref, or out in the
signature.

--
Jared Parsons [MSFT]
ja******@online .microsoft.com
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.c om/info/cpyright.htm"

"A.M" <IH*******@sapm 123.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Hi,

Are string parameters in functions by refrence or by value ?

Thanks,
Ali

Nov 15 '05 #3
A.M wrote:
Are string parameters in functions by refrence or by value ?


Unless marked otherwise, all parameters are by value. However, this does not
work the way it did in VB6 and below. The value is a duplicate reference to
an object, not a duplicate object.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #4
Hi Ali,

Thank you for posting in the community!

Based on my understanding, you want to know if the string class is pass to
a method by value or by reference.

=============== ===============
Just as the community stated, everything passed to a method is default by
value, unless it is marked by "ref" keyword, then it will by reference.

I want to point out that pass by value or by reference type has nothing to
do with the reference type or value type. That is, for reference type, it
also has 2 types: by value and by reference. Please see the 2 samples below:

1). Passing Reference Types by Value
using System;
class PassingRefByVal
{
static void Change(int[] arr)
{
arr[0]=888; // This change affects the original element.
arr = new int[5] {-3, -1, -2, -3, -4}; // This change is local.
Console.WriteLi ne("Inside the method, the first element is: {0}",
arr[0]);
}

public static void Main()
{
int[] myArray = {1,4,5};
Console.WriteLi ne("Inside Main, before calling the method, the first
element is: {0}", myArray [0]);
Change(myArray) ;
Console.WriteLi ne("Inside Main, after calling the method, the first
element is: {0}", myArray [0]);
}
}

Output
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: 888

2). Passing Reference Types by Reference
using System;
class PassingRefByRef
{
static void Change(ref int[] arr)
{
// Both of the following changes will affect the original variables:
arr[0]=888;
arr = new int[5] {-3, -1, -2, -3, -4};
Console.WriteLi ne("Inside the method, the first element is: {0}",
arr[0]);
}

public static void Main()
{
int[] myArray = {1,4,5};
Console.WriteLi ne("Inside Main, before calling the method, the first
element is: {0}", myArray [0]);
Change(ref myArray);
Console.WriteLi ne("Inside Main, after calling the method, the first
element is: {0}", myArray [0]);
}
}

Output
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: -3

~~~~~~~~~~~~
In the sample you will see that: for reference type by value, the
reference's content was not copied, but the reference variable self is
copied. So you can change the original reference variable's content, but
you can not change the reference variable itself.

So for string class without "ref" keyword, you "may" change the string
instance's content, but you can not change the string instance self.(That
is point to another string)
But, string class is a special class, whose content can not be modified. So
you can not do this:

string str="jeffrey";
f(str);

private void f(string str1)
{
str1[0]='a'; //this line will generate compile error
}

Hope I have clarified for you :-) For more information, please refer to:
"Passing Parameters"
http://msdn.microsoft.com/library/de...us/csref/html/
vclrfPassingMet hodParameters.a sp

=============== =============== =====
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5
Also make sure you are talking about string datatype and not about the class. If you use String class then it is pass by ref, if you use string datatype then by valu

----- A.M wrote: ----

Hi

Are string parameters in functions by refrence or by value

Thanks
Al

Nov 15 '05 #6
GajananK wrote:
Also make sure you are talking about string datatype and not about
the class. If you use String class then it is pass by ref, if you use
string datatype then by value


string is an alias for System.String.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #7

Hi,

In C#, string datatype is a one by one relation with System.String class,
so it is also a reference type. They are all default pass by value, for
more details, please refer to my reply in this post.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #8
A.M wrote:
Hi,

Are string parameters in functions by refrence or by value ?

Thanks,
Ali


Here's a _great_ article on parameter passing, which you should
definitely read: http://www.yoda.arachsys.com/csharp/parameters.html
Nov 15 '05 #9

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

Similar topics

9
3697
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
4
431
by: Niyazi | last post by:
Hi I am trying to insert some value into SQL Server 2000 tables and I am keep getting the "Input string was not in a correct format" error. When user fills the Form it updates the table call tbl_04_Hellenic. Then In that form some of data also goes and fills another table call tbl_LEHTAR. Here is my code:
2
1447
by: roopeman | last post by:
i need your help, my code as below : //----------------------------------------------------------------------- //Wrote by Michael April 30 2005 //----------------------------------------------------------------------- using System.Text; using System.Diagnostics; using System.Threading;
3
485
by: Dominique Vandensteen | last post by:
after the very small & vs string.format discussion I did some speed tests... loop of 1.000.000 concatenations of 5 public string variables in a class gave following results: result = a & b & c & d & e +/- 420ms
1
8276
by: Caroline | last post by:
I am trying to update a record though a stored procedure and parameters, but I keep getting this error: Additional information: Argument 'Prompt' cannot be converted to type 'String'. Any ideas? Here is the code
6
1275
by: Manuel Canas | last post by:
Hello there, this is the piece of code that if giving problems right now; cnSQL = New SqlConnection(ConnectionString) **** strSQL = "UPDATE tb_product SET (ProductID = @cProductID, Code = @Code, ServiceName = @ServiceName, Price = @Price " & _ "WHERE ProductID = @cProductID)" ****
8
5774
by: **Developer** | last post by:
Been reading some of the Marshal doc and am a little confused. Anyway, I've been using the following and it works OK. Public Declare Auto Function SHGetFolderPath Lib "shell32.dll" (ByVal hWnd As Integer, _ ByVal nFolder As Integer, ByVal nToken As Integer, _ ByVal dwFlags As Integer, _
16
2156
by: Mark A. Sam | last post by:
Hello, I am having a problem with imputting into a string variable: Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany, txtPhone, txtEmail, txtComment, chkGrower, chkProduceDealer, txtOtherCustType, chkStandardBags, chkCustomBags,txtOtherBags) " + _ "VALUES ('" + txtName.Text + "','" + txtCompany.Text + "','" + txtPhone.Text + "','" + txtEmail.Text + "','" + txtComment.Text + "','" +
3
1857
by: =?Utf-8?B?SmFuIEhlcHBlbg==?= | last post by:
Hi, I've a question. I'm developing a windows application and for the application i need to run functions and procedures that are stored in a database. Here is an example that i tried to get working. How can i execute the function that is stored in the string expr ?
15
1669
by: cj | last post by:
I've got a long string to be written to a field in a sql db table via stored procedure. For some reason I find only 258 chars are being written. Any ideas? relevant bits of my VB2005 code: mySqlConnection.ConnectionString = ".......... mySqlCommand.Connection = mySqlConnection mySqlCommand.CommandType = CommandType.StoredProcedure
0
9655
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
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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,...
0
9964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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
6749
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
5398
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...
1
4069
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 we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.