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

Compile time type check in C#

I am working with .NET 1.1 datatypes in my project in C#.

I ended up writing code a typical waterfall if..else modal.

if ( xType == "System.String")
do this

if ( xType == "System.Integer")
do this

if ( xType == "System.Double")
do that.

If it had been in C++, I would have very well used compile time type
checking. But i don't know how easy to achieve in C# with .NET 1.1
Platform.

Is C# generics is the only solution?

Jan 13 '07 #1
2 2591
Polymorphism is your friend. If you're switching on type (or even an
enum) chances are you're not making the best use of OO.

private void DoThis(string x)
{}

private void DoThis(int x)
{}

private void DoThat(double x)
{}
or, as you point out,

private void DoThat(double x)
{ }

private void DoThis<T>(T x)
{ }
On Jan 13, 1:01 pm, "n.net" <ksngro...@gmail.comwrote:
I am working with .NET 1.1 datatypes in my project in C#.

I ended up writing code a typical waterfall if..else modal.

if ( xType == "System.String")
do this

if ( xType == "System.Integer")
do this

if ( xType == "System.Double")
do that.

If it had been in C++, I would have very well used compile time type
checking. But i don't know how easy to achieve in C# with .NET 1.1
Platform.

Is C# generics is the only solution?
Jan 13 '07 #2
Let me explain little more clearly, I am using a debugger object

DType = debugger.GetExpression(DataTypeExpression,true,300 0).Value;

The DType is nothing but a string containing the data type. How do i
instantiate the function corresponding to the data type contained in
DType String.
Flinky Wisty Pomm wrote:
Polymorphism is your friend. If you're switching on type (or even an
enum) chances are you're not making the best use of OO.

private void DoThis(string x)
{}

private void DoThis(int x)
{}

private void DoThat(double x)
{}
or, as you point out,

private void DoThat(double x)
{ }

private void DoThis<T>(T x)
{ }
On Jan 13, 1:01 pm, "n.net" <ksngro...@gmail.comwrote:
I am working with .NET 1.1 datatypes in my project in C#.

I ended up writing code a typical waterfall if..else modal.

if ( xType == "System.String")
do this

if ( xType == "System.Integer")
do this

if ( xType == "System.Double")
do that.

If it had been in C++, I would have very well used compile time type
checking. But i don't know how easy to achieve in C# with .NET 1.1
Platform.

Is C# generics is the only solution?
Jan 15 '07 #3

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

Similar topics

3
by: Eric Newton | last post by:
I'm wondering if there's a compile time cast gaurantee if any? Given the following: //----- begin sample code public interface IRegion { string Text { get; } RectangleF Area { get; } }
2
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a...
4
by: John F | last post by:
Currently I'm working on a C# app for a large organization (700 users nationwide). This App is still under development. I have setup code in my App to pull C# source code from a database and...
4
by: John Smith | last post by:
Hi I'm porting some C++ code to new platforms and have some 1-byte aligned structures which need a specific size. Since datatypes can vary on different platforms (which I found out the hard way...
14
by: Urs Thuermann | last post by:
What is the most elegant way to check certain conditions at compile time? I.e. I want a compile time error to be generated if for example the size of a struct is not a multiple of 4 or if one...
1
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started:...
4
by: andrewcw | last post by:
I am moving some code forward from .NET 1.1. I was able to load the XSL file and perform the transform. The MSDN documentation looks like it should be easy. But I get a compile error. Ideas ?...
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
14
by: Dave Rahardja | last post by:
Is there a way to generate a series of statements based on the data members of a structure at compile time? I have a function that reverses the endianness of any data structure: /// Reverse...
12
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.