C# constructor

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

    #1

    C# constructor

    Is it correct to define a constructor with a return value in one of the
    paramters as follows -
    public constructor1(bo ol boolvalue, out int intvalue)
    {
    ....
    }
    regrards
    Ronny


  • Hans Kesting

    #2
    Re: C# constructor

    Ronny used his keyboard to write :
    Is it correct to define a constructor with a return value in one of the
    paramters as follows -
    public constructor1(bo ol boolvalue, out int intvalue)
    {
    ...
    }
    regrards
    Ronny
    It is at least *very* unusual, but what happens when you compile it?

    Why not have that "out" value as a readonly (get; private set;)
    property of the constructed class?

    Hans Kesting


    Comment

    • Peter Morris

      #3
      Re: C# constructor

      I've never seen it done, but I can't think why you shouldn't be able to do
      such a thing. Mostly though I am curious as to what you intend to use it
      for, please tell :-)



      --
      Pete
      ====



      Comment

      • =?ISO-8859-1?Q?Bj=F8rn_Brox?=

        #4
        Re: C# constructor

        Peter Morris skrev:
        I've never seen it done, but I can't think why you shouldn't be able to
        do such a thing. Mostly though I am curious as to what you intend to
        use it for, please tell :-)
        >
        I can see many reasons, where one of the reasons is to return some
        status value telling that the constructor actually failed and there is
        no reason to go further, and as we know a constructor cannot return null.

        Classical situation: Some TakePicture diaglog and the constructor
        detects that there is no camera present.

        Personally it is probably smarter to have a get {} property you can test
        on before firing for example ShowDialog().

        --
        Bjørn Brox

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: C# constructor

          I would disagree with this usage. If the constructor fails, then
          allowing the object to be created would be a bad design decision. An
          exception should be thrown to prevent the construction of the object, and
          the calling code should look for this vs a return code (in this case).

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


          "Bjørn Brox" <bpbrox@gmail.c omwrote in message
          news:49186ff8$1 @news.broadpark .no...
          Peter Morris skrev:
          >I've never seen it done, but I can't think why you shouldn't be able to
          >do such a thing. Mostly though I am curious as to what you intend to use
          >it for, please tell :-)
          >>
          I can see many reasons, where one of the reasons is to return some
          status value telling that the constructor actually failed and there is
          no reason to go further, and as we know a constructor cannot return null.
          >
          Classical situation: Some TakePicture diaglog and the constructor
          detects that there is no camera present.
          >
          Personally it is probably smarter to have a get {} property you can test
          on before firing for example ShowDialog().
          >
          --
          Bjørn Brox

          Comment

          • Peter Morris

            #6
            Re: C# constructor

            The only way for a constructor to fail is to throw an exception isn't it?



            --
            Pete
            ====


            Comment

            • =?ISO-8859-1?Q?Bj=F8rn_Brox?=

              #7
              Re: C# constructor

              Peter Morris skrev:
              The only way for a constructor to fail is to throw an exception isn't it?
              >
              Well, fail is probably the wrong word in my example above, - it is just
              useless to continue, and better to show a decent error/warning message,
              but I agree on the conclusion that it is best to
              throw an exception.

              --
              Bjørn Brox

              Comment

              • Peter Duniho

                #8
                Re: C# constructor

                On Mon, 10 Nov 2008 12:39:26 -0800, Peter Morris
                <mrpmorrisNO@sp amgmail.comwrot e:
                The only way for a constructor to fail is to throw an exception isn't it?
                It depends on how you define "fail".

                That certainly is the canonical way, and in the sense that it's the only
                way to prevent an object reference from being returned to the code site
                instantiating the object with "new", you're right. But exceptions are far
                from the only way to report a failure, and a class _could_ decide to
                always allow the object to be instantiated and require the caller to check
                some other state, such as the value in an "out" argument, a property,
                calling some validation method, or even checking some global state (the
                first two were even mentioned as possible methods in this thread already).

                All of those approachs, and others not even mentioned, could be considered
                a legitimate way to report "failure".

                Pete

                Comment

                • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                  #9
                  Re: C# constructor

                  Ronny wrote:
                  Is it correct to define a constructor with a return value in one of the
                  paramters as follows -
                  public constructor1(bo ol boolvalue, out int intvalue)
                  {
                  ...
                  }
                  regrards
                  Ronny
                  >
                  The out keyword is rarely used in that way, as has been mentioned. An OO
                  approach is usually preferred.

                  An exception would be used if the constructor should actually fail. If
                  the condition is not really a failure, you could use a static method
                  that would either call the constructor to create an instance, or return
                  a null reference.

                  --
                  Göran Andersson
                  _____
                  Göran Anderssons privata hemsida.

                  Comment

                  • Arto Viitanen

                    #10
                    Re: C# constructor

                    Ronny kirjoitti:
                    Is it correct to define a constructor with a return value in one of the
                    paramters as follows -
                    public constructor1(bo ol boolvalue, out int intvalue)
                    {
                    ...
                    }
                    regrards
                    Ronny
                    >
                    >
                    System.Threadin g namespace's Mutex class has a constructor that uses out
                    on parameters. Mutex(bool initiallyOwned, string name, out bool
                    createdNow) returns createdNow that tells if the calling thread was
                    granted initial ownership of the mutex.

                    --
                    Arto Viitanen

                    Comment

                    Working...