none
Specify System.Type as Optional Parameter in Procedure Definition RRS feed

  • Question

  • Greetings.

    I need to specify an optional parameter of type "System.Type" in a procedure. Of course you have to initialize optional parameters with a default, but I can't seem to get the syntax down... I've tried

    Public Sub AddUnknownKeyword(ByVal keyname As String, Optional ByVal value As String = Nothing, Optional ByVal valueType As System.Type = GetType(String))

    But that isn't valid. (Constant expression is required.) For the same reason, I can't define a variable as System.Type and then use it.

    I've also tried

    Public Sub AddUnknownKeyword(ByVal key As String, Optional ByVal value As String = Nothing, Optional ByVal valueType As Type = System.String)

    But that also is invalid. ('String' is a type in 'System' and can not be used as an expression.)

    So I guess the question is; how do I use a type as an expression?

    Thanks in advance!

    Wednesday, June 27, 2012 9:44 PM

Answers

  • But that also is invalid. ('String' is a type in 'System' and can not be used as an expression.)

    So I guess the question is; how do I use a type as an expression?

    Thanks in advance!

    All you need to do is set it to "Nothing" like the previous Optional parameter.

    Here is a little test I did that shows how you can use System.Type:

    Public Class Class1
        Public Sub AddUnknownKeyword(ByVal keyname As String, _
                                    Optional ByVal value As System.Object = Nothing, _
                                    Optional ByVal valueType As System.Type = Nothing)
            Dim type As System.Type
            type = value.GetType()
            Console.WriteLine("Type of Object = " + type.Name)
            If valueType IsNot Nothing Then
                Console.WriteLine("valueType = " + valueType.Name)
            End If
    
        End Sub
    End Class


    Dan Randolph

    • Proposed as answer by Adavesh Friday, June 29, 2012 3:14 AM
    • Marked as answer by Mike FengModerator Sunday, July 8, 2012 10:05 AM
    Thursday, June 28, 2012 5:56 PM

All replies

  • If this were C#, you would overload the function.  Can VB overload functions?  If you can, create an overloaded function that takes all parameters but the last one.  The implementation of this overload would simply call the other function passing along GetType(String).  I don't know vb.net so I can't be sure.

    Jose R. MCP
    Code Samples

    Thursday, June 28, 2012 12:22 AM
  • But that also is invalid. ('String' is a type in 'System' and can not be used as an expression.)

    So I guess the question is; how do I use a type as an expression?

    Thanks in advance!

    All you need to do is set it to "Nothing" like the previous Optional parameter.

    Here is a little test I did that shows how you can use System.Type:

    Public Class Class1
        Public Sub AddUnknownKeyword(ByVal keyname As String, _
                                    Optional ByVal value As System.Object = Nothing, _
                                    Optional ByVal valueType As System.Type = Nothing)
            Dim type As System.Type
            type = value.GetType()
            Console.WriteLine("Type of Object = " + type.Name)
            If valueType IsNot Nothing Then
                Console.WriteLine("valueType = " + valueType.Name)
            End If
    
        End Sub
    End Class


    Dan Randolph

    • Proposed as answer by Adavesh Friday, June 29, 2012 3:14 AM
    • Marked as answer by Mike FengModerator Sunday, July 8, 2012 10:05 AM
    Thursday, June 28, 2012 5:56 PM