468,119 Members | 1,878 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

byval & byref by god are drving me nuts

Is there any way to pass parameters to a function and simply know there will
get there without the silly (C like ) complexity of worring about byval and
or perhaps byref ?

(Why bother to have two languages in the first place now ....silliness ; OOP
taken to dysfunctional absurdity ! This idea is too counter productive to
last; MS , your king has no clothes on ...)
Nov 21 '05 #1
7 1648

"barrett bonden" <ar**********@yahoo.com> wrote in message
news:KQ*****************@fe09.lga...
Is there any way to pass parameters to a function and simply know there will get there without the silly (C like ) complexity of worring about byval and or perhaps byref ?

(Why bother to have two languages in the first place now ....silliness ; OOP taken to dysfunctional absurdity ! This idea is too counter productive to
last; MS , your king has no clothes on ...)


um, I understand that you are frustrated. However, I don't quite understand
the question.
What problem are you experiencing?
Are you asking what the real difference between ByVal and ByRef are?
Help me help you, provide more info.

Gerald
Nov 21 '05 #2

"barrett bonden" <ar**********@yahoo.com> wrote in message
news:KQ*****************@fe09.lga...
Is there any way to pass parameters to a function and simply know there
will
get there without the silly (C like ) complexity of worring about byval
and
or perhaps byref ?

(Why bother to have two languages in the first place now ....silliness ;
OOP
taken to dysfunctional absurdity ! This idea is too counter productive to
last; MS , your king has no clothes on ...)


Public Static Sub Main()
Dim byValParam As Integer = 12345
Dim byRefParam As Integer = 12345

ByValMethod(byValParam)
ByRefMethod(byRefParam)

Console.WriteLine("ByVal Value After ByValMethod: " & byValParam)
Console.WriteLine("ByRef Value After ByRefMethod: " & byRefParam)
End Sub

Public Sub ByValMethod(ByVal MyParam As Integer)
Console.WriteLine("ByValMethod MyParam Value: " & MyParam)
MyParam = 234
End Sub

Public Sub ByRefMethod(ByRef MyParam As Integer)
Console.WriteLine("ByRefMethod MyParam Value: " & MyParam)
MyParam = 234
End Sub
This will display:

ByValMethod MyParam Value: 12345
ByRefMethod MyParam Value: 12345
ByVal Value After ByValMethod: 12345
ByRef Value After ByRefMethod: 234
Follow it through and see the results yourself (Console application).

HOpe this helps ;)

Mythran
Nov 21 '05 #3
Barret,

In addition to the other answers

One of the big differences between a value and an object is the passing.

A ByVal passes with a value the value
A ByVal passes with an object his reference in the value
A ByRef passes (boxed) the reference in both situation in the value

I hope this helps?

Cor
Nov 21 '05 #4
>A ByRef passes (boxed) the reference in both situation in the value

ByRef doesn't imply boxing.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #5
Mattias,

I will investigate it better to explain it in a better way. Although it
explains in this way very easy.

Thanks.

Cor
Nov 21 '05 #6
Barrett,

"barrett bonden" <ar**********@yahoo.com> schrieb:
Is there any way to pass parameters to a function and simply know there
will
get there without the silly (C like ) complexity of worring about byval
and
or perhaps byref ?


In VB.NET, 'ByVal' is the default (opposed to VB6, where 'ByRef' was the
default), so you don't need to explicitly specify it. 'ByVal' and 'ByRef'
serve slightly different purposes for value types and reference types:

When passing a variable to a method in a 'ByVal' parameter, the variable's
content is duplicated and the copy is passed to the method. Altering the
parameter variable inside the method won't change the variable you passed to
the method. On the other hand, 'ByRef' will pass a "reference" to the
variable to the method, and thus assigning another value to the parameter
variable inside the method will affect the value of the variable passed to
the method:

\\\
Dim i As Integer = 2
DoSomething1(i)
MsgBox(CStr(i)) ' "2".
i = 2
DoSomething2(i)
MsgBox(CStr(i)) ' "3".
..
..
..
Private Sub DoSomething1(ByVal i As Integer)
i = i + 1
End Sub

Private Sub DoSomething2(ByRef i As Integer)
i = i + 1
End Sub
///

For reference types (classes) there is also a difference between 'ByVal' and
'ByRef'. By passing a variable to a parameter that is marked as 'ByVal', a
copy of the reference stored in the variable is created and this reference
is passed to the method. Thus you cannot change the reference the variable
you passed to the method is pointing to inside the method ('DoSomething1').
When passing the variable 'ByRef', the variable's reference is exposed to
the method directly and can be manipulated by the method, which means that
the method can let the variable you passed to the method point to another
object. The sample below will demonstrate this behavior:

\\\
Dim sb As New StringBuilder("Foo")
DoSomething1(sb)
MsgBox(sb.ToString()) ' "Foo".
sb = New StringBuilder("Foo")
DoSomething2(sb)
MsgBox(sb.ToString()) ' "Bla".
..
..
..
Private Sub DoSomething1(ByVal s As StringBuilder)
s = New StringBuilder()
s.Append("Bla")
End Sub

Private Sub DoSomething2(ByRef s As StringBuilder)
s = New StringBuilder()
s.Append("Bla")
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7
Very kind; many thanks to you all - I'll spend (waste in frustration ?)
some time working your kindly posted examples through ----I've spent 15
years writing (SMALL ! SHORT ! ) applications in Dbase, MS Access, etc, and
earning a good living at it - and you know what I'll say next; without ever
needing OOP or pointers or stacks, etc - I've approached OPP and VB Net a
number of times now and in good faith I still feel and must say: to me it
seems like nothing more than silly, massively excessive layers of
complexity that only get in the way of productive code -that OPP is a fad,
and will go the way of Deconstruction in lit or radical Behaviorism in
psychology or Marxism -

While I've got you guys; how much penetration is VB net making ? Anywhere
near as much as VB6? And is it successful - are (perhaps forgive me )
people really writing large applications in it ?

sorry for the rant, and thanks again -
"barrett bonden" <ar**********@yahoo.com> wrote in message
news:KQ*****************@fe09.lga...
Is there any way to pass parameters to a function and simply know there will get there without the silly (C like ) complexity of worring about byval and or perhaps byref ?

(Why bother to have two languages in the first place now ....silliness ; OOP taken to dysfunctional absurdity ! This idea is too counter productive to
last; MS , your king has no clothes on ...)

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Sandy | last post: by
3 posts views Thread by Scott M. | last post: by
7 posts views Thread by Hei | last post: by
4 posts views Thread by Carlos Gomez | last post: by
2 posts views Thread by Witold Iwaniec via .NET 247 | last post: by
8 posts views Thread by Boni | last post: by
15 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.