Make a Parameter CONST?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brien King

    #1

    Make a Parameter CONST?

    If I have a parameter that has an Object type (as opposed to something like
    a string), can I make that parameter a CONST?

    Right now, if you pass an object into a sub/function, that sub/function can
    modify the object no matter how it's defined (ByVal or ByRef).

    In some cases, I want to make sure that you cannot modify the object. Is
    there a way to do that in VS2003 or the up comming VS2005?


    Thanks,


    Brien King


  • david

    #2
    Re: Make a Parameter CONST?

    On 2005-09-17, Brien King <spammehere@arc aderestoration. com> wrote:[color=blue]
    > If I have a parameter that has an Object type (as opposed to something like
    > a string), can I make that parameter a CONST?[/color]

    That's kinda weirdly phrased, a string IS an Object, but the answer is
    no.
    [color=blue]
    >
    > Right now, if you pass an object into a sub/function, that sub/function can
    > modify the object no matter how it's defined (ByVal or ByRef).[/color]
    [color=blue]
    > In some cases, I want to make sure that you cannot modify the object. Is
    > there a way to do that in VS2003 or the up comming VS2005?[/color]

    Nope. This seems to be one of those things that the CLR and .Net
    language designers looked at and decided they just didn't want.

    Comment

    • m.posseth

      #3
      Re: Make a Parameter CONST?

      just pass a class or structure as the parameter and define a property as
      readonly
      now you can read the parameter but you can`t change it



      Private Class constVal

      ReadOnly Property example()

      Get

      Return "this is a constant value"

      End Get

      End Property

      End Class

      Private Sub test(ByVal x As constVal)

      MsgBox(x.exampl e)

      End Sub

      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click

      Dim y As New constVal

      test(y)

      End Sub


      regards

      Michel Posseth

      "Brien King" <spammehere@arc aderestoration. com> wrote in message
      news:erx4YvyuFH A.2008@TK2MSFTN GP10.phx.gbl...[color=blue]
      > If I have a parameter that has an Object type (as opposed to something
      > like a string), can I make that parameter a CONST?
      >
      > Right now, if you pass an object into a sub/function, that sub/function
      > can modify the object no matter how it's defined (ByVal or ByRef).
      >
      > In some cases, I want to make sure that you cannot modify the object. Is
      > there a way to do that in VS2003 or the up comming VS2005?
      >
      >
      > Thanks,
      >
      >
      > Brien King
      >[/color]


      Comment

      • Jesse Liberty

        #4
        Re: Make a Parameter CONST?

        "Brien King" <spammehere@arc aderestoration. com> wrote in message
        news:erx4YvyuFH A.2008@TK2MSFTN GP10.phx.gbl...[color=blue]
        > If I have a parameter that has an Object type ...can I make that parameter
        > a CONST?
        > In some cases, I want to make sure that you cannot modify the object. Is
        > there a way to do that in VS2003 or the up comming VS2005?[/color]


        There is no way to do so, but there may be acceptable work-around. You can
        for example create a shadow struct that has members for the values you want
        to pass by value, and a method in your class to generate that struct. You
        would then pass the struct, which is passed by value.


        --
        Jesse Liberty
        Author of .NET books for O'Reilly



        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Make a Parameter CONST?

          "Brien King" <spammehere@arc aderestoration. com> schrieb:[color=blue]
          > If I have a parameter that has an Object type (as opposed to something
          > like a string), can I make that parameter a CONST?
          >
          > Right now, if you pass an object into a sub/function, that sub/function
          > can modify the object no matter how it's defined (ByVal or ByRef).
          >
          > In some cases, I want to make sure that you cannot modify the object. Is
          > there a way to do that in VS2003 or the up comming VS2005?[/color]

          No.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • m.posseth

            #6
            Re: Make a Parameter CONST?

            after rethinking ,,, i provided a workaround but it is absolutely useless

            why the $##$$# would you do that if you could just as easily use a local (
            in the same class as the method ) contstant value

            maybe we should ask you what is your situation in wich you think this is
            needed ???

            as i said i would not pass a contstant value at all to a parameter , i would
            make it a class construct parameter and call it with a read only property in
            the method itself

            regards

            Michel Posseth [MCP]




            "Brien King" <spammehere@arc aderestoration. com> wrote in message
            news:erx4YvyuFH A.2008@TK2MSFTN GP10.phx.gbl...[color=blue]
            > If I have a parameter that has an Object type (as opposed to something
            > like a string), can I make that parameter a CONST?
            >
            > Right now, if you pass an object into a sub/function, that sub/function
            > can modify the object no matter how it's defined (ByVal or ByRef).
            >
            > In some cases, I want to make sure that you cannot modify the object. Is
            > there a way to do that in VS2003 or the up comming VS2005?
            >
            >
            > Thanks,
            >
            >
            > Brien King
            >[/color]


            Comment

            • david

              #7
              Re: Make a Parameter CONST?

              On 2005-09-17, m.posseth <michelp@nohaus ystems.nl> wrote:[color=blue]
              > after rethinking ,,, i provided a workaround but it is absolutely useless
              >
              > why the $##$$# would you do that if you could just as easily use a local (
              > in the same class as the method ) contstant value
              >
              > maybe we should ask you what is your situation in wich you think this is
              > needed ???
              >
              > as i said i would not pass a contstant value at all to a parameter , i would
              > make it a class construct parameter and call it with a read only property in
              > the method itself[/color]

              You're missing the point. What the OP wants is to pass reference types
              to a method and ensure that the method won't change the contents of the
              passed object. It would look something like this...

              Public Sub Foo(ByConst list As ArrayList)

              Console.Writeli ne(list(0)) ' This is OK
              list(0) = "Hello" ' This would be a compile error


              Because the list parameter is declared const, the Foo method can't
              change the contents. This is a very common construct in C++, but
              isn't supported in the CLR or the Framework.






              [color=blue]
              >
              > regards
              >
              > Michel Posseth [MCP]
              >
              >
              >
              >
              > "Brien King" <spammehere@arc aderestoration. com> wrote in message
              > news:erx4YvyuFH A.2008@TK2MSFTN GP10.phx.gbl...[color=green]
              >> If I have a parameter that has an Object type (as opposed to something
              >> like a string), can I make that parameter a CONST?
              >>
              >> Right now, if you pass an object into a sub/function, that sub/function
              >> can modify the object no matter how it's defined (ByVal or ByRef).
              >>
              >> In some cases, I want to make sure that you cannot modify the object. Is
              >> there a way to do that in VS2003 or the up comming VS2005?
              >>
              >>
              >> Thanks,
              >>
              >>
              >> Brien King
              >>[/color]
              >
              >[/color]

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Make a Parameter CONST?

                David,

                [color=blue]
                > You're missing the point. What the OP wants is to pass reference types
                > to a method and ensure that the method won't change the contents of the
                > passed object.[/color]

                And in my opinion very correct answered by you last week.



                :-)

                Cor


                Comment

                • david

                  #9
                  Re: Make a Parameter CONST?

                  On 2005-09-17, Cor Ligthert [MVP] <notmyfirstname @planet.nl> wrote:[color=blue]
                  > David,
                  >
                  >[color=green]
                  >> You're missing the point. What the OP wants is to pass reference types
                  >> to a method and ensure that the method won't change the contents of the
                  >> passed object.[/color]
                  >
                  > And in my opinion very correct answered by you last week.
                  >
                  > http://groups.google.com/group/micro...66eac3c?hl=en&
                  >
                  >:-)[/color]

                  Yes, the same questions do seem to pop up here quite often.

                  Comment

                  • m.posseth

                    #10
                    Re: Make a Parameter CONST?


                    No i am not missing the point ,

                    i do understand that what he want is not possible in this way , however i
                    provided a workaround to acomplish something simular
                    however after rethinking my first solution i thought that there is a much
                    better way

                    If i rethink this in a OOP way it isn`t so difficult to acomplish at all
                    ( define the needed business rules in some objects on demand ) ,,, however
                    it is still a workaround but it will do what he wants

                    well .... maybe i should have mentioned the impossibility in forehand , but
                    as i read the hole thread i base my answer on what is previously said
                    ( sorry :-)

                    regards

                    and have a nice weekend

                    Michel Posseth




                    "david" <david@woofix.l ocal.dom> wrote in message
                    news:slrndio3ot .edn.david@loca lhost.localdoma in...[color=blue]
                    > On 2005-09-17, m.posseth <michelp@nohaus ystems.nl> wrote:[color=green]
                    >> after rethinking ,,, i provided a workaround but it is absolutely
                    >> useless
                    >>
                    >> why the $##$$# would you do that if you could just as easily use a local
                    >> (
                    >> in the same class as the method ) contstant value
                    >>
                    >> maybe we should ask you what is your situation in wich you think this is
                    >> needed ???
                    >>
                    >> as i said i would not pass a contstant value at all to a parameter , i
                    >> would
                    >> make it a class construct parameter and call it with a read only property
                    >> in
                    >> the method itself[/color]
                    >
                    > You're missing the point. What the OP wants is to pass reference types
                    > to a method and ensure that the method won't change the contents of the
                    > passed object. It would look something like this...
                    >
                    > Public Sub Foo(ByConst list As ArrayList)
                    >
                    > Console.Writeli ne(list(0)) ' This is OK
                    > list(0) = "Hello" ' This would be a compile error
                    >
                    >
                    > Because the list parameter is declared const, the Foo method can't
                    > change the contents. This is a very common construct in C++, but
                    > isn't supported in the CLR or the Framework.
                    >
                    >
                    >
                    >
                    >
                    >
                    >[color=green]
                    >>
                    >> regards
                    >>
                    >> Michel Posseth [MCP]
                    >>
                    >>
                    >>
                    >>
                    >> "Brien King" <spammehere@arc aderestoration. com> wrote in message
                    >> news:erx4YvyuFH A.2008@TK2MSFTN GP10.phx.gbl...[color=darkred]
                    >>> If I have a parameter that has an Object type (as opposed to something
                    >>> like a string), can I make that parameter a CONST?
                    >>>
                    >>> Right now, if you pass an object into a sub/function, that sub/function
                    >>> can modify the object no matter how it's defined (ByVal or ByRef).
                    >>>
                    >>> In some cases, I want to make sure that you cannot modify the object.
                    >>> Is
                    >>> there a way to do that in VS2003 or the up comming VS2005?
                    >>>
                    >>>
                    >>> Thanks,
                    >>>
                    >>>
                    >>> Brien King
                    >>>[/color]
                    >>
                    >>[/color][/color]


                    Comment

                    • DWS

                      #11
                      RE: Make a Parameter CONST?

                      Brien,
                      Your right the sub or function can modify the objects you pass to em!

                      Byval the sub can modify a temporary copy of the object.
                      Byref the sub can modify the object.

                      Good Luck
                      DWS











                      Byval = a copy of the object. its just a copy so when you return from the
                      sub or function the original object is not changed.

                      ByRef = the actual object.




                      Byref = the function can actually change the object.





                      "Brien King" wrote:
                      [color=blue]
                      > If I have a parameter that has an Object type (as opposed to something like
                      > a string), can I make that parameter a CONST?
                      >
                      > Right now, if you pass an object into a sub/function, that sub/function can
                      > modify the object no matter how it's defined (ByVal or ByRef).
                      >
                      > In some cases, I want to make sure that you cannot modify the object. Is
                      > there a way to do that in VS2003 or the up comming VS2005?
                      >
                      >
                      > Thanks,
                      >
                      >
                      > Brien King
                      >
                      >
                      >[/color]

                      Comment

                      • david

                        #12
                        Re: Make a Parameter CONST?

                        On 2005-09-17, m.posseth <michelp@nohaus ystems.nl> wrote:[color=blue]
                        >
                        > No i am not missing the point ,[/color]

                        OK, then I misunderstood this...
                        [color=blue][color=green]
                        >>why the $##$$# would you do that if you could just as easily use a local (
                        >>in the same class as the method ) contstant value[/color][/color]

                        A local (or remote) constant value has no relevance to const parameters.
                        [color=blue]
                        > i do understand that what he want is not possible in this way , however i
                        > provided a workaround to acomplish something simular
                        > however after rethinking my first solution i thought that there is a much
                        > better way
                        >
                        > If i rethink this in a OOP way it isn`t so difficult to acomplish at all
                        > ( define the needed business rules in some objects on demand ) ,,, however
                        > it is still a workaround but it will do what he wants[/color]

                        I'm curious what you mean here, but I must admit I'm not sure what
                        you're getting at or what it's relevance is.


                        Comment

                        • m.posseth

                          #13
                          Re: Make a Parameter CONST?

                          Hmm well we are obviously not talking the same language :-)

                          when i said
                          [color=blue][color=green][color=darkred]
                          >>>why the $##$$# would you do that if you could just as easily use a local
                          >>>(
                          >>>in the same class as the method ) contstant value[/color][/color][/color]

                          I was refering to the previous answer i had given ( as i feel that
                          encapsulation in a class is a much better aproach )
                          [color=blue]
                          > I'm curious what you mean here, but I must admit I'm not sure what
                          > you're getting at or what it's relevance is.[/color]

                          Please tell me what is wrong with defining a class with friend / public
                          readonly properties that can only be set during construction from the
                          outside , as a workaround for the problem


                          Your answer was No and i have given an alternative aproach ,,,, and after
                          rereading the original question i believe it is verry relevant .... if you
                          feel that it isn`t then let the TS decide if this might be an alternative ,
                          as it is obvious that he comes here for a solution of his problem .

                          regards

                          Michel Posseth







                          "david" <david@woofix.l ocal.dom> wrote in message
                          news:slrndiouap .ers.david@loca lhost.localdoma in...[color=blue]
                          > On 2005-09-17, m.posseth <michelp@nohaus ystems.nl> wrote:[color=green]
                          >>
                          >> No i am not missing the point ,[/color]
                          >
                          > OK, then I misunderstood this...
                          >[color=green][color=darkred]
                          >>>why the $##$$# would you do that if you could just as easily use a local
                          >>>(
                          >>>in the same class as the method ) contstant value[/color][/color]
                          >
                          > A local (or remote) constant value has no relevance to const parameters.
                          >[color=green]
                          >> i do understand that what he want is not possible in this way , however i
                          >> provided a workaround to acomplish something simular
                          >> however after rethinking my first solution i thought that there is a much
                          >> better way
                          >>
                          >> If i rethink this in a OOP way it isn`t so difficult to acomplish at all
                          >> ( define the needed business rules in some objects on demand ) ,,,
                          >> however
                          >> it is still a workaround but it will do what he wants[/color]
                          >
                          > I'm curious what you mean here, but I must admit I'm not sure what
                          > you're getting at or what it's relevance is.
                          >
                          >[/color]


                          Comment

                          • david

                            #14
                            Re: Make a Parameter CONST?

                            On 2005-09-18, m.posseth <michelp@nohaus ystems.nl> wrote:[color=blue]
                            > Hmm well we are obviously not talking the same language :-)
                            >
                            > when i said
                            >[color=green][color=darkred]
                            >>>>why the $##$$# would you do that if you could just as easily use a local
                            >>>>(
                            >>>>in the same class as the method ) contstant value[/color][/color]
                            >
                            > I was refering to the previous answer i had given ( as i feel that
                            > encapsulation in a class is a much better aproach )
                            >[color=green]
                            >> I'm curious what you mean here, but I must admit I'm not sure what
                            >> you're getting at or what it's relevance is.[/color]
                            >
                            > Please tell me what is wrong with defining a class with friend / public
                            > readonly properties that can only be set during construction from the
                            > outside , as a workaround for the problem[/color]

                            You're right, we're not talking the same language. The paragraph I said
                            I didn't understand what you meant was "If i rethink this in a OOP way
                            it isn`t so difficult to acomplish at all ( define the needed business
                            rules in some objects on demand )", which is a whole lot more obscure
                            than what you just said. But no matter...
                            [color=blue]
                            > Your answer was No and i have given an alternative aproach ,,,, and after
                            > rereading the original question i believe it is verry relevant .... if you
                            > feel that it isn`t then let the TS decide if this might be an alternative ,
                            > as it is obvious that he comes here for a solution of his problem .[/color]

                            No, Jesse posted more or less the same thing. Wrapping the object in
                            read-only class or interface is the standard response given to this,
                            and it's certainly relevant.

                            Of course, it's also quite limited and not really a reasonable replacement
                            at all except in very simple circumstances. All in all, I agree with
                            the arguments against const parameters that people like Anders Hejlberg
                            have made, but it's a very useful construct that isn't easily duplicated
                            in .Net, and I think the idea that the equivalent is somehow easy to
                            accomplish is simply wrong.

                            Comment

                            • Brien King

                              #15
                              Re: Make a Parameter CONST?

                              DWS,

                              Sorry, but that is not what happens, and thus my query. In .NET, if you
                              pass an object ByVal, it does NOT copy the object itself and the original
                              object DOES get modified. I have tested this, and it's easy to demonstrate.

                              Here is a quick example:

                              Public Class TTestClass
                              Private m_myVar As String
                              Property MyVar() As String
                              Get
                              Return m_myVar
                              End Get
                              Set(ByVal Value As String)
                              m_myVar = Value
                              End Set
                              End Property
                              End Class

                              Private Sub Button4_Click(B yVal sender As System.Object, ByVal e As
                              System.EventArg s) Handles Button4.Click
                              Dim testClass As New TTestClass
                              Dim testString As String

                              ModifyMyTestCla ss(testClass)
                              MessageBox.Show (testClass.MyVa r)

                              ModifyMyTestStr ingByVal(testSt ring)
                              MessageBox.Show (testString)

                              ModifyMyTestStr ingByRef(testSt ring)
                              MessageBox.Show (testString)

                              End Sub

                              Private Sub ModifyMyTestCla ss(ByVal P_testClass As TTestClass)
                              P_testClass.MyV ar = "This shouldn't leave this method... but it does!"
                              End Sub

                              Private Sub ModifyMyTestStr ingByVal(ByVal P_testString As String)
                              P_testString = "This won't leave this method..."
                              End Sub

                              Private Sub ModifyMyTestStr ingByRef(ByRef P_testString As String)
                              P_testString = "This WILL leave this method..."
                              End Sub

                              When you run that code, you'll see the message "This shouldn't leave this
                              method... but it does!".

                              Technically, a String is an Object in .NET so you would expect the same
                              behaviour, but it behaves as I would expect it to. The first Message box is
                              Empty, the second has a value.

                              Now I had made the wrong assumption up to this point, that it actually made
                              a copy of the object that is being passed in and that it wouldn't modify the
                              original object. If I wanted to modify the original object I would have had
                              to specify ByRef. To me is was the same as passing in someting in C++ as &
                              or passing in as const &. Apparently I was wrong.

                              I don't need to work around it, I just needed to be aware that is the way it
                              works. Because it behaves this way, it would be EASY to introduce subtle
                              bugs into the code due to the fact that it DOES modify the original object.

                              It would be Nice to be able to specify that a Parameter is CONST so that the
                              compiler would catch an assignment to that parameter. The main reason for
                              me to use that is to ensure that every INPUT parameter is exactly that.
                              INPUT.



                              Brien King


                              "DWS" <DWS@discussion s.microsoft.com > wrote in message
                              news:2F53C4B1-E2D8-4D42-8C56-E31428902882@mi crosoft.com...[color=blue]
                              > Brien,
                              > Your right the sub or function can modify the objects you pass to em!
                              >
                              > Byval the sub can modify a temporary copy of the object.
                              > Byref the sub can modify the object.
                              >
                              > Good Luck
                              > DWS
                              >
                              > Byval = a copy of the object. its just a copy so when you return from the
                              > sub or function the original object is not changed.
                              >
                              > ByRef = the actual object.
                              >
                              >
                              >
                              >
                              > Byref = the function can actually change the object.
                              >
                              >[/color]

                              Comment

                              Working...