Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7595033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:34:12+00:00 2026-05-30T21:34:12+00:00

I’m learning about accessor methods and enumeration. I wrote a public class ‘Car’ under

  • 0

I’m learning about accessor methods and enumeration. I wrote a public class ‘Car’ under the namespace ‘Vehicles’, and set private properties such as _manufacturer, _model, _year and _color. I’d like to write a single method to access properties and another to set/update them. This is my class:

using System;

namespace Vehicles
{
    public class Car
    {
        private string _manufacturer;
        private string _model;
        private string _year;
        private string _color;

        public void honkHorn()
        {
            // Add argument for a file name?
            // Code here to play a WAV file?
            MessageBox.Show("Honk!");
        }

        public string getCarInfo(string whichProperty)
        {
            switch (whichProperty)
            {
                case ("manufacturer"):
                   return _manufacturer;
                case ("model"):
                    return _model;
                case ("year"):
                    return _year;
                case ("color"):
                    return _color;
                default:
                    return null;
            }
        }

        public void setCarInfo(string whichProperty, string newValue)
        {
            switch (whichProperty)
            {
                case ("manufacturer"):
                    _manufacturer = newValue;
                    break;
                case ("model"):
                    _model = newValue;
                    break;
                case ("year"):
                    _year = newValue;
                    break;
                case ("color"):
                    _color = newValue;
                    break;
            }
        }
    }
}

And this is my form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Vehicles;

namespace CS_Enumeration
{
    public partial class Form1 : Form
    {
        public Car myCar = new Car();

        public Form1()
        {
            InitializeComponent();

            myCar.setCarInfo("manufacturer", "Ford");
            labelManfValue.Text = myCar.getCarInfo("manufacturer");

            myCar.setCarInfo("model", "Ranger");
            labelModelValue.Text = myCar.getCarInfo("model");

            myCar.setCarInfo("year", "2012");
            labelYearValue.Text = myCar.getCarInfo("year");

            myCar.setCarInfo("color", "Blue");
            labelColorValue.Text = myCar.getCarInfo("color");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myCar.honkHorn();
        }
    }
}

Is this really the best way to write a single method that can get/set? I first tried to cast a string value that matched the name of the object property and return the actual property, but that doesn’t work (unless someone knows how to cast a string to an object property?).

Thanks for the replies. This is all an exercise from a book I’m reading. It goes so far as to says that not everything should be public, but not everything should be private either. So how do I know when things should/should not be public/private? Sounds like the book is leading me in the wrong direction as to what’s good coding design. Anyone have any book suggestions for learning good coding design practices for Visual C#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:34:14+00:00Added an answer on May 30, 2026 at 9:34 pm

    Don’t do this.

    Use public properties instead and you gain type safety and a much more expressive usage of your class. In your current approach any typo in the property name string will cause a run-time exception instead of a compilation error.

    Just use properties:

    public class Car
    {
       public string Manufacturer {get; set;}
       public string Model {get; set;}
       public string Year {get; set;}
       public string Color {get; set;}
    
      //..
    }
    

    Now you can just access the properties directly:

    myCar.Manufacturer  = "Ford";
    labelManfValue.Text = myCar.Manufacturer;
    

    Also you should define a constructor that fully initializes a Car object, otherwise you might have some properties set, and others not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.