Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c# ftp code 3 52
Windows Universal App 22 51
asp.net repeater server posting 5 34
Timeouts during development 3 25
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

740 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