creating a compile time error... is it possible?

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

    creating a compile time error... is it possible?

    Hi,
    I'd like to create a compile time error in my class... maybe there's a way
    already built in in the framework so I can achieve what I want... I have 2
    constructors in my class. One of them has mandatory parameters, I mean, they
    should not be null nor empty (for strings). So I'd make the validation in
    the constructor and generate a compile-time error if the validation does not
    match...

    Is there a way to achieve this or to specify mandatory parameters? (I'm
    using C#)

    thanks

    ThunderMusic



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: creating a compile time error... is it possible?

    ThunderMusic,

    That's easy, just create a syntax error, forget a semi-colon somewhere.

    I think what you really want is a run-time error. If you have a
    compile-time error, then your code doesn't compile, and it certainly doesn't
    run.

    Basically, what you want to do is throw an exception. In the case of an
    argument being null, you want to do something like this:

    public class MyClass
    {
    public MyClass()
    {
    }

    public MyClass(SomeOth erClass other)
    {
    // If other is null, throw an exception.
    if (other == null)
    {
    // Throw an exception.
    throw new ArgumentNullExc eption();
    }
    }
    }

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "ThunderMus ic" <NoSpAmdanlatat hotmaildotcom@N oSpAm.comwrote in message
    news:uTOLzjSxGH A.3264@TK2MSFTN GP03.phx.gbl...
    Hi,
    I'd like to create a compile time error in my class... maybe there's a
    way already built in in the framework so I can achieve what I want... I
    have 2 constructors in my class. One of them has mandatory parameters, I
    mean, they should not be null nor empty (for strings). So I'd make the
    validation in the constructor and generate a compile-time error if the
    validation does not match...
    >
    Is there a way to achieve this or to specify mandatory parameters? (I'm
    using C#)
    >
    thanks
    >
    ThunderMusic
    >
    >
    >

    Comment

    • ThunderMusic

      #3
      Re: creating a compile time error... is it possible?

      What I wanted to do is to have the user (developer) of my class have a
      compile-time error when it supplies null as the value of a parameter when he
      compiles.... like myMethod(param1 , null, param3,...) so param2 should
      generate a compile-time error because it's explicitly null... Is there a
      way to make this validation or I must go with the NullReferenceEx ception?
      Sure, runtime exceptions will have to be implemented because param1 or
      param3 could also be null without being explicit, but as a first time
      validation, I would like to implement compile-time error if possible...

      thanks

      ThunderMusic


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
      message news:e%23KUnpSx GHA.1284@TK2MSF TNGP05.phx.gbl. ..
      ThunderMusic,
      >
      That's easy, just create a syntax error, forget a semi-colon somewhere.
      >
      I think what you really want is a run-time error. If you have a
      compile-time error, then your code doesn't compile, and it certainly
      doesn't run.
      >
      Basically, what you want to do is throw an exception. In the case of
      an argument being null, you want to do something like this:
      >
      public class MyClass
      {
      public MyClass()
      {
      }
      >
      public MyClass(SomeOth erClass other)
      {
      // If other is null, throw an exception.
      if (other == null)
      {
      // Throw an exception.
      throw new ArgumentNullExc eption();
      }
      }
      }
      >
      Hope this helps.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      "ThunderMus ic" <NoSpAmdanlatat hotmaildotcom@N oSpAm.comwrote in message
      news:uTOLzjSxGH A.3264@TK2MSFTN GP03.phx.gbl...
      >Hi,
      >I'd like to create a compile time error in my class... maybe there's a
      >way already built in in the framework so I can achieve what I want... I
      >have 2 constructors in my class. One of them has mandatory parameters, I
      >mean, they should not be null nor empty (for strings). So I'd make the
      >validation in the constructor and generate a compile-time error if the
      >validation does not match...
      >>
      >Is there a way to achieve this or to specify mandatory parameters? (I'm
      >using C#)
      >>
      >thanks
      >>
      >ThunderMusic
      >>
      >>
      >>
      >
      >

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: creating a compile time error... is it possible?

        Hi,

        "ThunderMus ic" <NoSpAmdanlatat hotmaildotcom@N oSpAm.comwrote in message
        news:uEpL$7SxGH A.1284@TK2MSFTN GP05.phx.gbl...
        What I wanted to do is to have the user (developer) of my class have a
        compile-time error when it supplies null as the value of a parameter when
        he compiles


        There is no way to do this. The only moment where you can evaluate your
        parameters is at runtime, you cannot know the possible values at
        compile-time


        --
        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation


        Comment

        • ThunderMusic

          #5
          Re: creating a compile time error... is it possible?

          ok, thanks... ;) after your first post, I realized that, but I thought "hey,
          what if it's possible in a way I don't know..." ;) I'll use the
          exceptions...


          "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us wrote
          in message news:e9xSI%23Sx GHA.5056@TK2MSF TNGP06.phx.gbl. ..
          Hi,
          >
          "ThunderMus ic" <NoSpAmdanlatat hotmaildotcom@N oSpAm.comwrote in message
          news:uEpL$7SxGH A.1284@TK2MSFTN GP05.phx.gbl...
          >What I wanted to do is to have the user (developer) of my class have a
          >compile-time error when it supplies null as the value of a parameter when
          >he compiles
          >
          >
          >
          There is no way to do this. The only moment where you can evaluate your
          parameters is at runtime, you cannot know the possible values at
          compile-time
          >
          >
          --
          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation
          >

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: creating a compile time error... is it possible?

            ThunderMusic,

            What you are looking for is a static analysis tool, which will analyze
            the possible code paths. Needless to say such tools are difficult to get
            right (since they can't possibly get ALL possible code paths for larger,
            more complex applications).


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "ThunderMus ic" <NoSpAmdanlatat hotmaildotcom@N oSpAm.comwrote in message
            news:uwNwFXTxGH A.4460@TK2MSFTN GP04.phx.gbl...
            ok, thanks... ;) after your first post, I realized that, but I thought
            "hey, what if it's possible in a way I don't know..." ;) I'll use the
            exceptions...
            >
            >
            "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
            wrote in message news:e9xSI%23Sx GHA.5056@TK2MSF TNGP06.phx.gbl. ..
            >Hi,
            >>
            >"ThunderMusi c" <NoSpAmdanlatat hotmaildotcom@N oSpAm.comwrote in message
            >news:uEpL$7SxG HA.1284@TK2MSFT NGP05.phx.gbl.. .
            >>What I wanted to do is to have the user (developer) of my class have a
            >>compile-time error when it supplies null as the value of a parameter
            >>when he compiles
            >>
            >>
            >>
            >There is no way to do this. The only moment where you can evaluate your
            >parameters is at runtime, you cannot know the possible values at
            >compile-time
            >>
            >>
            >--
            >--
            >Ignacio Machin,
            >ignacio.mach in AT dot.state.fl.us
            >Florida Department Of Transportation
            >>
            >
            >

            Comment

            • Greg Young

              #7
              Re: creating a compile time error... is it possible?

              Spec# does a great job of handling this case :)

              Cheers,

              Greg
              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
              message news:eyp3yaTxGH A.4872@TK2MSFTN GP02.phx.gbl...
              ThunderMusic,
              >
              What you are looking for is a static analysis tool, which will analyze
              the possible code paths. Needless to say such tools are difficult to get
              right (since they can't possibly get ALL possible code paths for larger,
              more complex applications).
              >
              >
              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m
              >
              "ThunderMus ic" <NoSpAmdanlatat hotmaildotcom@N oSpAm.comwrote in message
              news:uwNwFXTxGH A.4460@TK2MSFTN GP04.phx.gbl...
              >ok, thanks... ;) after your first post, I realized that, but I thought
              >"hey, what if it's possible in a way I don't know..." ;) I'll use the
              >exceptions.. .
              >>
              >>
              >"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
              >wrote in message news:e9xSI%23Sx GHA.5056@TK2MSF TNGP06.phx.gbl. ..
              >>Hi,
              >>>
              >>"ThunderMusic " <NoSpAmdanlatat hotmaildotcom@N oSpAm.comwrote in message
              >>news:uEpL$7Sx GHA.1284@TK2MSF TNGP05.phx.gbl. ..
              >>>What I wanted to do is to have the user (developer) of my class have a
              >>>compile-time error when it supplies null as the value of a parameter
              >>>when he compiles
              >>>
              >>>
              >>>
              >>There is no way to do this. The only moment where you can evaluate your
              >>parameters is at runtime, you cannot know the possible values at
              >>compile-time
              >>>
              >>>
              >>--
              >>--
              >>Ignacio Machin,
              >>ignacio.machi n AT dot.state.fl.us
              >>Florida Department Of Transportation
              >>>
              >>
              >>
              >
              >

              Comment

              • naveed@gmail.com

                #8
                Re: creating a compile time error... is it possible?

                You should probably also look at #error directive, here is the msdn
                link:


                Take care
                Naveed

                Comment

                • Tigger

                  #9
                  Re: creating a compile time error... is it possible?

                  The ref keyword might be able to help you here.

                  "An argument passed to a ref parameter must first be initialized"

                  Comment

                  • Marc Gravell

                    #10
                    Re: creating a compile time error... is it possible?

                    Well, if it is null, then it *is* initialized; and the OP was talking about
                    *explicitely* passing null, in which case the relevant error would be "A ref
                    or out argument must be an assignable variable", but I personally wouldn't
                    advocate making this change; it would preclude, for instance, passing
                    (inline) consts, readonly fields, properties, method results, etc; all sorts
                    of things.

                    Marc


                    Comment

                    Working...