foreach help

  • Thread starter Thread starter esha
  • Start date Start date
E

esha

I tried to translate VB code for FOREACH and got this result:

public static string GetUnauthorizedUsersCount() {
MembershipUser objUser;
MembershipUserCollection objUserCollection = Membership.GetAllUsers();
MembershipUserCollection objUsers = new MembershipUserCollection();

foreach (objUser in objUserCollection)
{
if ((objUser.IsApproved == false))
{
objUsers.Add(objUser);
}
}
if ((objUsers.Count > 0))
{
return objUsers.Count.ToString();
}
else {
return "0";
}
}

Compiler complains about 'in' in foreach (objUser in objUserCollection)
'Type and identifier are both required in a foreach statement '

If I put MembershipUser as a type I get another error:
'A local variable named 'objUser' cannot be declared in this scope because
it would give a different meaning to 'objUser', which is already used in a
'parent or current' scope to denote something else

How should I rewrite this FOREACH?

Thank you

Esha
 
esha said:
I tried to translate VB code for FOREACH and got this result:

public static string GetUnauthorizedUsersCount() {
MembershipUser objUser;

^^^ delete this line
MembershipUserCollection objUserCollection = Membership.GetAllUsers();
MembershipUserCollection objUsers = new MembershipUserCollection();

foreach (objUser in objUserCollection)

^^^ replace this line with:
foreach (MembershipUser objUser in objUserCollection)
{
if ((objUser.IsApproved == false))
{
objUsers.Add(objUser);
}
}
if ((objUsers.Count > 0))
{
return objUsers.Count.ToString();
}
else {
return "0";
}
}

Compiler complains about 'in' in foreach (objUser in
objUserCollection)
'Type and identifier are both required in a foreach statement '

If I put MembershipUser as a type I get another error:
'A local variable named 'objUser' cannot be declared in this scope because
it would give a different meaning to 'objUser', which is already used in a
'parent or current' scope to denote something else

How should I rewrite this FOREACH?

See inline above.

-cd
 
Thanks a lot

Esha

Carl Daniel said:
^^^ delete this line


^^^ replace this line with:
foreach (MembershipUser objUser in objUserCollection)


See inline above.

-cd
 
Back
Top