Get/Set sections of a class property confusing...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy B

    Get/Set sections of a class property confusing...

    I saw this in the set accessor of a property:

    Set(ByVal value As DataSet)



    What exactly does the stuff in the () mean? VS complained about it not being
    there when I took it out not knowing it needed to be there.






  • Tom Shelton

    #2
    Re: Get/Set sections of a class property confusing...

    On 2008-05-29, Andy B <a_borka@sbcglo bal.netwrote:
    I saw this in the set accessor of a property:
    >
    Set(ByVal value As DataSet)
    >
    >
    >
    What exactly does the stuff in the () mean? VS complained about it not being
    there when I took it out not knowing it needed to be there.
    >
    >
    >
    >
    >
    >
    Well... Under the covers, get and set accessors are methods. The
    argument to the Set part of the property, is the value you want to pass
    in. Properties are syntactic sugar, really.

    Basically this:

    Class SomeClass
    Private _data As DataSet

    Public Property Data As DataSet
    Get
    Return Me._data
    End Get
    Set (ByVal value As DataSet)
    Me._data = value
    End Set
    End Property
    End Class

    Dim c As SomeClass = New SomeClass()
    c.Data = New DataSet()

    Turns into is something like (warning, this is psuedo code for
    illustration purposes only!):

    Class SomeClass
    Private _data As DataSet

    Public Function GetData () As DataSet
    Return Me._data
    End Function

    Public Sub SetData (ByVal value As DataSet)
    Me._data = value
    End Sub
    End Class

    Dim c As SomeClass = New SomeClass()
    c.SetData (New DataSet())

    Of course, the compiler immits IL, not basic code - but that is
    essentially what the il is doing. It is creating a get/set pair of
    methods and calling them :)


    --
    Tom Shelton

    Comment

    • Andy B

      #3
      Re: Get/Set sections of a class property confusing...

      Makes sense. This brings up another question: When do you use the "ByVal"
      keyword?


      "Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in message
      news:eyDLtQewIH A.5892@TK2MSFTN GP02.phx.gbl...
      On 2008-05-29, Andy B <a_borka@sbcglo bal.netwrote:
      >I saw this in the set accessor of a property:
      >>
      >Set(ByVal value As DataSet)
      >>
      >>
      >>
      >What exactly does the stuff in the () mean? VS complained about it not
      >being
      >there when I took it out not knowing it needed to be there.
      >>
      >>
      >>
      >>
      >>
      >>
      >
      Well... Under the covers, get and set accessors are methods. The
      argument to the Set part of the property, is the value you want to pass
      in. Properties are syntactic sugar, really.
      >
      Basically this:
      >
      Class SomeClass
      Private _data As DataSet
      >
      Public Property Data As DataSet
      Get
      Return Me._data
      End Get
      Set (ByVal value As DataSet)
      Me._data = value
      End Set
      End Property
      End Class
      >
      Dim c As SomeClass = New SomeClass()
      c.Data = New DataSet()
      >
      Turns into is something like (warning, this is psuedo code for
      illustration purposes only!):
      >
      Class SomeClass
      Private _data As DataSet
      >
      Public Function GetData () As DataSet
      Return Me._data
      End Function
      >
      Public Sub SetData (ByVal value As DataSet)
      Me._data = value
      End Sub
      End Class
      >
      Dim c As SomeClass = New SomeClass()
      c.SetData (New DataSet())
      >
      Of course, the compiler immits IL, not basic code - but that is
      essentially what the il is doing. It is creating a get/set pair of
      methods and calling them :)
      >
      >
      --
      Tom Shelton

      Comment

      • Tom Dacon

        #4
        Re: Get/Set sections of a class property confusing...

        Andy, this looks like it would be a good time for you to get a good basic
        book on VB and spend a few days studying it. It'd be a lot faster than
        asking one question at a time.

        Tom Dacon
        Dacon Software Consulting

        "Andy B" <a_borka@sbcglo bal.netwrote in message
        news:ex90tYewIH A.5288@TK2MSFTN GP06.phx.gbl...
        Makes sense. This brings up another question: When do you use the "ByVal"
        keyword?
        >

        Comment

        • Jack Jackson

          #5
          Re: Get/Set sections of a class property confusing...

          99.999% of the time you will use the default of ByVal.

          With ByVal, changes made to the parameter are not reflected in the
          caller. With ByRef, changes made to the parameter are seen by the
          caller.

          Consider these methods:

          Public Sub MyMethod1(ByVal ds As DataSet)
          ds = Nothing
          End Sub

          Public Sub MyMethod2(ByRef ds As DataSet)
          ds = Nothing
          End Sub

          Dim ds As New DataSet

          MyMethod1(ds)
          ' ds still has a reference to the DataSet
          ' allocated in the Dim statement

          MyMethod2(ds)
          ' ds is now Nothing

          It is important to distinguish the parameter from properties of the
          object being passed. ByVal does not prevent the called method from
          changing properties of the object.

          On Thu, 29 May 2008 19:46:23 -0400, "Andy B" <a_borka@sbcglo bal.net>
          wrote:
          >Makes sense. This brings up another question: When do you use the "ByVal"
          >keyword?
          >
          >
          >"Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in message
          >news:eyDLtQewI HA.5892@TK2MSFT NGP02.phx.gbl.. .
          >On 2008-05-29, Andy B <a_borka@sbcglo bal.netwrote:
          >>I saw this in the set accessor of a property:
          >>>
          >>Set(ByVal value As DataSet)
          >>>
          >>>
          >>>
          >>What exactly does the stuff in the () mean? VS complained about it not
          >>being
          >>there when I took it out not knowing it needed to be there.
          >>>
          >>>
          >>>
          >>>
          >>>
          >>>
          >>
          >Well... Under the covers, get and set accessors are methods. The
          >argument to the Set part of the property, is the value you want to pass
          >in. Properties are syntactic sugar, really.
          >>
          >Basically this:
          >>
          >Class SomeClass
          >Private _data As DataSet
          >>
          >Public Property Data As DataSet
          >Get
          >Return Me._data
          >End Get
          >Set (ByVal value As DataSet)
          >Me._data = value
          >End Set
          >End Property
          >End Class
          >>
          >Dim c As SomeClass = New SomeClass()
          >c.Data = New DataSet()
          >>
          >Turns into is something like (warning, this is psuedo code for
          >illustration purposes only!):
          >>
          >Class SomeClass
          >Private _data As DataSet
          >>
          >Public Function GetData () As DataSet
          >Return Me._data
          >End Function
          >>
          >Public Sub SetData (ByVal value As DataSet)
          >Me._data = value
          >End Sub
          >End Class
          >>
          >Dim c As SomeClass = New SomeClass()
          >c.SetData (New DataSet())
          >>
          >Of course, the compiler immits IL, not basic code - but that is
          >essentially what the il is doing. It is creating a get/set pair of
          >methods and calling them :)
          >>
          >>
          >--
          >Tom Shelton
          >

          Comment

          • Andy B

            #6
            Re: Get/Set sections of a class property confusing...

            Makes sense. so, using ByRef makes anything outside the method/property see
            the new value of the object when the method gets done with it. The other is
            only changed for the scope of the method/property. Properties of the passed
            object can be changed for the external code even when ByVal is used.



            "Jack Jackson" <jjackson@cinno vations.netwrot e in message
            news:orhu34tli3 10dnrqh5rn7ier9 5r3u9rn8q@4ax.c om...
            99.999% of the time you will use the default of ByVal.
            >
            With ByVal, changes made to the parameter are not reflected in the
            caller. With ByRef, changes made to the parameter are seen by the
            caller.
            >
            Consider these methods:
            >
            Public Sub MyMethod1(ByVal ds As DataSet)
            ds = Nothing
            End Sub
            >
            Public Sub MyMethod2(ByRef ds As DataSet)
            ds = Nothing
            End Sub
            >
            Dim ds As New DataSet
            >
            MyMethod1(ds)
            ' ds still has a reference to the DataSet
            ' allocated in the Dim statement
            >
            MyMethod2(ds)
            ' ds is now Nothing
            >
            It is important to distinguish the parameter from properties of the
            object being passed. ByVal does not prevent the called method from
            changing properties of the object.
            >
            On Thu, 29 May 2008 19:46:23 -0400, "Andy B" <a_borka@sbcglo bal.net>
            wrote:
            >
            >>Makes sense. This brings up another question: When do you use the "ByVal"
            >>keyword?
            >>
            >>
            >>"Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in message
            >>news:eyDLtQew IHA.5892@TK2MSF TNGP02.phx.gbl. ..
            >>On 2008-05-29, Andy B <a_borka@sbcglo bal.netwrote:
            >>>I saw this in the set accessor of a property:
            >>>>
            >>>Set(ByVal value As DataSet)
            >>>>
            >>>>
            >>>>
            >>>What exactly does the stuff in the () mean? VS complained about it not
            >>>being
            >>>there when I took it out not knowing it needed to be there.
            >>>>
            >>>>
            >>>>
            >>>>
            >>>>
            >>>>
            >>>
            >>Well... Under the covers, get and set accessors are methods. The
            >>argument to the Set part of the property, is the value you want to pass
            >>in. Properties are syntactic sugar, really.
            >>>
            >>Basically this:
            >>>
            >>Class SomeClass
            >>Private _data As DataSet
            >>>
            >>Public Property Data As DataSet
            >>Get
            >>Return Me._data
            >>End Get
            >>Set (ByVal value As DataSet)
            >>Me._data = value
            >>End Set
            >>End Property
            >>End Class
            >>>
            >>Dim c As SomeClass = New SomeClass()
            >>c.Data = New DataSet()
            >>>
            >>Turns into is something like (warning, this is psuedo code for
            >>illustratio n purposes only!):
            >>>
            >>Class SomeClass
            >>Private _data As DataSet
            >>>
            >>Public Function GetData () As DataSet
            >>Return Me._data
            >>End Function
            >>>
            >>Public Sub SetData (ByVal value As DataSet)
            >>Me._data = value
            >>End Sub
            >>End Class
            >>>
            >>Dim c As SomeClass = New SomeClass()
            >>c.SetData (New DataSet())
            >>>
            >>Of course, the compiler immits IL, not basic code - but that is
            >>essentially what the il is doing. It is creating a get/set pair of
            >>methods and calling them :)
            >>>
            >>>
            >>--
            >>Tom Shelton
            >>

            Comment

            • Jack Jackson

              #7
              Re: Get/Set sections of a class property confusing...

              Yes. ByRef can be used as a way to return more information than just
              using the return value of a function. However, I don't like to do
              that because it may not be obvious to someone reading the code that
              some parameters to a method call may be modified by the call.

              On Thu, 29 May 2008 21:04:24 -0400, "Andy B" <a_borka@sbcglo bal.net>
              wrote:
              >Makes sense. so, using ByRef makes anything outside the method/property see
              >the new value of the object when the method gets done with it. The other is
              >only changed for the scope of the method/property. Properties of the passed
              >object can be changed for the external code even when ByVal is used.
              >
              >
              >
              >"Jack Jackson" <jjackson@cinno vations.netwrot e in message
              >news:orhu34tli 310dnrqh5rn7ier 95r3u9rn8q@4ax. com...
              >99.999% of the time you will use the default of ByVal.
              >>
              >With ByVal, changes made to the parameter are not reflected in the
              >caller. With ByRef, changes made to the parameter are seen by the
              >caller.
              >>
              >Consider these methods:
              >>
              >Public Sub MyMethod1(ByVal ds As DataSet)
              > ds = Nothing
              >End Sub
              >>
              >Public Sub MyMethod2(ByRef ds As DataSet)
              > ds = Nothing
              >End Sub
              >>
              >Dim ds As New DataSet
              >>
              >MyMethod1(ds )
              >' ds still has a reference to the DataSet
              >' allocated in the Dim statement
              >>
              >MyMethod2(ds )
              >' ds is now Nothing
              >>
              >It is important to distinguish the parameter from properties of the
              >object being passed. ByVal does not prevent the called method from
              >changing properties of the object.
              >>
              >On Thu, 29 May 2008 19:46:23 -0400, "Andy B" <a_borka@sbcglo bal.net>
              >wrote:
              >>
              >>>Makes sense. This brings up another question: When do you use the "ByVal"
              >>>keyword?
              >>>
              >>>
              >>>"Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in message
              >>>news:eyDLtQe wIHA.5892@TK2MS FTNGP02.phx.gbl ...
              >>>On 2008-05-29, Andy B <a_borka@sbcglo bal.netwrote:
              >>>>I saw this in the set accessor of a property:
              >>>>>
              >>>>Set(ByVal value As DataSet)
              >>>>>
              >>>>>
              >>>>>
              >>>>What exactly does the stuff in the () mean? VS complained about it not
              >>>>being
              >>>>there when I took it out not knowing it needed to be there.
              >>>>>
              >>>>>
              >>>>>
              >>>>>
              >>>>>
              >>>>>
              >>>>
              >>>Well... Under the covers, get and set accessors are methods. The
              >>>argument to the Set part of the property, is the value you want to pass
              >>>in. Properties are syntactic sugar, really.
              >>>>
              >>>Basically this:
              >>>>
              >>>Class SomeClass
              >>>Private _data As DataSet
              >>>>
              >>>Public Property Data As DataSet
              >>>Get
              >>>Return Me._data
              >>>End Get
              >>>Set (ByVal value As DataSet)
              >>>Me._data = value
              >>>End Set
              >>>End Property
              >>>End Class
              >>>>
              >>>Dim c As SomeClass = New SomeClass()
              >>>c.Data = New DataSet()
              >>>>
              >>>Turns into is something like (warning, this is psuedo code for
              >>>illustrati on purposes only!):
              >>>>
              >>>Class SomeClass
              >>>Private _data As DataSet
              >>>>
              >>>Public Function GetData () As DataSet
              >>>Return Me._data
              >>>End Function
              >>>>
              >>>Public Sub SetData (ByVal value As DataSet)
              >>>Me._data = value
              >>>End Sub
              >>>End Class
              >>>>
              >>>Dim c As SomeClass = New SomeClass()
              >>>c.SetData (New DataSet())
              >>>>
              >>>Of course, the compiler immits IL, not basic code - but that is
              >>>essentiall y what the il is doing. It is creating a get/set pair of
              >>>methods and calling them :)
              >>>>
              >>>>
              >>>--
              >>>Tom Shelton
              >>>
              >

              Comment

              • rowe_newsgroups

                #8
                Re: Get/Set sections of a class property confusing...

                On May 29, 9:04 pm, "Andy B" <a_bo...@sbcglo bal.netwrote:
                Makes sense. so, using ByRef makes anything outside the method/property see
                the new value of the object when the method gets done with it. The other is
                only changed for the scope of the method/property. Properties of the passed
                object can be changed for the external code even when ByVal is used.
                >
                "Jack Jackson" <jjack...@cinno vations.netwrot e in message
                >
                news:orhu34tli3 10dnrqh5rn7ier9 5r3u9rn8q@4ax.c om...
                >
                99.999% of the time you will use the default of ByVal.
                >
                With ByVal, changes made to the parameter are not reflected in the
                caller. With ByRef, changes made to the parameter are seen by the
                caller.
                >
                Consider these methods:
                >
                Public Sub MyMethod1(ByVal ds As DataSet)
                ds = Nothing
                End Sub
                >
                Public Sub MyMethod2(ByRef ds As DataSet)
                ds = Nothing
                End Sub
                >
                Dim ds As New DataSet
                >
                MyMethod1(ds)
                ' ds still has a reference to the DataSet
                ' allocated in the Dim statement
                >
                MyMethod2(ds)
                ' ds is now Nothing
                >
                It is important to distinguish the parameter from properties of the
                object being passed. ByVal does not prevent the called method from
                changing properties of the object.
                >
                On Thu, 29 May 2008 19:46:23 -0400, "Andy B" <a_bo...@sbcglo bal.net>
                wrote:
                >
                >Makes sense. This brings up another question: When do you use the "ByVal"
                >keyword?
                >
                >"Tom Shelton" <tom_shel...@YO UKNOWTHEDRILLco mcast.netwrote in message
                >news:eyDLtQewI HA.5892@TK2MSFT NGP02.phx.gbl.. .
                >On 2008-05-29, Andy B <a_bo...@sbcglo bal.netwrote:
                >>I saw this in the set accessor of a property:
                >
                >>Set(ByVal value As DataSet)
                >
                >>What exactly does the stuff in the () mean? VS complained about it not
                >>being
                >>there when I took it out not knowing it needed to be there.
                >
                >Well... Under the covers, get and set accessors are methods. The
                >argument to the Set part of the property, is the value you want to pass
                >in. Properties are syntactic sugar, really.
                >
                >Basically this:
                >
                >Class SomeClass
                >Private _data As DataSet
                >
                >Public Property Data As DataSet
                >Get
                >Return Me._data
                >End Get
                >Set (ByVal value As DataSet)
                >Me._data = value
                >End Set
                >End Property
                >End Class
                >
                >Dim c As SomeClass = New SomeClass()
                >c.Data = New DataSet()
                >
                >Turns into is something like (warning, this is psuedo code for
                >illustration purposes only!):
                >
                >Class SomeClass
                >Private _data As DataSet
                >
                >Public Function GetData () As DataSet
                >Return Me._data
                >End Function
                >
                >Public Sub SetData (ByVal value As DataSet)
                >Me._data = value
                >End Sub
                >End Class
                >
                >Dim c As SomeClass = New SomeClass()
                >c.SetData (New DataSet())
                >
                >Of course, the compiler immits IL, not basic code - but that is
                >essentially what the il is doing. It is creating a get/set pair of
                >methods and calling them :)
                >
                >--
                >Tom Shelton
                Actually things get even more confusing when you throw in value and
                reference types. Value types (such as integers) when used as ByRef
                parameters will be changed by the method, but when you use ByVal they
                are actually copied by the function and the original will not change.
                For reference types however (such as classes) only the pointer is
                passed ByRef/ByVal, so in essence the original object (the true object
                the pointer is pointing at) will always be modified, making ByRef and
                ByVal seemingly pointless when it comes to reference types.

                Sorry, I'm sure that sounded rather confusing, especially since I
                typed it very fast :-)

                Thanks,

                Seth Rowe [MVP]

                Comment

                Working...