locked
How to convert class runtime in CType in second parameter RRS feed

  • Question

  • User750184344 posted

    Hi,  

    I have two questions.

    1. I have three classes. Now I want to know how do I convert object to my class runtime?

    for example,

    Dim obj as New Object

    obj = CType( MyPassedObject, Class1 )

    Now, in above code, MyPassedObject can be Class1 or Class2 or Class3 but it is passed as Object. So is there any way that I can convert into CType as per the type of that object, directly to that class? I mean, I can pass object name into parameter and write down CASE statement here to convert to that class but I do not want to write down Case statement for each of my class, so just want to know is it possible anyway?

    I mean just for example, Can I do something like  obj = CType( MyPassedObject, GetObject('Class1') or any other way?

    2. Now for this obj, I am getting properties runtime and setting values and then updating.

    for example,

     Dim PropertyInfo As Reflection.PropertyInfo() = obj.GetType.GetProperties()

    Now, Will it work fine if obj has been declared as Object and then I typeCast it runtime and then I get properties and set its values. Will it work if obj's variable type is Object or it must be that class type itself.

     

    Thanks in advance,

    Monday, March 22, 2010 10:47 AM

Answers

  • User1485408165 posted

    Are you talking about doing something like this?

     

    Select Case TypeOf obj 
    Case Is Class1
           newObj = CType(obj,Class1)
    Case Is Class2
           newObj = CType(obj,Class2)
    Case Is Class3
           newObj = CType(obj,Class3)
    End Select

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 25, 2010 7:06 PM

All replies

  • User559104930 posted

    My question to you would be why? Why are you taking this approach? As per your description, it sounds like you would benefit from using generics or creating a base class or interface for your objects to implement. There should very rarely (almost never), be a reason to type-cast an object. It is usually a sign that a type parameter is wrong, not generic enough (i.e. IList vs. IEnumerable), or you are missing an abstraction.

    Tuesday, March 23, 2010 7:50 AM
  • User750184344 posted

    actually, I am accessing a third party object. And depending on the name passed, I have to convert object to that third party object's class type. So I need to decide this at runtime depending on the named passed to my parameter. 

    Thursday, March 25, 2010 2:37 PM
  • User1485408165 posted

    Are you talking about doing something like this?

     

    Select Case TypeOf obj 
    Case Is Class1
           newObj = CType(obj,Class1)
    Case Is Class2
           newObj = CType(obj,Class2)
    Case Is Class3
           newObj = CType(obj,Class3)
    End Select

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 25, 2010 7:06 PM