Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Confused about ByVal

Confused about ByVal

Scheduled Pinned Locked Moved Visual Basic
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Brydon
    wrote on last edited by
    #1

    Hi, I'm a little confused about sending a variable into a function ByVal. I've always understood it that if you send it by value the program will make a copy of the variable and anything you do to that variable inside the function will not affect the original one. Thing is my code seems to contradict this. (i hate it when people post loads of code so i've simplified it)

    class SomeClass

    ' a ticket has a property .Message which is a Mymailer.Message
    private ticket as MyMailer.Ticket

    private function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
    message.Subject = "Test subject"
    message.Body = "This is a test message"
    return message
    end function

    private sub SomeSubroutine()
    dim newMessage as MyMailer.Message = DoMailMerge(ticket.Message)
    end sub

    end class

    for some reason when i call SomeSubroutine() the message contained in the variable 'ticket' is changed, even though i passed it by value. can anyone see why the variable ticket would be affected? it works if the DoSomething function is:

    private function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
    dim subject as string = "TestSubject"
    dim body as string = "This is a test message"

    dim newmessage as new MyMailer.Message
    newmessage.Subject = subject
    newmessage.Body = body
    return newmessage
    end function

    kevin

    C M 2 Replies Last reply
    0
    • K Kevin Brydon

      Hi, I'm a little confused about sending a variable into a function ByVal. I've always understood it that if you send it by value the program will make a copy of the variable and anything you do to that variable inside the function will not affect the original one. Thing is my code seems to contradict this. (i hate it when people post loads of code so i've simplified it)

      class SomeClass

      ' a ticket has a property .Message which is a Mymailer.Message
      private ticket as MyMailer.Ticket

      private function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
      message.Subject = "Test subject"
      message.Body = "This is a test message"
      return message
      end function

      private sub SomeSubroutine()
      dim newMessage as MyMailer.Message = DoMailMerge(ticket.Message)
      end sub

      end class

      for some reason when i call SomeSubroutine() the message contained in the variable 'ticket' is changed, even though i passed it by value. can anyone see why the variable ticket would be affected? it works if the DoSomething function is:

      private function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
      dim subject as string = "TestSubject"
      dim body as string = "This is a test message"

      dim newmessage as new MyMailer.Message
      newmessage.Subject = subject
      newmessage.Body = body
      return newmessage
      end function

      kevin

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If your object is a reference type ( a class ) then ByVal means nothing, it's always ByRef

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      • K Kevin Brydon

        Hi, I'm a little confused about sending a variable into a function ByVal. I've always understood it that if you send it by value the program will make a copy of the variable and anything you do to that variable inside the function will not affect the original one. Thing is my code seems to contradict this. (i hate it when people post loads of code so i've simplified it)

        class SomeClass

        ' a ticket has a property .Message which is a Mymailer.Message
        private ticket as MyMailer.Ticket

        private function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
        message.Subject = "Test subject"
        message.Body = "This is a test message"
        return message
        end function

        private sub SomeSubroutine()
        dim newMessage as MyMailer.Message = DoMailMerge(ticket.Message)
        end sub

        end class

        for some reason when i call SomeSubroutine() the message contained in the variable 'ticket' is changed, even though i passed it by value. can anyone see why the variable ticket would be affected? it works if the DoSomething function is:

        private function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
        dim subject as string = "TestSubject"
        dim body as string = "This is a test message"

        dim newmessage as new MyMailer.Message
        newmessage.Subject = subject
        newmessage.Body = body
        return newmessage
        end function

        kevin

        M Offline
        M Offline
        MikeMarq
        wrote on last edited by
        #3

        Objects and arrays are reference types. Basically an object is a pointer to a structure and the name of the object is essentially a pointer/reference. So when you pass them byval the compiler makes a copy of the pointer/reference but both copies point to the same "structure". That's why any change made by one will affect the other.

        K 1 Reply Last reply
        0
        • M MikeMarq

          Objects and arrays are reference types. Basically an object is a pointer to a structure and the name of the object is essentially a pointer/reference. So when you pass them byval the compiler makes a copy of the pointer/reference but both copies point to the same "structure". That's why any change made by one will affect the other.

          K Offline
          K Offline
          Kevin Brydon
          wrote on last edited by
          #4

          Thanks Christian and Mike. Now I understand!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups