Conserving 'const'ness

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

    #1

    Conserving 'const'ness

    Hi all,

    I have the following sub in one of my classes that requires an optional
    parameter in the form of an ole integer color. The build error I get is
    'Constant expression required', so basically (I think) I need to get a
    Constant value from the Color structure.

    Public Class myClass

    Public Sub mySub(byRef myParam as String, Optional ByRef lColor as
    Integer = ColorTranslator .ToOle(SystemCo lors.Control))
    'My code here
    End Sub

    End Class

    Anyone able to give a few hints?

    Thanks in advance.


    Nick


  • Pipo

    #2
    Re: Conserving 'const'ness

    The ColorTranslator .ToOle(SystemCo lors.Control) returns a constant value.

    "dotNEWBIE" <Newbie@dotNE T> wrote in message
    news:%23piZuBw0 FHA.3336@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > Hi all,
    >
    > I have the following sub in one of my classes that requires an optional
    > parameter in the form of an ole integer color. The build error I get is
    > 'Constant expression required', so basically (I think) I need to get a
    > Constant value from the Color structure.
    >
    > Public Class myClass
    >
    > Public Sub mySub(byRef myParam as String, Optional ByRef lColor as
    > Integer = ColorTranslator .ToOle(SystemCo lors.Control))
    > 'My code here
    > End Sub
    >
    > End Class
    >
    > Anyone able to give a few hints?
    >
    > Thanks in advance.
    >
    >
    > Nick
    >
    >[/color]


    Comment

    • dotNEWBIE

      #3
      Re: Conserving 'const'ness

      Yes, but the bug is how do I get a const value from the 'Control' color to
      supply to the ColorTranslator .ToOle method.

      "Pipo" <NoSpam@me.co m> wrote in message
      news:evCLCLw0FH A.2064@TK2MSFTN GP09.phx.gbl...[color=blue]
      > The ColorTranslator .ToOle(SystemCo lors.Control) returns a constant value.
      >
      > "dotNEWBIE" <Newbie@dotNE T> wrote in message
      > news:%23piZuBw0 FHA.3336@TK2MSF TNGP12.phx.gbl. ..[color=green]
      > > Hi all,
      > >
      > > I have the following sub in one of my classes that requires an optional
      > > parameter in the form of an ole integer color. The build error I get is
      > > 'Constant expression required', so basically (I think) I need to get a
      > > Constant value from the Color structure.
      > >
      > > Public Class myClass
      > >
      > > Public Sub mySub(byRef myParam as String, Optional ByRef lColor as
      > > Integer = ColorTranslator .ToOle(SystemCo lors.Control))
      > > 'My code here
      > > End Sub
      > >
      > > End Class
      > >
      > > Anyone able to give a few hints?
      > >
      > > Thanks in advance.
      > >
      > >
      > > Nick
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Conserving 'const'ness

        "Pipo" <NoSpam@me.co m> schrieb:[color=blue]
        > The ColorTranslator .ToOle(SystemCo lors.Control) returns a constant value.[/color]

        No, it actually doesn't. The property's value reflects the current system
        color settings and thus is not constant (known at compile time).

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

        Comment

        • dotNEWBIE

          #5
          Re: Conserving 'const'ness

          Thanks Herfried, could you suggest a possible solution to my problem?

          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
          news:eqa9Pcw0FH A.2072@TK2MSFTN GP14.phx.gbl...[color=blue]
          > "Pipo" <NoSpam@me.co m> schrieb:[color=green]
          > > The ColorTranslator .ToOle(SystemCo lors.Control) returns a constant[/color][/color]
          value.[color=blue]
          >
          > No, it actually doesn't. The property's value reflects the current system
          > color settings and thus is not constant (known at compile time).
          >
          > --
          > M S Herfried K. Wagner
          > M V P <URL:http://dotnet.mvps.org/>
          > V B <URL:http://classicvb.org/petition/>
          >[/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Conserving 'const'ness

            "dotNEWBIE" <Newbie@dotNE T> schrieb:[color=blue]
            > Thanks Herfried, could you suggest a possible solution to my problem?[/color]

            You simply cannot use a non-constant value as default value for an optional
            parameter. The workaround would be to use another constant value as default
            value.

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

            Comment

            • Pipo

              #7
              Re: Conserving 'const'ness

              Maybe something like this?

              Public Class myClass

              Public Sub mySub(byRef myParam as String, Optional Byval
              blnUseConstColo r as boolean)
              'My code here
              if blnUseConstColo r then
              dim lColor as Integer =
              ColorTranslator .ToOle(SystemCo lors.Control)
              end if
              End Sub

              End Class


              "dotNEWBIE" <Newbie@dotNE T> wrote in message
              news:%23piZuBw0 FHA.3336@TK2MSF TNGP12.phx.gbl. ..[color=blue]
              > Hi all,
              >
              > I have the following sub in one of my classes that requires an optional
              > parameter in the form of an ole integer color. The build error I get is
              > 'Constant expression required', so basically (I think) I need to get a
              > Constant value from the Color structure.
              >
              > Public Class myClass
              >
              > Public Sub mySub(byRef myParam as String, Optional ByRef lColor as
              > Integer = ColorTranslator .ToOle(SystemCo lors.Control))
              > 'My code here
              > End Sub
              >
              > End Class
              >
              > Anyone able to give a few hints?
              >
              > Thanks in advance.
              >
              >
              > Nick
              >
              >[/color]


              Comment

              • alantolan@users.com

                #8
                Re: Conserving 'const'ness

                You might try using an overload instead of an optional parameter. This
                will give the same effect but doesn't require the const expression in
                the signature

                e.g

                Private Overloads Sub MySub( _
                ByVal str As String _
                )
                MySub(str,
                System.Drawing. Color.FromKnown Color(System.Dr awing.KnownColo r.Control))
                End Sub

                Private Overloads Sub MySub( _
                ByVal str As String, _
                ByVal col As Color _
                )
                ' place code here
                End Sub


                hth,
                Alan.

                Comment

                Working...