Results 1 to 3 of 3

Thread: CType using a variable to hold the type doesn't work

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    67

    CType using a variable to hold the type doesn't work

    So I do not know at design time if the var ms is going to be a stringreader or a memorystream. So I am trying to do basically this:

    Dim ms as object = myfunction...
    Dim mytype As Type = ms.GetType
    Dim returnedobject = ser.Deserialize(CType(ms, mytype))

    But I get an error saying "type mytype is not defined". How can I do this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: CType using a variable to hold the type doesn't work

    You can't. A data type and an instance of the Type class are two very different things.

    The whole point of casting is that you tell the compiler what the object is. That means that you have to know exactly what the type is when you compile. If you are going to start down the road of Reflection then you must stay on that road. You can't suddenly jump off halfway through.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    67

    Re: CType using a variable to hold the type doesn't work

    Nested if then statements it is...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width