Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regarding Contructors

Regarding Contructors

Scheduled Pinned Locked Moved C#
csharphelpquestion
10 Posts 6 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi guys, Is a Constructor only accessible by the members of a function in a particular class? Suppose, I have a class where I've two functions. If I declare a constructor in one of the functions, wont it be accessible by the other one? Somebody please explain it to me. I am greatly confused!:suss: Your help will be truly appreciated, Rajdeep.NET

    L P 2 Replies Last reply
    0
    • L Lost User

      Hi guys, Is a Constructor only accessible by the members of a function in a particular class? Suppose, I have a class where I've two functions. If I declare a constructor in one of the functions, wont it be accessible by the other one? Somebody please explain it to me. I am greatly confused!:suss: Your help will be truly appreciated, Rajdeep.NET

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can declare constructor in a Class, not in Function.

      L 1 Reply Last reply
      0
      • L Lost User

        You can declare constructor in a Class, not in Function.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        stancrm wrote:

        You can declare constructor in a Class, not in Function.

        Hi Stancrm, so, if I declare a constructor in a class, will it be accessible by all the functions present in the that class? Is it also accessible by other classes? :^)

        S OriginalGriffO 2 Replies Last reply
        0
        • L Lost User

          stancrm wrote:

          You can declare constructor in a Class, not in Function.

          Hi Stancrm, so, if I declare a constructor in a class, will it be accessible by all the functions present in the that class? Is it also accessible by other classes? :^)

          S Offline
          S Offline
          Samer Aburabie
          wrote on last edited by
          #4

          Hi, I am not sure what is the question is ... constructors can be declared to create a new object of that class ... they can by public (accessible to outer classes) or private (only to the same class) now its not commonly used to declare a private constructor ... most common case is when you are creating a singleton object (read about singleton design pattern)... but can we use constructors in the same class ? Yes we can ... no roles can forbid doing so ...

          Sincerely Samer Abu Rabie Imagination is more important than knowledge !

          1 Reply Last reply
          0
          • L Lost User

            Hi guys, Is a Constructor only accessible by the members of a function in a particular class? Suppose, I have a class where I've two functions. If I declare a constructor in one of the functions, wont it be accessible by the other one? Somebody please explain it to me. I am greatly confused!:suss: Your help will be truly appreciated, Rajdeep.NET

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Look, you've been posting basic questions all over this forum. Here's an idea - rather than running against a problem, posting it here on CP and then potentially waiting hours for somebody to reply; why not actually go out and buy a book that covers the basics? Then everytime you run up against something you don't know the answer to, you've got two choices; 1. read the book; 2. whack yourself around the head with the book until the urge to post a basic question is gone.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            L 1 Reply Last reply
            0
            • L Lost User

              stancrm wrote:

              You can declare constructor in a Class, not in Function.

              Hi Stancrm, so, if I declare a constructor in a class, will it be accessible by all the functions present in the that class? Is it also accessible by other classes? :^)

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Just what use do you suppose a constructor would be if you couldn't access it from outside the class? Go read a book on C# and/or programming.

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              L N 2 Replies Last reply
              0
              • OriginalGriffO OriginalGriff

                Just what use do you suppose a constructor would be if you couldn't access it from outside the class? Go read a book on C# and/or programming.

                No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                In a Factory pattern, you would hide constructors by making them protected or private, then provide a public CreateSomeObject() method that actually invokes the appropriate constructor. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                1 Reply Last reply
                0
                • P Pete OHanlon

                  Look, you've been posting basic questions all over this forum. Here's an idea - rather than running against a problem, posting it here on CP and then potentially waiting hours for somebody to reply; why not actually go out and buy a book that covers the basics? Then everytime you run up against something you don't know the answer to, you've got two choices; 1. read the book; 2. whack yourself around the head with the book until the urge to post a basic question is gone.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Has been suggested many times already, to no avail. Also good replies don't sink in. I stopped reading his posts. And his replies to other posts. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  L 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Just what use do you suppose a constructor would be if you couldn't access it from outside the class? Go read a book on C# and/or programming.

                    No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                    N Offline
                    N Offline
                    Nagy Vilmos
                    wrote on last edited by
                    #9

                    There are plenty of reasons for keeping a constructor private within a class, you just need to know why. Examples - 0. Singleton or cached class where you want to control the instatiation through a static method. The method is public, the constructor private. 1. You have several public constructors and wish to use one combined super constructor that talkes EVERY parameter, however you don't want to make the super constructor available - again you keep it private.


                    Panic, Chaos, Destruction. My work here is done.

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Has been suggested many times already, to no avail. Also good replies don't sink in. I stopped reading his posts. And his replies to other posts. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      But they sure make for good reading on a Friday afternoon!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups