473,385 Members | 1,356 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,385 software developers and data experts.

How to create Type from TypeCode...

Does anyone know if there is a built-in function for creating a Type from a
TypeCode?

Type t = somefunction(TypeCode.Int16);
Sep 21 '05 #1
3 6946
Hi HairlipDog,

Thanks for your post.

public static object Convert.ChangeType(object, TypeCode); method has the
similiar function, It can change an object instance into a type which
TypeCode equals to TypeCode parameter.

However, for your specific request, it seems that .Net Framework did not
have build-in method. We can build a method of this function ourselves.

If we use Reflector to view Convert.ChangeType's source code, we will see
that it internally uses switch case statement to do the conversion. Listed
below:
public static object ChangeType(object value, TypeCode typeCode,
IFormatProvider provider)
{
IConvertible convertible1 = value as IConvertible;
if (convertible1 == null)
{
if ((value != null) || (typeCode != TypeCode.Empty))
{
throw new
InvalidCastException(Environment.GetResourceString ("InvalidCast_IConvertible
"));
}
return null;
}
switch (typeCode)
{
case TypeCode.Empty:
{
throw new
InvalidCastException(Environment.GetResourceString ("InvalidCast_Empty"));
}
case TypeCode.Object:
{
return value;
}
case TypeCode.DBNull:
{
throw new
InvalidCastException(Environment.GetResourceString ("InvalidCast_DBNull"));
}
case TypeCode.Boolean:
{
return convertible1.ToBoolean(provider);
}
case TypeCode.Char:
{
return convertible1.ToChar(provider);
}
case TypeCode.SByte:
{
return convertible1.ToSByte(provider);
}
case TypeCode.Byte:
{
return convertible1.ToByte(provider);
}
case TypeCode.Int16:
{
return convertible1.ToInt16(provider);
}
case TypeCode.UInt16:
{
return convertible1.ToUInt16(provider);
}
case TypeCode.Int32:
{
return convertible1.ToInt32(provider);
}
case TypeCode.UInt32:
{
return convertible1.ToUInt32(provider);
}
case TypeCode.Int64:
{
return convertible1.ToInt64(provider);
}
case TypeCode.UInt64:
{
return convertible1.ToUInt64(provider);
}
case TypeCode.Single:
{
return convertible1.ToSingle(provider);
}
case TypeCode.Double:
{
return convertible1.ToDouble(provider);
}
case TypeCode.Decimal:
{
return convertible1.ToDecimal(provider);
}
case TypeCode.DateTime:
{
return convertible1.ToDateTime(provider);
}
case TypeCode.String:
{
return convertible1.ToString(provider);
}
}
throw new
ArgumentException(Environment.GetResourceString("A rg_UnknownTypeCode"));
}

So we can also write a large switch case statement to return corresponding
Type based on the TypeCode.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Sep 22 '05 #2
Thanks Jeffrey,

I wound up implementing a large case statement, but wanted to check whether
there was a built-in to ensure upward compatibility with type changes.

-Mark
Sep 22 '05 #3
Yes, I see your concern. Currently, we have to implement a large switch
case statement to get this done.

If you need further help, please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Sep 23 '05 #4

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

Similar topics

2
by: baylor | last post by:
This is probably an easy question but i apparently can't find the right Google key words i am passed a string and the name of the data type i'm hoping that string is (technically, i'm passed a...
3
by: Steve Brown | last post by:
Hello all, Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this: template <class T> Object::Object(int TypeCode, T* data)...
2
by: Ed Sutton | last post by:
I have variables that are declared as objects. Ints, strings and other data types are then assigned to these objects. How can I write a compare objects function? I tried objOne==objTwo. I am...
7
by: Jean Stax | last post by:
Hi ! Jeffrey Richter gives the following example in his book: struct Point { Int32 x, y; } class AAA{ .... public virtual void Add(Object value);
9
by: **Developer** | last post by:
I do the following: Select Case mDataType(mColumnToSort) Case TypeCode.Int32 Result = CInt(xText).CompareTo(CInt(yText)) Case TypeCode.String
3
by: HairlipDog58 | last post by:
Does anyone know if there is a built-in function for creating a Type from a TypeCode? Type t = somefunction(TypeCode.Int16);
4
by: Demetri | last post by:
I have a method that has a parameter that accepts a System.Type. How can I test to see if the type passed is one of the numeric types? I want to avoid a big if/else or switch statement if...
3
by: Andy | last post by:
Hi all, I have a situation where I have two objects, and if they are both any kind of numeric, I need to perform some opration on them (such as multiple or divide). I'd like to do so without...
46
by: DragonLord | last post by:
Ok here is the situation i am porting a project from vb.net to C# and i have the following issues and i don't know how to resolve them. VB.NET Public Class TextFieldEnumerator Inherits...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.