473,703 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1887

"barrett bonden" <ar**********@y ahoo.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**********@y ahoo.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(byV alParam)
ByRefMethod(byR efParam)

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

Public Sub ByValMethod(ByV al MyParam As Integer)
Console.WriteLi ne("ByValMetho d MyParam Value: " & MyParam)
MyParam = 234
End Sub

Public Sub ByRefMethod(ByR ef MyParam As Integer)
Console.WriteLi ne("ByRefMetho d 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**********@y ahoo.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(By Val i As Integer)
i = i + 1
End Sub

Private Sub DoSomething2(By Ref 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.ToStr ing()) ' "Foo".
sb = New StringBuilder(" Foo")
DoSomething2(sb )
MsgBox(sb.ToStr ing()) ' "Bla".
..
..
..
Private Sub DoSomething1(By Val s As StringBuilder)
s = New StringBuilder()
s.Append("Bla")
End Sub

Private Sub DoSomething2(By Ref 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**********@y ahoo.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
383
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef points to something and changes it. The default for simple data types is ByVal and for objects, it's ByRef. Am I correct so far? If so, I still don't have a clue as
3
6609
by: Scott M. | last post by:
If I pass a reference type ByVal, am I making a copy of the object on the heap or am I making a copy of a pointer to the object on the heap? If I pass a string object (reference type) into a sub ByVal and modify it, the original string doesn't change. If I pass it ByRef it does. This makes it seem like the actual type (reference or value) that is being passed is irrelevant and that only the ByVal or ByRef keyword matters. Is this...
7
5504
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
4
12313
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from changing the original variable passed)
14
2508
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered allows me to store a reference to the object I passed by val, to change that object and for the change to be reflected in the callers copy of the reference (confused?). Well, what is byref for in that case? I'm coming from a C++ background...
14
1531
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed something? Regards /Niklas Public Class Main Shared Sub Main() Dim testPropObj As New MyPropertObject testPropObj.MyInt = 1
4
2467
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive, as I expected. I haven't seen this behavior explained precisely in the .net world yet, so I wanted to check and make sure I've got it right. I apologize that this is a bit long. I've tried to keep it concise. There are code fragments here, but...
2
10271
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not Public Class ITest Private MyName As String Public TestId As String Public Sub New()
8
2211
by: Boni | last post by:
Dear all, I found out that I don' understand byVal/byRef in VB. There is a simple example: Why in the first test the result is 10,10 where in the second 0,20. Thanks for your help. Boni Module Module1 Class A
0
8756
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9014
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7861
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4429
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2063
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.