Solved

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

Posted on 2016-09-22
8
34 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
ID: 41811486
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
ID: 41811502
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
ID: 41811511
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
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.

 

Author Comment

by:mcrmg
ID: 41811521
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
 
LVL 20

Accepted Solution

by:
Russ Suter earned 500 total points
ID: 41811528
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
ID: 41811537
my boss is kicking everyone out now...I will try it in the AM and post back..thank you
0
 

Author Comment

by:mcrmg
ID: 41811548
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
ID: 41812527
Thank you very much.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Initial get page response times and IIS8 15 55
IIS redirect 1 77
transaction in asp.net, sql server 6 43
asp.net input html code have it display the content 2 22
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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