Compile time type check in C#

Discussion in 'ASP .Net' started by n.net, Jan 13, 2007.

  1. n.net

    n.net Guest

    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?
    n.net, Jan 13, 2007
    #1
    1. Advertising

  2. 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" <> wrote:
    > 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?
    Flinky Wisty Pomm, Jan 13, 2007
    #2
    1. Advertising

  3. n.net

    n.net Guest

    Let me explain little more clearly, I am using a debugger object

    DType = debugger.GetExpression(DataTypeExpression,true,3000).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" <> wrote:
    > > 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?
    n.net, Jan 15, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Pilcher
    Replies:
    4
    Views:
    409
    Ian Pilcher
    Jan 28, 2004
  2. flamesrock
    Replies:
    8
    Views:
    438
    Hendrik van Rooyen
    Nov 24, 2006
  3. Nagaraj
    Replies:
    1
    Views:
    840
    Lionel B
    Mar 1, 2007
  4. er
    Replies:
    2
    Views:
    942
  5. Carter
    Replies:
    2
    Views:
    490
    Carter
    Mar 4, 2009
Loading...

Share This Page