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

Create variable of base types

Im trying to create a variable that can store all possible simple C#
types. This is so I can assign the type to a database column,
regardless of whether its string, int, bool etc. But, Id rather not
cast a string and switch the value, that seems a bit redundant.

So an example would be:

//
SimpleBaseTypes MyType;
MyType = bool;
MyDatabaseColumn.Type = MyType;
//

Is this possible or am I going about this in the wrong way?

Cheers all,

Nick
Nov 17 '05 #1
3 1271
Hi,

The father of all the types is the "object" type, so you can assign whatever
you like to it. The problem is that after the assignation the variable is
cast to object and lost the original type, so if you ask for the type it will
say "object".

C# version 2.0 includes templates that will allow you to do this like C++.

Best regards
Salva

"Nick Weekes" wrote:
Im trying to create a variable that can store all possible simple C#
types. This is so I can assign the type to a database column,
regardless of whether its string, int, bool etc. But, Id rather not
cast a string and switch the value, that seems a bit redundant.

So an example would be:

//
SimpleBaseTypes MyType;
MyType = bool;
MyDatabaseColumn.Type = MyType;
//

Is this possible or am I going about this in the wrong way?

Cheers all,

Nick

Nov 17 '05 #2
Hi,

What you want is done alreayd, Object is the ancestor of ALL the types,
in fact DataTable.Rows[x][ColumnName] is an object this allow you to assign
it no matter the real type fo the column.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Nick Weekes" <ni***********@yahoo.co.uk> wrote in message
news:3c**************************@posting.google.c om...
Im trying to create a variable that can store all possible simple C#
types. This is so I can assign the type to a database column,
regardless of whether its string, int, bool etc. But, Id rather not
cast a string and switch the value, that seems a bit redundant.

So an example would be:

//
SimpleBaseTypes MyType;
MyType = bool;
MyDatabaseColumn.Type = MyType;
//

Is this possible or am I going about this in the wrong way?

Cheers all,

Nick

Nov 17 '05 #3
Salvador <Sa******@discussions.microsoft.com> wrote:
The father of all the types is the "object" type, so you can assign
whatever you like to it. The problem is that after the assignation
the variable is cast to object and lost the original type, so if you
ask for the type it will say "object".


No, no type information is lost. For instance:

using System;

class Test
{
static void Main()
{
int i = 5;
object o = i;

Console.WriteLine (o.GetType().FullName);
}
}

will print "System.Int32", not "System.Object".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

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

Similar topics

4
by: troloo | last post by:
Hello, I hope you can help me :)) The story goes as follows: I have a class with different methods and member variables. I store pointers to objects of this class inside a vector. Now, I would like...
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
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: 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?
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
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...
0
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...
0
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...

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.