none
Creating a class of Boat with automatic and non automatic properties in C# RRS feed

  • Question

  • I need a help for automatic and non automatic properties in C#.

    The class should contain the following,

    Properties Length and Weight

    -Length should not be automatic

    -Weight should be automatic

    Public Methods: Sail and StopSail

    -Sail has the Length and Weight parameter, no return value, empty method (//ToDo)

    -StopSail has no return value empty method (//ToDo)

    Friday, April 20, 2018 8:44 AM

All replies

  • This is 100% homework and if it is not, then take first a course in C#. 

    We see more and more that point collectors are giving code for this in the Microsoft forums. Avoiding persons learn themselves how to program.

    It does not help. If you search on Internet for your question you find very quick this page.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties


    Success
    Cor

    Friday, April 20, 2018 9:05 AM
  • Thank you very much for the link
    Friday, April 20, 2018 9:25 AM
  • @Cor Ligthert

    This is what i have done so far, but i have just one problem I need a tips to make the length property non automatic.

    Sincerely

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ques6m3
    {
        class Boats
        {
            // Units kg(kilo grams)
            public int weight { set; get; }

            // Units m(meters)
            public int length { set; get; }

            // Commences sailing
            public void Sail(int length, int weight)
            {
                // TODO
            }

            // Stops sailing
            public void StopSail()
            {
                // TODO
            }
        }

        public class Test
        {
            public static void testBoats()
            {
                Boats boats = new Boats();

                // Dynamic change in values
                boats.length = 170;
                boats.weight = 100;

                // How to call the sail method
                boats.Sail(100, 200);
                boats.StopSail();
            }

            static void Main(string[] args)
            {
                testBoats();
            }
        }
    }

    Saturday, April 21, 2018 8:14 AM
  • But it is really on the page, in C# 7 style and in the link properties this in older style

    public class Whatever
    {
        private int length = 0;
    
        public int Length
        {
            get
            {
                return length;
            }
            set
            {
                if ((value > 0) && (value < 500))
                {
                    length = value;
                }
            }
        }
    }

    I changed it a little bit here in this message


    Success
    Cor


    Saturday, April 21, 2018 9:25 AM
  • Hi Bless4ever,

    In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

    Here is about automatic property:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    When additional logic is required in the property accessors, you need to use no automatic property:

     class Boats
        {
            // Units kg(kilo grams)
            public int weight { set; get; }
    
            // Units m(meters)
            private int length;
            public int _length
            {
                get { return length; }
                set
                {
                    if(value>=0)
                    {
                        length = value;
                    }
                }
            }
    
            // Commences sailing
            public void Sail(int length, int weight)
            {
                // TODO
            }
    
            // Stops sailing
            public void StopSail()
            {
                // TODO
            }
        }

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, April 25, 2018 9:12 AM
    Monday, April 23, 2018 8:38 AM