473,396 Members | 2,034 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,396 software developers and data experts.

Convert "Int32" to type Int32

Joe
I have data types stored in a field in a database and would like to use
something similar to GetType when reading these values.

string datatype = dr["DataTypeColumn"]

// something like this would be nice.
ConvertStringToCorrectDataType(datatype);

I guess I could just list all the string values of the data types and do a
comparision but I thought there would be some kind of built in method for
this.

Thanks,
Joe
Nov 16 '05 #1
1 1605
I think that there is something missing from your question.
Specifically, you must have another column that holds the actual data,
no? Having nothing more than a string that says "Int32" doesn't get you
very far without a value to use with it.

Yes, there are ways using Reflection to instantiate an instance of a
type given its name. See the Type.GetType() static method, for
starters. Once you have a Type object then you can get the constructor
using Type.GetConstructor, and invoke the resulting constructor using
ConstructorInfo.Invoke, which will get you an object of the indicated
type.

However, I wonder if this is the best design: storing .NET Framework
type names in a database? What if some other language has to use the
database and read these type names? What if Microsoft changes the name
of a type? (Naah... they wouldn't do that, would they? :)

I would think that you'd be much better off storing a "type indicator"
in the database. Make up your own names or codes. Then write a method,
as you stated, that has a big "switch" statement that builds the
correct kind of object based on the name. This has two major
advantages:

1. Easier to understand, and thus easier to maintain. Newbie
programmers can read this code more easily than they can decipher
tricky calls to Reflection.
2. Language-independent. Non-.NET languages can easily read the
database and implement the same logic.

Just my two cents' worth....

Nov 16 '05 #2

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

Similar topics

3
by: Tim Osborne | last post by:
I want to know if a call to Convert.Int32 can handle a Culture specific string. I have the following code: string sText = "111,111,111"; int i; i = Convert.Int32(...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
1
by: Alan | last post by:
Hi all, I want to convert Int32 to byte.How should i do? Thanks! Alan
3
by: Jan | last post by:
How can I convert Int32 (4 bytes long) value to array of bytes? I need only 4 bytes in result. example: long value 55695542 --> arr(0) == &HB6 arr(1) == &HD8 arr(2) == &H51 arr(3) == &H03
1
by: James Pose | last post by:
Convert.Int32 fails for "0" but works fine for all other numbers. This does not work on one machine when "0" it works fine on other machines Convert.ToInt32(“0”); gives exception {"Input...
4
by: bob95226 | last post by:
Hi All, I wrote a VB.NET control in Visual Studio 2003 IDE, I am trying to re-compile it with command line compiler vbc.exe like this: vbc /t:library /out:my_control.dll my_control.vb However...
7
by: mehafi | last post by:
hi, I need to write a few integer numbers to file. A method write from FileStream writes an array of bytes. So how to conwert my integers to byte array? I've tried Convert class, but it hasn't...
1
by: John | last post by:
Peter Duniho wrote: Well, the code uses a system of building token types and objects out of the string. I am porting some C++ code that uses putback on the stream during the processing which can...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
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...
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
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 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.