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
Solved

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

Posted on 2013-01-07
3
198 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 29

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 29

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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