foreach help

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
 
C

Carl Daniel [VC++ MVP]

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
 
E

Esha

Thanks a lot

Esha

Carl Daniel said:
^^^ delete this line


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


See inline above.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top