byval & byref by god are drving me nuts

  • Thread starter Thread starter barrett bonden
  • Start date Start date
B

barrett bonden

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 ...)
 
barrett bonden said:
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
 
barrett bonden said:
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
 
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
 
A ByRef passes (boxed) the reference in both situation in the value

ByRef doesn't imply boxing.



Mattias
 
Mattias,

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

Thanks.

Cor
 
Barrett,

barrett bonden said:
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
///
 
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 -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top