John Oliver (UK)MSP, VSIP

Hi ALL,

This shows the difference of passing ByVal or ByRef with a simple example.

Just add two buttons to a FORM along with this code.

PASTE it in if you like.

Regards,

S_DS

____________________________________

Private Sub Button1_Click _

(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

Dim someInteger = 10

'Of course ByRef makes a difference.

'The following CALL alters someInteger value to 7

'output is

'someInteger is now = 7'

'ByRef changes the original Objects value(s)

'if they are changed in a Sub or Function or another CLASS

'as the OBJECT itself is passed so watch out!!

Call AltersIntegerValue(someInteger)

MessageBox.Show("someInteger is now = " & someInteger.ToString)

End Sub

Private Sub Button2_Click _

(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button2.Click

Dim someInteger = 10

'ByVal of course DOES NOT alter this Sub's

'someInteger value this Sub still produces

'someInteger is still = 10'

'as ByVal passes a 'copy' of an object

'not the object itself.

Call DontAlterIntegerValue(someInteger)

MessageBox.Show("someInteger is still = " & someInteger.ToString)

End Sub

Private Sub AltersIntegerValue(ByRef myInt As Integer)

myInt = 7

End Sub

Private Sub DontAlterIntegerValue(ByVal myInt As Integer)

myInt = 7

End Sub




Re: Visual Basic Language ByVal and ByRef differences are demonstrated here.

kleinma

You should probably comment on the fact that there are really 4 true ways, not just 2 for passing ByVal or ByRef because value and reference types act differently when passed byval or byref.

Using integers you are showing how value types work when passed ByRef and ByVal, however if you pass a reference type (like your own custom class), you are only passing the pointer to the memory where your object is ByVal or ByRef, the actual object is still the same spot in memory, and can be modified in the calling routine, even if passed ByVal...






Re: Visual Basic Language ByVal and ByRef differences are demonstrated here.

Spidermans_DarkSide - VSIP

kleinma wrote:

You should probably comment on the fact that there are really 4 true ways, not just 2 for passing ByVal or ByRef because value and reference types act differently when passed byval or byref.

Using integers you are showing how value types work when passed ByRef and ByVal, however if you pass a reference type (like your own custom class), you are only passing the pointer to the memory where your object is ByVal or ByRef, the actual object is still the same spot in memory, and can be modified in the calling routine, even if passed ByVal...

Hi,

I believe you have just answered my question at

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1618581&SiteID=1

Regards,

S_DS






Re: Visual Basic Language ByVal and ByRef differences are demonstrated here.

kleinma

I believe I have

It is a bit confusing at first since you expect anything passed ByRef or ByVal to do just that...

However once you understand the value and reference types, it becomes quite clear






Re: Visual Basic Language ByVal and ByRef differences are demonstrated here.

Spidermans_DarkSide - VSIP

Hi,

I guess a value type is anything holding a value such as a;

  • Byte
  • Decimal
  • Double
  • Int16
  • Int32
  • Int64
  • Single
  • String
  • UInt16
  • UInt32
  • UInt64

are there any others

Is there a Microsoft web page detailing the range of these TYPEs in both 32 bit and 64 bit environments please

How many characters can a string hold I think it is 65535 or is it 65536 with a zero indexed array

A reference type is 'anything else'

Please confirm for the benefit of everyone else, including beginners.

I'm still learning too!!

Regards,

S_DS






Re: Visual Basic Language ByVal and ByRef differences are demonstrated here.

kleinma

strings can hold a lot more than 65535 chars in .NET, I believe it actually can vary on a given system based on the amount of memory available. Needless to say, you can generally make VERY LARGE strings...

Here is info from MSDN documentation on value and reference types:

data type is a value type if it holds the data within its own memory allocation. A reference type contains a pointer to another memory location that holds the data.

Value Types

Value types include the following:

  • All numeric data types

  • Boolean, Char, and Date

  • All structures, even if their members are reference types

  • Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong

Reference Types

Reference types include the following:

  • String

  • All arrays, even if their elements are value types

  • Class types, such as Form

  • Delegates

Elements That Are Not Types

The following programming elements do not qualify as types, because you cannot specify any of them as a data type for a declared element:

  • Namespaces

  • Modules

  • Events

  • Properties and procedures

  • Variables, constants, and fields






Re: Visual Basic Language ByVal and ByRef differences are demonstrated here.

kleinma

ok found some specific documentation on the max string length..

Holds sequences of unsigned 16-bit (2-byte) code points ranging in value from 0 through 65535. Each code point, or character code, represents a single Unicode character. A string can contain from 0 to approximately 2 billion (2 ^ 31) Unicode characters.






Re: Visual Basic Language ByVal and ByRef differences are demonstrated here.

Spidermans_DarkSide - VSIP

Hiya again,

2 ^ 31 is 2,147,483,648

In England i believe 1000 million is not a billion but in the USA it is.

See http://en.wikipedia.org/wiki/Billion

Regards,

S_DS






Re: Visual Basic Language ByVal and ByRef differences are demonstrated here.

Chris Dunaway

Hmmm...

I never realized that other countries had different names for the numbers. Here (US), 1 billion = 1000 million and 1 trillion = 1000 billion and I believe 1 quadrillion = 1000 trillion.

By the way, can you loan me 1 quadrillion dollars I'd sure appreciate it!! Smile

Back to the subject, here is a good article about parameter passing. The code is C# but the concepts are the same:

http://www.yoda.arachsys.com/csharp/parameters.html

Chris





Re: Visual Basic Language ByVal and ByRef differences are demonstrated here.

Spidermans_DarkSide - VSIP

Chris Dunaway wrote:

Hmmm...

I never realized that other countries had different names for the numbers. Here (US), 1 billion = 1000 million and 1 trillion = 1000 billion and I believe 1 quadrillion = 1000 trillion.

By the way, can you loan me 1 quadrillion dollars I'd sure appreciate it!! Smile

Back to the subject, here is a good article about parameter passing. The code is C# but the concepts are the same:

http://www.yoda.arachsys.com/csharp/parameters.html

Chris

Hi Chris,

How about "Monopoly game money"

I'm waiting for Santa to fetch a 10Yib hard drive for me.

That is a 10 YobiByte hard-drive. That would be 10 * ( 2 ^ 20 ) bytes.

See here about a YobiByte.>> http://en.wikipedia.org/wiki/Yobibyte

See todays featured picture and it's article here.>> http://en.wikipedia.org/wiki/Aerogel

I only know BASIC, a bit of early VB ( before .Net ), a bit of Motorola 6809 8 bit assembly language and "snippets" of Pascal remain in my physical memory.

I may have to read a book on any flavour of C whether it is C++ or C#.

Oh i am also a 40+ year old mature student, nearly at the end of a university course, trying to get a better job.

So apart from being a student level VSIP ( Visual Studio Industry Partner ) ,i am going to apply to be an MSP= Microsoft Student Partner.

I will still look at the url link you have posted though.

Regards,

S_DS