Results 1 to 6 of 6

Thread: ByVal / ByRef Question

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    ByVal / ByRef Question

    Riddle me this:

    Why is it that a method that accepts a ByVal textbox actually changes the textbox as if it was passed in ByRef? Are UI controls automatically ByRef?

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    As far as I now, Objects are always Byref, regardless of declaration, whereas Integer and string and the like depends on Byref and Byval.

    Ofcourse, I may be wrong (It has happend before.. ), but I'm pretty convinced.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Originally posted by pax
    Hi.

    As far as I now, Objects are always Byref, regardless of declaration, whereas Integer and string and the like depends on Byref and Byval.

    Ofcourse, I may be wrong (It has happend before.. ), but I'm pretty convinced.

    Yes, the textbox control is a reference type. Also, not a big deal, but, a string is a reference type. Many of the type's methods are overloaded to make is seem like a value type. This is why you read and hear so often that it is expensive to build strings (concatenation).

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI,

    "As far as I now, Objects are always Byref, regardless of declaration, whereas Integer and string and the like depends on Byref and Byval"

    I thought variables ARE objects???
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    .NET Types are split up into two categories:

    • Reference Types
    • Value Types


    The difference between the two is where they are placed in memory. Value types are placed on the stack and reference types are placed on the managed heap, which is watched and cleaned-up by the garbage collector.

    HTH

  6. #6

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    But a string is a reference type and I can pass that into a method ByVal and the original is not changed. Why does the original change for the textbox when passed in by val? Wouldn't it make a copy instead of another pointer to the same memory location?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width