Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-07
3
Medium Priority
?
217 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
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

927 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