Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Recently I have discovered there is an exception to the rules of ByVal and ByRef. The problem is, how do you identify a varible or object that is part of the exception? The one object that I have found to be part of the exception is the DataSet object. I have yet to see professionally written code that passes this object ByRef. Can someone explain to me the REAL rules of ByVal and ByRef? How do you identify objects that are part of the exception?
  • *Experts*
Posted

To understand when to use one, you need to know what's going on with the memory behind object and how they get passed to/from functions. Basically there are two types of variables in .NET: value type and reference type. Base types (int, char, decimal, etc.) and structs are value types, classes are reference types.

 

When you pass a reference type ByVal to a function, you are only passing a copy of the *pointer*, not the data inside. That means that if you change the data inside the function, it affects the object itself and when you return those changes can be seen. What you CAN'T do when passing a reference type as ByVal is change the pointer inside the function. So if you had a variable ds in Function1 that pointed to a DataSet and you pass it ByVal to Function2 and Function2 sets it to Nothing, ds will still point to the DataSet. If you were to remove a table inside Function2, it would be removed for good and you won't see it when you return to Function1.

 

Structs work differently. If you had a variable p that was type Point (in System.Drawing I think) and pass it ByVal to a function, it creates a copy of whatever data is in p and that's what you're working on inside Function2. If you change a value in Function2, it won't affect the variable p's data back in Function1. Now if you pass p ByRef, any change you make in Function2 will show up back in Function1.

 

Hope that helps,

ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Actually it does make sense... thank you.... So basically any class passed is in essence being passed by ref (that is to say any changes made to its "parts" affect the passed class).

 

Thanks

 

Phylum

  • 1 month later...
Posted

Remoting behaviour

 

This is a really strange behaviour. I feel whenever an object is passed from caller to callee without specifying 'ref' keyword, it should be passed by value, a copy of it should be made in callee and used.

 

So in nutshell, whenever you pass object irrespestive of hether it is by Ref or By Val, it will get modified if the callee modifies it.

 

However this behaviour is not true if the call goes through remoting and object that is passed to callee is marked serializable. In this case a copy of the object is made before passing it to remote method.

 

Regards

Ravindra Sadaphule

MCP(ASP.NET)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...