Solved

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

Posted on 2016-09-22
8
30 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET e-commerce website 4 57
ASP.NET Web Form Variables being reset 2 26
JavaScript error 1 52
ASP.NET 5 Templates 2 65
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

15 Experts available now in Live!

Get 1:1 Help Now