ScriptUO
Casa de TrailMyx => Programming / Operating Systems => .NET Programming => Topic started by: OMGBurgers on August 13, 2008, 09:28:50 PM
-
I'm following this C# tutorial for doing some user roles setting in my database and it gives me the following code:
(Line 13 is the trouble line)
1 private void CheckRolesForSelectedUser()
2 {
3 // Determine what roles the selected user belongs to
4 string selectedUserName = UserList.SelectedValue;
5 string[] selectedUsersRoles = Roles.GetRolesForUser(selectedUserName);
6
7 // Loop through the Repeater's Items and check or uncheck the checkbox as needed
8 foreach (RepeaterItem ri in UsersRoleList.Items)
9 {
10 // Programmatically reference the CheckBox
11 CheckBox RoleCheckBox = ri.FindControl("RoleCheckBox") as CheckBox;
12 // See if RoleCheckBox.Text is in selectedUsersRoles
13 if (selectedUsersRoles.Contains<string>(RoleCheckBox.Text))
14 RoleCheckBox.Checked = true;
15 else
16 RoleCheckBox.Checked = false;
17 }
18 }
Well I punch it in and run it and I get a error "'System.Array' does not contain a definition for 'Contains'" (Line 13)
Well the tutorial then says:
Note: The selectedUserRoles.Contains<string>(...) syntax will not compile if you are using ASP.NET version 2.0. The Contains<string> method is part of the LINQ library, which is new to ASP.NET 3.5. If you are still using ASP.NET version 2.0, use the Array.IndexOf<string> method instead.
So I click the link to that array.indexof thing and I just can't figure it out haha.
I've changed that line 13 to something like:
if (Array.IndexOf<string>(selectedUsersRoles, RoleCheckBox.Text))
But I know it's still not right. I haven't really covered much of the C# itself yet, I just realized that I didn't complete a few pages of this tutorial in asp.net & want to do it haha.
-
Ok so I came up with this:
if (Array.IndexOf<string>(selectedUsersRoles, RoleCheckBox.Text) != -1)
I went ahead and ran it and had no errors so no idea... lol. I'll do the next part real quick which sets the roles & give it a go and see if it loads correctly.
-
And just to tripple post....
It works! hah.
It loads them up perfectly. I don't really know exactly how i came up with that since i have no clue what that <string> part means ;(. All I know is it returns a value as far as the location of the rolecheckbox.text inside the selecteduserroles string.
Eh oh well. One day ill laugh at this!
-
For the first example you posted, you can just probably get rid of the <string> all together because that information is implied in the array selectedUsersRoles.
-
I can't wait to get started with the C# tutorials. Then I won't have this problem hopefully haha. 3 pages of this asp.net stuff left. I wana learn the c# after I set up all this basic crap so I can go back in and fine tune all these arguments, add in more, etc. So long as I know it functions now I'm set lol.
-
Yah, it's been a fun experience for me. There are several language constructs that I've always wished were available in other languages. Collections are something that I love and have always been responsible for creating myself, but you get them for free. So things like Dictionaries, Lists, Stacks make data storage/manipulation a breeze.