Solved

Should fields be created in classes to hold ID values from list boxes or drop downs?

Posted on 2013-01-07
3
199 Views
Last Modified: 2013-01-09
I would like to get a sense of what the best way is to deal with ID values of drop down lists, list boxes etc.   Should a field be created in the respective class to hold the ID value or would it be better to just pass it through as a parameter to the method that is handling the database interaction or is there an even better way that what I'm suggesting?

Thanks
0
Comment
Question by:obb-taurus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 38753384
I'm not quite sure what are you talking about. Probably you need to clarify your question.

What do you display in this controls? If I display, say, Company, Region etc, i.e. some objects that correspond to database records, I believe these objects should encapsulate this "id information" as a field. And normally this is a read-only field as IDs are set by a DB engine.

Also, business object like 'Company', 'Order', 'StockLine' often have methods like save, e.g.:
 
               Order.save();

Such a method requires all the information for this order stored in business object itself and not passed from somewhere. Normally, if you want to change a field value, you do something like:

           myCompany.Name = "Pokemon Ltd.";

and then save the object to a DB as shown above.
0
 

Author Comment

by:obb-taurus
ID: 38753420
Sorry, I was a little rushed when posting the question.  I think you got the gist of my question, if for example the drop down list is populated from a lookup table in the database that contains States and I have a Customer class which needs State information, should I have a field in the Customer class that will contain the ID value retrieved from the drop down (eg. dropdown.SelectedValue) or is there a better way to handle this situation.

Hopefully that helps.

Thanks
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 38756492
I'd definitely have an "ID" field on the class if you fill the drop down list with objects.
The only problem, I think, how you retrieve selected values. Again, it may depend on are you working in Win Forms or ASP. Say, if you use a combo box in WinForms, and fill your combo in some way like below:

            this.ddlStates.DisplayMember = "Name";
            this.ddlStates.ValueMember = "ID";

            State alabama = new State(1, "Alabama");
            State texas = new State(2, "Texas");
            State washington = new State(3, "Washington");
            this.ddlStates.Items.AddRange(new State[] {alabama, texas, washington });

Open in new window


then 'selectedvalue' and 'selectedtext' do not work. I'm not sure why - MS implementation...

In this case you may use something like:

            if (this.ddlStates.SelectedItem != null)
            {
                MessageBox.Show("Selected value = " + ((State)(this.ddlStates.SelectedItem)).ID + Environment.NewLine +
                            "Selected index = " + this.ddlStates.SelectedIndex + Environment.NewLine +
                             "Selected item = " + ((State)(this.ddlStates.SelectedItem)).Name);
            }

Open in new window



i.e use 'selectedItem' property.

For my tests I used the State class:

    public class State
    {
        private int _id;
        private string _name;

        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public State(int id, string name)
        {
            _id = id;
            _name = name;
        }
    }

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question