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. Using ByRef keyword

Using ByRef keyword

Scheduled Pinned Locked Moved Visual Basic
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.
  • C Offline
    C Offline
    Chris Quick
    wrote on last edited by
    #1

    I am writing a class that should have the ability to modify the members of another class. I am wondering if ByRef will allow this functionality. Thanks for any info...

    D S 2 Replies Last reply
    0
    • C Chris Quick

      I am writing a class that should have the ability to modify the members of another class. I am wondering if ByRef will allow this functionality. Thanks for any info...

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      ByRef can only be used in Sub and Function parameter passing. If ObjectB is passed to a method in ClassA ByRef, then that method can get to that instance of ObjectB.

      Dim b As Integer = 23
      ModifySomething(b)
      Debug.WriteLine("The value of b is: " & b.ToString())

      Private Sub ModifySomething(ByRef someInt As Integer)
      someInt = 99
      End Sub

      -----8<------------------------------------------------

      Output:
      The value of b is: 99

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      C 1 Reply Last reply
      0
      • D Dave Kreskowiak

        ByRef can only be used in Sub and Function parameter passing. If ObjectB is passed to a method in ClassA ByRef, then that method can get to that instance of ObjectB.

        Dim b As Integer = 23
        ModifySomething(b)
        Debug.WriteLine("The value of b is: " & b.ToString())

        Private Sub ModifySomething(ByRef someInt As Integer)
        someInt = 99
        End Sub

        -----8<------------------------------------------------

        Output:
        The value of b is: 99

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        C Offline
        C Offline
        Chris Quick
        wrote on last edited by
        #3

        So, is the following idea permitted?

        Public Class Form1
        Private Function SaveObject()
        Object.Save()
        End Private
        End Class

        Public Class Object
        ' ... members, properties and constructors ...

        Public Property DataID as Integer
              ' ... Get and Set the DataID
        End Property
        
        Public Sub Save()
           Dim DataSaver as New ObjectDataLogic
           DataSaver.Save(Me)
        End Sub
        

        End Class

        Public Class ObjectDataLogic
        ' ... members, properties and constructors ...
        Public Sub Save(ByRef Object as Object)
        ' ... save to the database and return get the new ID
        Object.DataID = NewID
        End Public
        End Class

        I want the data logic and the business object on two seperate tiers. Would this be considered good design or is there a better way to accomplish this? Any issues that could arise from this approach? I am seeking any additional resources that are out there! Thanks!

        1 Reply Last reply
        0
        • C Chris Quick

          I am writing a class that should have the ability to modify the members of another class. I am wondering if ByRef will allow this functionality. Thanks for any info...

          S Offline
          S Offline
          Steven Campbell
          wrote on last edited by
          #4

          Passing objects byref is pointless 99% of the time. You do not need to pass byref in order to be able to modify the object properties. It is the most misused keyword in VB, which is why the VB.NET team switched the default from ByRef to ByVal.


          my blog

          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