Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A little theory please: passing objects to functions

Status
Not open for further replies.

ghesse

Programmer
Jun 16, 2003
51
US
Hello,

I was just wondering how objects are handled when they are passed to a function. By default, is a copy made of the object inside of the function or are they passed by ref.

Thanks,
ghesse
 
There is no default "mode" to pass objects to a function.
In C, C++, C# , VB , COBOL (etc ...) you have to specify the "mode" when you declare the function.
Exemples:
1.
int iStored = 0;
int foo(int OneInteger, int *pToInteger) - first is by value the second is by reference
Usage : foo(108, &iStored)
2.
void foo1(DataTable dt, ref DataTable dt2) - first is by value e.g. a copy of the dt object is made on the local stack , 2nd is by reference e.g only the reference (pointer) of the dt2 object is copied on the local stack.

So, in both cases there is something copied on the stack but there is a huge difference between what is copied.

-obislavu-
 
Not to step on your toes obislavu, but C# doesn't work that way. It is much more like Java than C++ in terms of passing variables to functions.

In C# their are two types of types, object (reference, etc) types (string, DataTable, etc) and value types (int, float, any struct, etc). When an object type is passed to a function, the default is by reference, since all that you have is a reference anyway. This means any changes you make to the object remain after the function completes. [Be careful about tesing this with strings as they are immutable, meaning string functions don't change strings, they just make new ones.]

Value types are different, as they are copied when passed to function. In order to pass a value type by reference (without using unsafe pointers), you must use the ref modifier in the function prototype. This allows the function to make permanent modifications to the variable.

One final note, passing an object type with the ref modifier means that the actual variable itself can be changed, not just the object. The following example demonstrates:
Code:
StringBuilder temp = new StringBuilder("Hello World!");
foo(temp);
...
void foo (StringBuilder s) {
   s = new StringBuilder("Goodbye World!");
}
after foo, temp is still "Hello World!", becuase only the object can be modified. However:
Code:
StringBuilder temp = new StringBuilder("Hello World!");
foo(ref temp);
...
void foo (ref StringBuilder s) {
   s = new StringBuilder("Goodbye World!");
}
now after foo, temp is "Goodbye World!".

If you have any more questions, let me know.

"Programming is like sex, one mistake and you have to support it forever."

John

johnmc@mvmills.com
 
Hi John,
You are right about only two types in C# but I wanted to give an explanation about passing objects (built-in, values, types, structures, fields etc...) doesn't metter the language. Functions exist in all languages.
As you can see even in C# there are two methods of passing:
myfunction1 ( object o1, ref object o2)
myfunction2 (ref object o1, object o2)
myfunction3 (ref object o1, ref object o2)
myfunction4 (object o1, object o2)

I hope ghesse could confirm if he understood from what we are talking.

-obislavu-
 
Ok, so the people I've talked to tell me when a user created object is passed to a function a copy of that object's address is copied onto the stack. So in effect all user created objects are bassed by ref. Other objects in C# such as ints, strings..etc are bassed by value unless a ref is specified.

ghesse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top