PC Review


Reply
Thread Tools Rate Thread

casting to type

 
 
Ron Vecchi
Guest
Posts: n/a
 
      3rd Mar 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
 
      3rd Mar 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
 
      3rd Mar 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" <detcheff_@hotmail.com> 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
 
      3rd Mar 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" <detcheff_@hotmail.com> 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
Rate This Thread
Rate This Thread:

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
Type Casting Analizer1 Microsoft C# .NET 4 31st Oct 2007 07:37 PM
Type Casting pitdog@gmail.com Microsoft C# .NET 1 2nd Jun 2006 06:11 PM
type casting in VB.Net vs C# =?Utf-8?B?Sm9obks=?= Microsoft Dot NET 1 10th Dec 2004 12:52 PM
Type Casting René Nordby Microsoft VB .NET 20 1st Aug 2004 09:30 PM
Type casting object type to user defined class or interface Programmer Microsoft C# .NET 6 17th Jan 2004 08:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:37 AM.