CType cast with a variable

Posted on 2006-04-17
Medium Priority
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

Accepted Solution

dancebert earned 500 total points
ID: 16468843
>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

ID: 16468912
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

580 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