Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8069589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:05:56+00:00 2026-06-05T13:05:56+00:00

I am trying to figure out something. I have a method which adds some

  • 0

I am trying to figure out something. I have a method which adds some items into a ComboBox named “cbSize”. I realize that if I add two types of data into it, the code will crash. Is this because a ComboBox can only accommodate one type of data?

items.Add(1);
items.Add(10);
items.Add(100);
items.Add(2);
items.Add(20);
items.Add(3);
items.Add(30);       //works fine if add numbers only

//items.Add("4");    //will crash if mix both numbers and text
//items.Add("2");    //works fine if add text only

//then sort them out
items.Sort();

//now clear original cbSize items
cbSize.Items.Clear();

//and add them back in sorted order
cbSize.Items.AddRange(items.ToArray());

//gotta clear ArrayList for the next time or else things will add up
items.Clear();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:05:59+00:00Added an answer on June 5, 2026 at 1:05 pm

    Is this because a ComboBox can only accommodate one type of data?

    No, try below it will work

    cbSize.Items.Add("44");
    cbSize.Items.Add(44);
    

    problem is with your items collection, it is type safe. you can’t add different types to it.

    try with list of objects. it will work. reason is both int and string are objects

    List<object> items = new List<object>();
    items.Add(1);
    items.Add(30);     
    items.Add("4");    
    items.Add("2");    
    
    //since you have string and int value you need to create custom comparer
    items.Sort((x, y) => Convert.ToInt32(x).CompareTo(Convert.ToInt32(y)));
    
    //now clear original cbSize items
    cbSize.Items.Clear();
    
    //and add them back in sorted order
    cbSize.Items.AddRange(items.ToArray());
    

    OR you can use ArrayList class (not type-safe because it can store any object)

    var integers = new ArrayList();
    integers.Add(1);
    integers.Add(2);
    integers.Add("3");
    comboBox1.Items.AddRange(integers.ToArray());
    
    • What is type-safe in .net?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do a simple update, but can't figure out somthing. I have
I am trying to figure out something - Every time I use AVAudioPlayer I
Trying to figure out the best way to do this: Should I do something
I'm trying to figure out how to show something like Showing 1-10 of 52
I'm trying to figure out how to do something similar to the twitter silverlight
I'm working on a little something and I am trying to figure out whether
Trying to figure out which to use.
i've been trying to figure this out and i have absolutely no clue why
I am trying to figure out which I should be using. On closing my
I'm fairly new to CodeIgniter, and am trying to figure out something I feel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.