I would like to get something like this to work...
Type t = FindMyType(); // might be int, float, double, etc
string s = "1233";
object v = t.Parse(s);
This doesn't work of couse, Parse is not a member of Type.
I have to think that this is possible. Is it?
Regards,
Bob Rundle 3 3233
Hmm, only way I could think of would be something like this:
Type t = FindMyType(); // might be int, float, double, etc
object v = null;
if (t == typeof(double))
v = double.Parse(s);
else if (t == typeof(int))
v = int.Parse(s);
.... etc
Maybe someone else knows a better way?
--
Adam Clauss ca*****@tamu.edu
"Bob Rundle" <ru****@rundle.com> wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl... I would like to get something like this to work...
Type t = FindMyType(); // might be int, float, double, etc string s = "1233"; object v = t.Parse(s);
This doesn't work of couse, Parse is not a member of Type.
I have to think that this is possible. Is it?
Regards, Bob Rundle
Yes,
That is what I am doing now....not very elegant.
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:ux**************@tk2msftngp13.phx.gbl... Hmm, only way I could think of would be something like this:
Type t = FindMyType(); // might be int, float, double, etc object v = null; if (t == typeof(double)) v = double.Parse(s); else if (t == typeof(int)) v = int.Parse(s); ... etc
Maybe someone else knows a better way? -- Adam Clauss ca*****@tamu.edu
"Bob Rundle" <ru****@rundle.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl... I would like to get something like this to work...
Type t = FindMyType(); // might be int, float, double, etc string s = "1233"; object v = t.Parse(s);
This doesn't work of couse, Parse is not a member of Type.
I have to think that this is possible. Is it?
Regards, Bob Rundle
The only other way I could think of to do it (if you want to get rid of the if's) would be to use reflection to get the Parse
method.
Code modified from: http://www.c-sharpcorner.com/Code/20...Reflection.asp
Check MSDN for the MethodInfo and BindingFlags, I think you'll need another using statement (or fully specify it here in the code
itself).
MethodInfo mi;
object result = null;
object[] args = new object[] {"ABC123"};
Type t = FindMyType(); // might be int, float, double, etc
mi = t.GetMethod("Parse");
if (mi != null)
{
result = t.InvokeMember ("Parse", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null, args);
}
Not sure if that would be considered any "cleaner" though.
--
Adam Clauss ca*****@tamu.edu
"Bob Rundle" <ru****@rundle.com> wrote in message news:On**************@TK2MSFTNGP10.phx.gbl... Yes,
That is what I am doing now....not very elegant.
"Adam Clauss" <ca*****@tamu.edu> wrote in message news:ux**************@tk2msftngp13.phx.gbl... Hmm, only way I could think of would be something like this:
Type t = FindMyType(); // might be int, float, double, etc object v = null; if (t == typeof(double)) v = double.Parse(s); else if (t == typeof(int)) v = int.Parse(s); ... etc
Maybe someone else knows a better way? -- Adam Clauss ca*****@tamu.edu
"Bob Rundle" <ru****@rundle.com> wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl... I would like to get something like this to work...
Type t = FindMyType(); // might be int, float, double, etc string s = "1233"; object v = t.Parse(s);
This doesn't work of couse, Parse is not a member of Type.
I have to think that this is possible. Is it?
Regards, Bob Rundle
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: josh dismukes |
last post by:
/// here is the code i'm getting a parse error on the last line of the
code which
/// is </html> any help will be much appreciated.
<?php
session_start ();
|
by: Andrew |
last post by:
It seems that in C#, given an instance "a" of some value type "A", one can pass a by reference to functions with signatures of either "void f(object obj)" or "void g(ref A obj)". But passing a into...
|
by: |
last post by:
Hi,
I need to read a big CSV file, where different fields should be converted to
different types,
such as int, double, datetime, SqlMoney, etc.
I have an array, which describes the fields and...
|
by: Steve Jorgensen |
last post by:
I'm wondering if there's an approach to writing consistent code to read/write
XML data in arbitrary order that I'm simply missing.
It seems to be easy getting stuff -out- of a DOM via XPath, but...
|
by: Matt |
last post by:
I want to convert a string to an integer, I tried the following 2 methods
and they both worked. But I dont understand in method 1: int.Parse(...),
"int" should be the primitive type, but it can...
|
by: Olivier BESSON |
last post by:
Hello,
I have a web service of my own on a server (vb.net). I must declare it with
SoapRpcMethod to be used with JAVA.
This is a simple exemple method of my vb source :
...
|
by: waylonflinn |
last post by:
I'm looking for a way to invoke methods with an arbitrary number of
parameters of arbitrary type from within a single method, when those
parameters are known at the time of invocation of the...
|
by: AdrianH |
last post by:
Assumptions
I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming.
FYI
Although I have called this...
|
by: Jon Skeet [C# MVP] |
last post by:
On Aug 11, 5:11 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
Exactly. My favourite example is Thread.Sleep. Suppose this code were
valid (its equivalent in Java is, for example):
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |