Compile time type check in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • n.net

    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?

  • Flinky Wisty Pomm

    #2
    Re: Compile time type check in C#

    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...@gmai l.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?

    Comment

    • n.net

      #3
      Re: Compile time type check in C#

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

      DType = debugger.GetExp ression(DataTyp eExpression,tru e,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" <ksngro...@gmai l.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?

      Comment

      Working...