Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > casting to type

Reply
Thread Tools

casting to type

 
 
Ron Vecchi
Guest
Posts: n/a
 
      03-03-2004
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

}


 
Reply With Quote
 
 
 
 
Martin Dechev
Guest
Posts: n/a
 
      03-03-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
> }
>
>



 
Reply With Quote
 
 
 
 
Ron
Guest
Posts: n/a
 
      03-03-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> > }
> >
> >

>
>



 
Reply With Quote
 
Martin Dechev
Guest
Posts: n/a
 
      03-03-2004
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" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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
> > >
> > > }
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
casting primitive type to user-defined type works in usage xllx.relient.xllx@gmail.com C++ 2 04-15-2006 05:37 AM
type casting vs. type converting Toby VHDL 3 09-07-2005 01:42 PM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM
Casting between const type** and type** Kevin L C++ 6 08-11-2003 03:39 PM



Advertisments