optional?

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

    #1

    optional?

    hi, im rewriting my printing class i wrote and i want to make one of the
    parameters optional, but i cant figure out how to do it. i want it to b
    something like this

    Public Sub PrintText(Byval text as string, Optional font as Font)
    ....
    End Sub

    thanks
  • Herfried K. Wagner [MVP]

    #2
    Re: optional?

    "iwdu15" <iwdu15@discuss ions.microsoft. com> schrieb:[color=blue]
    > hi, im rewriting my printing class i wrote and i want to make one of the
    > parameters optional, but i cant figure out how to do it. i want it to b
    > something like this
    >
    > Public Sub PrintText(Byval text as string, Optional font as Font)[/color]

    => '...(..., Optional ByVal font As Font = Nothing)'.

    Inside the method's implementation you can check if the 'font' parameter
    contains a null-reference:

    \\\
    If font Is Nothing Then
    ...
    End If
    ///

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

    Comment

    • Ken Tucker [MVP]

      #3
      Re: optional?

      Hi,

      You need to assign a constant value to an optional parameter.
      Unfortunately you can not do that with a font. Here is how I would do it.

      Public Overloads Sub PrintText(ByVal text As String)
      ' my procedure
      PrintText(text, Me.Font)
      End Sub

      Public Overloads Sub PrintText(ByVal text As String, ByVal fnt As Font)
      ' my procedure
      End Sub

      Ken
      --------------------
      "iwdu15" <iwdu15@discuss ions.microsoft. com> wrote in message
      news:6826D930-BC71-4DC7-B59F-DF12704E89DD@mi crosoft.com...[color=blue]
      > hi, im rewriting my printing class i wrote and i want to make one of the
      > parameters optional, but i cant figure out how to do it. i want it to b
      > something like this
      >
      > Public Sub PrintText(Byval text as string, Optional font as Font)
      > ...
      > End Sub
      >
      > thanks[/color]


      Comment

      • iwdu15

        #4
        Re: optional?

        i tried using it on a font, and having the optional defualt be a ont variable
        i set to a font name, and it says the expression must b a constant to be
        used...how can i make it so i can use a font as an optional and the defualt
        be times new roman?

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: optional?

          "iwdu15" <iwdu15@discuss ions.microsoft. com> schrieb:[color=blue]
          >i tried using it on a font, and having the optional defualt be a ont
          >variable
          > i set to a font name, and it says the expression must b a constant to be
          > used...how can i make it so i can use a font as an optional and the
          > defualt
          > be times new roman?[/color]

          Either use overloading as demonstrated by Ken(1) or check if the paramter
          contains a null-reference and instantiate the default font(2).

          (1)

          \\\
          Public Overloads Sub PrintText(ByVal Text As String)
          PrintText(Text, New Font("Times New Roman", ...))
          End Sub

          Public Overloads Sub PrintText(ByVal Text As String, ByVal font As Font)

          ' Print text here.
          End Sub

          (2)

          \\\
          If font Is Nothing Then
          font = New Font("Times New Roman", ...)
          End If

          ' Print text here.
          ///

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

          Comment

          • iwdu15

            #6
            Re: optional?

            aiight thanks, sry when i werote that i didnt refresh my browser so i didnt
            see your response, thanks i think that works fine
            --
            -iwdu15

            Comment

            Working...