473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

casting to type

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

}
Nov 18 '05 #1
3 1014
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" <rv*****@callitechnic.com> wrote in message
news:u1*************@TK2MSFTNGP09.phx.gbl...
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

}

Nov 18 '05 #2
Ron
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" <de*******@hotmail.com> wrote in message
news:uj**************@tk2msftngp13.phx.gbl...
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" <rv*****@callitechnic.com> wrote in message
news:u1*************@TK2MSFTNGP09.phx.gbl...
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

}


Nov 18 '05 #3
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" <rv*****@callitechnic.com> wrote in message
news:#R**************@TK2MSFTNGP09.phx.gbl...
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" <de*******@hotmail.com> wrote in message
news:uj**************@tk2msftngp13.phx.gbl...
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" <rv*****@callitechnic.com> wrote in message
news:u1*************@TK2MSFTNGP09.phx.gbl...
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

}



Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
4
by: c | last post by:
Hello All, I am struggling with simple casting issue within c# and would really appreciate some insight into where I am going wrong. I have two classes, ClassA and ClassB. Each implements a...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
8
by: David Williams | last post by:
Hi all, I have a templated Vector3D class which holds (x,y,z) components as the specified type. I quite often wish to cast a Vector3D holding ints into a Vector3D holding floats and vice versa....
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.