Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Can anybody please tell me how to call a recursive function in list box?

I have a user group by name lrg_prod, which contains 50 employee names. I have to load these employee's names into a list box of my front end screen.

my function GetUsersByGroup is like this:
public static Dictionary<string,> GetUsersByGroup(string group)
{
  Search s = new FWD2Search { Username = DataKey.ldapCredentialValue, Password = DataKey.ldapPassword };
  Group grp = s.GetGroup(group);
  Dictionary<string,> list = grp.PeopleInGroup;
  return list;
}

and my frontend screen newusergroup.cs file code is like this:
public NewUserGroup()
{
  string strGroupName = "lrg_prod";
  string[] employees = Helper.GetUsersByGroup(strGroupName);
  foreach(string employee in employees)
  {
    lbUsersList.Items.Add(employee);
  }
}
Posted
Updated 28-Dec-10 3:23am
v2
Comments
JF2015 28-Dec-10 9:23am    
Added code formatting.

No recursion in sight however I look at it, but you seem to be showing us code that doesn't even compile. Your function GetUsersByGroup returns a Dictionary and in your front end code you try to store that into a string array.
If the code doesn't fit, don't just quit: Try again and please point out what you mean by "using recursive function", you've gotten me curious now. :)

Cheers,

Manfred
 
Share this answer
 
v2
Look at your title of the question, I think that you don't want to add items to ListBox with for loop? And you want to use recursive function to add items into it?

But there are 2 points need to consider:

1) If you want to use recursive function, you cannot use Constructor for recursion.
2) The function GetUsersByGroup has return type is Dictionary<type,>. But in the constructor you set it to string[] type. It's not correct.

If you want to use recursive, please try:

C#
public static Dictionary<string,> GetUsersByGroup(string group)
{
  Search s = new FWD2Search { Username = DataKey.ldapCredentialValue, Password = DataKey.ldapPassword };
  Group grp = s.GetGroup(group);
  Dictionary<string,> list = grp.PeopleInGroup;
  return list;
}


And

C#
private const string strGroupName = "lrg_prod";
        private static Dictionary<string, > employees = GetUsersByGroup(strGroupName);
        private static int count = employees.Count;
        public static void AddItems()
        {
            count--;
            if(count >= 0){           lbUsersList.Items.Add(employees.ElementAt(count));//lbUsersList.Items.Add(employees.ElementAt(count).Key);
            }
            if (count > 0)
            {
                AddItems();
            }
            else
            {
                return;
            }
        }



Don't use Constructor to add items if you want to use recursive function.

Hope it helps you.

Andy
 
Share this answer
 
v3
Comments
jerrykid 28-Dec-10 21:01pm    
Please notice, and carefully with static variable and function. You can change it to non-static if you want :)
Hi Andy,

I used the above code, but i cant able to declare public or static modifier in my function, it is showing as a invalid expression term, i can use only the default modifier, but it is loading the data of one record..
Because am not able to call AddItems()function inside the second if condition, as the declaration is giving error..
So will u pls help how can i do this to load all the employees data.

if it is in a loop it would be helpful..
even i tried for and while loop but it is giving exception for the above functionality in my application, so will u pls tell me how can i do this...
 
Share this answer
 
Example:

C#
public class YourClassName{
        public static Dictionary<string,> GetUsersByGroup(string group)
        {
            Search s = new FWD2Search { Username =              DataKey.ldapCredentialValue, Password = DataKey.ldapPassword };
  Group grp = s.GetGroup(group);
  Dictionary<string,> list = grp.PeopleInGroup;
  return list;
}

        private const string strGroupName = "lrg_prod";
        private static Dictionary<string, > employees = GetUsersByGroup(strGroupName);
        private static int count = employees.Count;
        public static void AddItems()
        {
            count--;
            if(count >= 0){           lbUsersList.Items.Add(employees.ElementAt(count));//lbUsersList.Items.Add(employees.ElementAt(count).Key);
            }
            if (count > 0)
            {
                AddItems();
            }
            else
            {
                return;
            }
        }

}


How about this code?
 
Share this answer
 
Hi,
This is what the same code,

Am telling u na, in my function, it is not possible to declare access modifier for any datafield or any declaration, it is giving error like this declaration is not allowed here...

so i couldn't able to call AddItems() function inside if(count>0)loop,
even i try to do this by using for loop like
for(count--;count>=0;)
{
lbUsersList.Items.Add(employees.ElementAt(count));
}
or while loop like this

while(count>=0)
{
lbUsersList.Items.Add(employees.ElementAt(count));
count--;
}
both loops are giving runtime error(exception), so will u tell me how to achieve it..

Because if i use ur example, it will load single users data, as that AddItems() function is not calling from if() loop..


I hope u understand it well now...
 
Share this answer
 
Comments
jerrykid 29-Dec-10 2:47am    
Or post the full codes you used
jerrykid 29-Dec-10 2:47am    
Can you post the error message? In my program, it does not have error
Ahhh, I know what cause to the exception, the code
C#
lbUsersList.Items.Add(employees.ElementAt(count));


cause to error when loading item. Because employees.ElementAt(count) is KeyValuePair object.

Please try
C#
lbUsersList.Items.Add(employees.ElementAt(count).Key);
 
Share this answer
 
See this is what my code, i have used value.wholename to load the users name from the group, that is not causing the exception.

I told u na that if condition is running successfully, but i couldn't able to call function inside if() condition, as this is not possible in my function.

while (count >= 0)
            {
                lbUsersList.Items.Add(username.ElementAt(count).Value.WholeName);
                count--;
            }



so tell me how to do that inside if() loop or else within for loop
 
Share this answer
 
I see the code

C#
while (count >= 0)
            {
                lbUsersList.Items.Add(username.ElementAt(count).Value.WholeName);
                count--;
            }


This is wrong, please use:

C#
while (count >= 0)
            {
                count--;

lbUsersList.Items.Add(username.ElementAt(count).Value.WholeName);
                
            }


And/or:
C#
for(count--;count>=0;)
{
lbUsersList.Items.Add(employees.ElementAt(count));
}


It's wrong, please use:

C#
for(int i=0; i<count;i++)>
{
lbUsersList.Items.Add(employees.ElementAt(i));
}
 
Share this answer
 
v3
public void LoadListBoxData()
       {
           string strGroupName = "lrg_prod";
           Dictionary<string,> username = Helper.GetUsersByGroup(strGroupName);
           int count = username.Count;

          public void AddItems()//this declaration is not possible as it is not allowing the access modifier to declare here.
           {
           count--;

           if (count >= 0)
           {
               lbUsersList.Items.Add(username.ElementAt(count).Value.WholeName);  // It is loading the data of the single user of that group
           }
           if(count>0)
           {
               AddItems();// this function is not called
           }
           else return;
           }


this one is running successfully, but this AddItems() is not calling from that if() loop, so tell me how to do this, to display all the user name inside the listbox.
 
Share this answer
 
Haha,

If you post this function sooner, we don't leave much time to discuss. I see the error: You put the function AddItems inside the other function named LoadListBoxData, it's not possible.

Please use:

C#
public void LoadListBoxData()
   {
        AddItems();
   }

string strGroupName = "lrg_prod";
            Dictionary<string,> username = Helper.GetUsersByGroup(strGroupName);
            int count = username.Count;
           public void AddItems()//this declaration is not possible as it is not allowing the access modifier to declare here.
            {
            count--;
            if (count >= 0)
            {
                lbUsersList.Items.Add(username.ElementAt(count).Value.WholeName);  // It is loading the data of the single user of that group
            }
            if(count>0)
            {
                AddItems();// this function is not called
            }
            else return;
            }


All these codes is inside the Class
 
Share this answer
 
v2
Hi,

I got the solution, but now i wanted to take the group name programmatically, instead of odd-coding, like this..

I have a folder by name datakey, where i mentioned the groupname and execution location like(Prod,Test,etc), now i want to load the usernames based on the execution location, i have used a below code, but it is giving xmlparseexception, am not getting the reason for it, pls will u give me a solution for this...

public NewUserGroup()
        {
            InitializeComponent();           
            GetGroupMembers();                              
        }


void GetGroupMembers()
        {
            GetGroupExecutionLocation();
            Dictionary<string,> username = Helper.GetUsersByGroup(strGroupName);
            int count = username.Count;
            for (int i = 0; i < count; i++)
            {
                lbUsersList.Items.Add(username.ElementAt(i).Value.WholeName);
            }
         }


void GetGroupExecutionLocation()
        {
            if (DataKey.StrExecutionEnvironment == DataKey.TEST)
            {
                strGroupName = DataKey.GROUP_TEST;
            }
            if (DataKey.StrExecutionEnvironment == DataKey.PROD)
            {
                strGroupName = DataKey.GROUP_PROD;
            }
                        else return;
        }
 
Share this answer
 
Comments
jerrykid 30-Dec-10 6:19am    
xmlparseexception???? Which function or codes you use to get or modify xml?
jerrykid 30-Dec-10 6:24am    
Ah, please vote for the answer if it helps you to solve problem. It's very important if you want someone else to answer your question again :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900