Solved

'string' does not contain a definition for 'Items'

Posted on 2016-09-22
8
29 Views
Last Modified: 2016-09-23
I get the myCustomParameterValue as a dynamic value when user clicks on a button. the value is a string, and I need to assign it so it has Items property (I am not sure if I explain it correctly).  But as you can see, "myCustomParameterValue" has been used, I am not sure how I can assign it.  Thanks

            string myCustomParameterValue = attributColl["CustomParameter"];

            testBox.Text = myCustomParameterValue;

            ListBox myCustomParameterValue = new ListBox();
            foreach (var item in myCustomParameterValue.Items.Cast<ListItem>().Reverse())
            {
              //  mainlist.Items.Add(item);
              //  myCustomParameterValue.Items.Remove(item);
            }

Open in new window

0
Comment
Question by:mcrmg
  • 5
  • 3
8 Comments
 
LVL 20

Expert Comment

by:Russ Suter
Comment Utility
In your code you are creating an instance of a string and assigning it to the variable named myCustomParameterValue. Later you are creating an instance of a ListBox control and assigning it to a variable of the same name. The foreach loop has nothing to do since the ListBox you just created has no items associated.
0
 

Author Comment

by:mcrmg
Comment Utility
The first myCustomParameterValue is a string and I need to assign it to a ListBox control so it can be used in foreach loop.  Is there a way to do it?  thanks
0
 
LVL 20

Expert Comment

by:Russ Suter
Comment Utility
You cannot assign a single string value to be the data source of a ListBox control. That doesn't actually make much sense. If you could do that then you'd have a LIstBox which contains exactly one item. If your string is delimited in some way, like with a comma, you could split on that character and create a string[] object. The string array could then be set as the DataSource property of the ListBox control.
0
 

Author Comment

by:mcrmg
Comment Utility
I see.  That string is actually a CustomParameter from a button. I just want to have this function to be shared by different buttons instead of creating several functions that are doing the same thing.
0
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.

 
LVL 20

Accepted Solution

by:
Russ Suter earned 500 total points
Comment Utility
I see no reason why you can't call this function from multiple origins. The issue is that you need to have some sort of collection, be it a simple array, a List<> object, or something that implements the IEnumerable interface and then use the DataSource property of the ListBox control to assign that collection. If your input is a single string that is OK but you'd need to put it into a List<string> or a string[] array. A list or array containing only one item is perfectly legitimate. Something like this (off the top of my head)
string[] testData = new string[] { "Hello world" };
ListBox myListBox = new ListBox();
myListBox.DataSource = testData;
... etc ...

Open in new window

0
 

Author Comment

by:mcrmg
Comment Utility
my boss is kicking everyone out now...I will try it in the AM and post back..thank you
0
 

Author Comment

by:mcrmg
Comment Utility
This is what I did real quick, it looks like fireach is empty. But if I replace "myListBox" as name of listbox, the code works..thanks

string[] testData = new string[] { myCustomParameterValue };
            ListBox myListBox = new ListBox();
            myListBox.DataSource = testData;

            foreach (var item in myListBox.Items.Cast<ListItem>().Reverse())
            {
                mainlist.Items.Add("Sally");
                mainlist.Items.Add(item);
                myListBox.Items.Remove(item);
            }

Open in new window

0
 

Author Closing Comment

by:mcrmg
Comment Utility
Thank you very much.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

17 Experts available now in Live!

Get 1:1 Help Now