Solved

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

Posted on 2016-09-22
8
32 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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