473,837 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing Parse method for arbitrary Type

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
Nov 16 '05 #1
3 3248
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.ed u

"Bob Rundle" <ru****@rundle. com> wrote in message news:%2******** **********@TK2M SFTNGP10.phx.gb l...
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

Nov 16 '05 #2
Yes,

That is what I am doing now....not very elegant.

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:ux******** ******@tk2msftn gp13.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.ed u

"Bob Rundle" <ru****@rundle. com> wrote in message

news:%2******** **********@TK2M SFTNGP10.phx.gb l...
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

Nov 16 '05 #3
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("Pa rse");
if (mi != null)
{
result = t.InvokeMember ("Parse", BindingFlags.Pu blic | BindingFlags.In vokeMethod | BindingFlags.St atic, null, null, args);
}
Not sure if that would be considered any "cleaner" though.

--
Adam Clauss
ca*****@tamu.ed u
"Bob Rundle" <ru****@rundle. com> wrote in message news:On******** ******@TK2MSFTN GP10.phx.gbl...
Yes,

That is what I am doing now....not very elegant.

"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:ux******** ******@tk2msftn gp13.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.ed u

"Bob Rundle" <ru****@rundle. com> wrote in message

news:%2******** **********@TK2M SFTNGP10.phx.gb l...
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



Nov 16 '05 #4

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

Similar topics

3
2935
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 ();
9
2070
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 a function with signature "void f(ref object obj)" gives a compile error -- why (This is part of solving the original problem posed here: http://www.csharp-station.com/ShowPost.aspx?PostID=3625 What I need now is a method that accepts a ref to an...
24
3183
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 their types. I would like to somehow store a reference to parsing operations in this array (such as Int32.Parse, Double.Parse, SqlMoney.Parse, etc), so I can invoke the appropriate one without writing a long switch.
0
1105
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 it's much tougher building a DOM document in arbitrary order. Yes - I can get the parent context element first, using XPath, but then I build custom wrappers and helpers to simplify the building and adding fragments in the correct namespace,...
4
9808
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 operate a method Parse? int t1 = int.Parse(TextBox2.Text); //method 1 int t2 = System.Int32.Parse(TextBox2.Text); //method 2 Another question is which approach is correct? When to use which? Please help! Thanks!
3
5009
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 : >************************************************************************ > <WebMethod(), System.Web.Services.Protocols.SoapRpcMethod()> _ > Public Function HelloWorld() As > <System.Xml.Serialization.SoapElementAttribute("return")> String
2
1978
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 containing method. I don't see a reason why this shouldn't work. I also can't find the language feature which implements it. More details below. As part of the creation of a set of classes for doing unit testing I want to create a generic...
1
64242
AdrianH
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 article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
4
1915
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): ThreadStart ts = SomeMethodToRun; Thread thread = new Thread(ts); thread.Start(); thread.Sleep(500);
0
9846
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10891
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10582
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10638
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10280
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7009
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5859
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4480
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.