optional?

G

Guest

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
 
H

Herfried K. Wagner [MVP]

iwdu15 said:
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)

=> '...(..., 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
///
 
K

Ken Tucker [MVP]

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
 
G

Guest

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?
 
H

Herfried K. Wagner [MVP]

iwdu15 said:
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?

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.
///
 
G

Guest

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top