473,770 Members | 1,700 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 3245
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
2932
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
2067
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
3176
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
9806
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
5006
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
64192
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
10237
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
10071
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...
0
9882
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
8905
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
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
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.