Solved

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now