casting to type

Discussion in 'ASP .Net' started by Ron Vecchi, Mar 3, 2004.

  1. Ron Vecchi

    Ron Vecchi Guest

    If a have a funtion:

    public object MyFCT(string searchOn, System.Type typ){

    string foundval;

    ///foundval is a string pulled from a database
    /// How can I cast the string into the supplied System.Type typ
    /// and then return the object?

    /// Thanks

    }
     
    Ron Vecchi, Mar 3, 2004
    #1
    1. Advertising

  2. Hi, Ron Vecchi,

    You can create an instance of a type with the static method CreateInstance
    on the System.Activator class. I don't understand what is the connection
    between the string in the DB and the instance or the type.

    Hope this helps
    Martin
    "Ron Vecchi" <> wrote in message
    news:...
    > If a have a funtion:
    >
    > public object MyFCT(string searchOn, System.Type typ){
    >
    > string foundval;
    >
    > ///foundval is a string pulled from a database
    > /// How can I cast the string into the supplied System.Type typ
    > /// and then return the object?
    >
    > /// Thanks
    >
    > }
    >
    >
     
    Martin Dechev, Mar 3, 2004
    #2
    1. Advertising

  3. Ron Vecchi

    Ron Guest

    I am trying to create a function that will automaticall cast the value and
    return it based on the parameter. This way say if the value returned from
    the DB (or Xml,Text,.etc) is a string "True" I can call the method and use
    it like so:

    bool mybool = MyClass.MyFCT("searchString",bool); //excuse my newbyness
    The same would go for Int32, Int16, DateTime, Double......

    I am tring to wrap all the converion in one method without creating a method
    for each poosible type.

    Thanks!

    Ron
    "Martin Dechev" <> wrote in message
    news:...
    > Hi, Ron Vecchi,
    >
    > You can create an instance of a type with the static method CreateInstance
    > on the System.Activator class. I don't understand what is the connection
    > between the string in the DB and the instance or the type.
    >
    > Hope this helps
    > Martin
    > "Ron Vecchi" <> wrote in message
    > news:...
    > > If a have a funtion:
    > >
    > > public object MyFCT(string searchOn, System.Type typ){
    > >
    > > string foundval;
    > >
    > > ///foundval is a string pulled from a database
    > > /// How can I cast the string into the supplied System.Type typ
    > > /// and then return the object?
    > >
    > > /// Thanks
    > >
    > > }
    > >
    > >

    >
    >
     
    Ron, Mar 3, 2004
    #3
  4. You know the return type at compile time. I don't see your point. Why do you
    want to avoid

    bool.Parse(string)

    or

    Convert.ToBoolean(string)

    ?

    Greetings
    Martin
    "Ron" <> wrote in message
    news:#...
    > I am trying to create a function that will automaticall cast the value and
    > return it based on the parameter. This way say if the value returned from
    > the DB (or Xml,Text,.etc) is a string "True" I can call the method and use
    > it like so:
    >
    > bool mybool = MyClass.MyFCT("searchString",bool); //excuse my newbyness
    > The same would go for Int32, Int16, DateTime, Double......
    >
    > I am tring to wrap all the converion in one method without creating a

    method
    > for each poosible type.
    >
    > Thanks!
    >
    > Ron
    > "Martin Dechev" <> wrote in message
    > news:...
    > > Hi, Ron Vecchi,
    > >
    > > You can create an instance of a type with the static method

    CreateInstance
    > > on the System.Activator class. I don't understand what is the connection
    > > between the string in the DB and the instance or the type.
    > >
    > > Hope this helps
    > > Martin
    > > "Ron Vecchi" <> wrote in message
    > > news:...
    > > > If a have a funtion:
    > > >
    > > > public object MyFCT(string searchOn, System.Type typ){
    > > >
    > > > string foundval;
    > > >
    > > > ///foundval is a string pulled from a database
    > > > /// How can I cast the string into the supplied System.Type typ
    > > > /// and then return the object?
    > > >
    > > > /// Thanks
    > > >
    > > > }
    > > >
    > > >

    > >
    > >

    >
    >
     
    Martin Dechev, Mar 3, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,833
    Andrew McDonagh
    Jan 8, 2005
  2. heyo
    Replies:
    3
    Views:
    944
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    821
    Dan Pop
    Apr 2, 2004
  4. Wally Barnes
    Replies:
    3
    Views:
    540
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    586
    James Kanze
    Dec 20, 2009
Loading...

Share This Page