CType cast with a variable

Posted on 2006-04-17
Last Modified: 2012-06-27
I have a hashtable that I pass into a session. I use a function to get the value back out of the hashtable in the session.

I have to cast everything to a string when i pass it to the hashtable but when I get the value back out of the hashtable from the session i know what datatype i want to pass it back as.

So i pass into the function the Key and the returned DataType. The problem i am having is the CType will not allow for my variable that tells it what type to return. It keeps telling me that it is not a type.

I even tried passing in a type from a function.

    Public Shared Function ConfigSetting( _
                                       ByVal columnName As String, _
                                      ByVal returnDataType As String) As Object

              Dim ht As New Hashtable
              ht = HttpContext.Current.Session("ConfigSettings")
              Dim myValue = ht(columnName)
              Dim convertType As System.Type =  GetTypeByName(returnDataType.Trim)

              Return CType(columnValue, convertType)

        End If
    End Function

    Public Shared Function GetTypeByName(ByVal TypeName As String) As Type
        Dim AssemblyName As String = "System.Type"

        Dim oAssembly As [Assembly]
        oAssembly = [Assembly].Load(AssemblyName)
        Return oAssembly.GetType(TypeName, True, True)

    End Function


Question by:Lantrax
    LVL 9

    Accepted Solution

    >I have to cast everything to a string when i pass it to the hashtable

    That's not a requirement for a hashtable.  The value added to a Hashtable is type object, so it can be anything.  If you store it as it's original datatype then there is no conversion problem when retrieving it.

    Author Comment

    i knew that but i was having problems with it when i set the session to the hashtable.  I use hashtables all over the place and never have to cast it to a string but when i tried to pass the hashtable to the session it kept erroring out.

    I'll look into it more.


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now