Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: C# creating objects

  1. #1
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    C# creating objects

    anyone who can tell me how to create objects when using a constructor and when NOT using a constructor?

    Thanks on advance.
    Kind regards.

  2. #2
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: C# creating objects

    You have to use a constructor. If you do not define it, a default one is provided.
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  3. #3
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C# creating objects

    Quote Originally Posted by cgroza View Post
    You have to use a constructor. If you do not define it, a default one is provided.
    That is not necessarily true. The object may have one or more factory methods from which the object can be created. If you wish to create an object but the constructor is private, look in the documentation if there are any factory methods available.

  4. #4
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C# creating objects

    Quote Originally Posted by cgroza View Post
    You have to use a constructor. If you do not define it, a default one is provided.
    what would the code look like? for example i have a little program, where i try to get better. The program is to make a machine that can give a refreshing drink when you put enough money in the machine.

    And i'm having trouble to create a refreshing drink object. i cannot visualize it.
    Where i then would put the drink in a array to track how many drinks are their, and such.

    Any guides about object making?

  5. #5
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C# creating objects

    Quote Originally Posted by Drenriza View Post
    what would the code look like? for example i have a little program, where i try to get better. The program is to make a machine that can give a refreshing drink when you put enough money in the machine.

    And i'm having trouble to create a refreshing drink object. i cannot visualize it.
    Where i then would put the drink in a array to track how many drinks are their, and such.

    Any guides about object making?
    What language are you programming in?

    I suggest the following objects: a DrinkMachine and a Drink. The DrinkMachine could have a method called purchase with name and an amount of money.

    The DrinkMachine could have a number called capacity which keeps track of the number of drinks in the machine. Whenever a drink is purchased, a Drink object could be created and returned to the caller of the purchase method.

  6. #6
    Join Date
    Jul 2008
    Location
    England
    Beans
    866

    Re: C# creating objects

    Alternatively the DrinkMachine class could have an array of Drink objects. To fill the DrinkMachine class you could have a method:
    add(Drink drink)
    which adds a drink to the DrinkMachine (like when a real person is filling it up).

    When someone buys a drink the purchase method would take an amount of money for the drink and if the amount is enough, the Drink object would be removed from the array in DrinkMachine, and the object would be returned from the function.

    Paul

  7. #7
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C# creating objects

    Quote Originally Posted by simeon87 View Post
    what language are you programming in?
    c#

  8. #8
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: C# creating objects

    Quote Originally Posted by simeon87 View Post
    That is not necessarily true. The object may have one or more factory methods from which the object can be created. If you wish to create an object but the constructor is private, look in the documentation if there are any factory methods available.
    Those methods would be static. Not exactly my definition of constructor. If those methods have to use a private constructor, the user still uses a constructor indirectly.
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  9. #9
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C# creating objects

    Quote Originally Posted by cgroza View Post
    Those methods would be static. Not exactly my definition of constructor. If those methods have to use a private constructor, the user still uses a constructor indirectly.
    Yes, which means that you don't have to use a constructor to create an object. Sometimes objects can only be created by calling a factory method. Internally, a constructor is still used but a constructor is therefore not the only way to create an object (it can be private by design).

  10. #10
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: C# creating objects

    Quote Originally Posted by simeon87 View Post
    Yes, which means that you don't have to use a constructor to create an object. Sometimes objects can only be created by calling a factory method. Internally, a constructor is still used but a constructor is therefore not the only way to create an object (it can be private by design).
    A constructor still has a crucial role in the creation of the object. So the constructor is still used. Hidden by the implementation or not, it is used.
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •