DO get/set require a semicolen before the braces?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • YoungProgrammer
    New Member
    • Mar 2011
    • 2

    DO get/set require a semicolen before the braces?

    [CODE]
    set
    {
    this.loc.X = ValueType;
    }
    Error 1 ; expected Rect.cs 39 16 Lab 4_2
  • VijaySofist
    New Member
    • Jun 2007
    • 107

    #2
    Hi,

    Please post your full code here.

    Please find the Property Sample Below.

    Code:
    using System;
    
    public class Customer
    {
        private int m_id = -1;
    
        public int ID
        {
            get
            {
                return m_id;
            }
            set
            {
                m_id = value;
            }
        }
    
        private string m_name = string.Empty;
    
        public string Name
        {
            get
            {
                return m_name;
            }
            set
            {
                m_name = value;
            }
        }
    }
    
    public class CustomerManagerWithProperties
    {
        public static void Main()
        {
            Customer cust = new Customer();
    
            cust.ID = 1;
            cust.Name = "Amelio Rosales";
    
    	Console.WriteLine(
                "ID: {0}, Name: {1}",
                cust.ID,
                cust.Name);
    
            Console.ReadKey();
        }
    }
    Regards
    Vijay.R

    Comment

    • YoungProgrammer
      New Member
      • Mar 2011
      • 2

      #3
      Here, even though i know there are still other things wrong with some of this code. The error messages keep asking for semicolons after the get and set except for getFillColor

      [code]
      using System;
      using System.Collecti ons.Generic;
      using System.Linq;
      using System.Text;
      using System.Drawing;

      namespace Lab_4_2
      {
      class Rect
      {
      Point loc;
      Size dimentions;
      Color fill;
      public Rect(int x, int y, int width, int height)
      {
      ...
      }
      public void Draw(Graphics g)
      {
      ...
      }
      public Color getFillColor
      {
      get { return this.fill; }
      set { this.fill = Color; }
      }
      public static int X()
      {
      get;
      {
      return this.loc.X;
      }
      set
      {
      this.loc.X = ValueType;
      }
      }

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        You've got brackets for your X property. You don't need those. You also don't need a semi-colon after get since you're implementing the method. The only time you don't need this is when you use auto-implemented properties, which is when you don't define the method.

        Example:
        Code:
        public class Person
        {
          private int m_age;
        
          // This is an auto-implemented property (requires VS 2008 and up
          public string Name { get; set; }
        
          // This property is manually implemented
          public int Age
          {
            get { return m_age; }
            set { m_age = value; }
          }
        }
        Also, in the set for X, you're assigning the X property of the location variable to ValueType. In C# the correct keyword is value, as in the above example.

        You can look at my examples, those posted by Vijay, and your own getFillColor property for correct examples of how properties are used.

        Good luck!

        Comment

        Working...