Maryam
Maryam

Reputation: 525

How can I iterate through loop with list variable of another class?

I'm working with C#. I have a Employee class and I'm getting employee data from a URL then I created a list named EmpList in another class which is being populated with that information. I'm not getting the location of each employee so I want to hard code the location by making a set location function in Employee class. The name 'EmpList' does not exist in the current context.

I've tried to make setLocation function in CreateEmpList function and I got no error but location was empty. I know I'm probably doing something silly but I really need some help here. I really appreciate that. Thankyou.

This is my employee class.

public class Employee
{

    public string Name { get; set; }
    public string Email { get; set; }
    public Guid ID { get; set; } 
    public string Location { get; set; }
    public void SetLocation()
    {

        foreach (var item in EmpList) // I'm getting error here
        {
            if (item.Email == "[email protected]")
            {
                item.Location = "US";
            }
    }

And here I'm populating the list in another class.

    private List<Employee> EmpList = null;
        private void CreateEmpList(SPHttpClient client)
        {
            List<Employee> SortedList = new List<Employee>();
            JObject jsondata = client.ExecuteJson(UriResources); 
            string strjsondata = jsondata.First.First.First.First.ToString();
            JArray jsonArray = JArray.Parse(strjsondata);

            foreach (var item in jsonArray) //  Creating master resources list
            {
                ResourcesExcemptList.ForEach(i => i.ToLower()); 

             if(!ResourcesExcemptList.Contains(item["ResourceEmailAddress"].     
             ToString().ToLower()))
             {
                    if (Boolean.Parse(item["ResourceIsActive"].ToString()))
                    {
                        Employee emp = new Employee();
                        emp.ID = (Guid)item["ResourceId"];
                        emp.Email = item["ResourceEmailAddress"].ToString();
                        emp.Name = item["ResourceName"].ToString();
                        emp.Practice = item["ResourceGroup"].ToString();
                        emp.ApproverID = 
                       (Guid)item["ResourceTimesheetManageId"];

                        SortedList.Add(emp);
                    }
                }
            }
            EmpList= SortedList.OrderBy(o => o.Name).ToList();
            //private void setLocation(){  } 
}

Upvotes: 1

Views: 1885

Answers (4)

Flater
Flater

Reputation: 13823

The direct answer to your question

The main issue here is that you're not understanding how object oriented code works. You're not using this, and you seem to be confused when the class method will be executed and what that means.

Oddly, when in a class method, you still expect that you need to look through the list to find the correct object. That's the opposite of how you should approach it.

When an object's class method is being executed, you obviously already have found the object whose method you want to call. Because otherwise you wouldn't have been able to call that object's class method.

So what you need to do here is to iterate over the list before you call the object's class method, not after. Your Employee class:

public void SetLocation()
{
    this.Location = "US";
}

And then:

private void CreateEmpList(SPHttpClient client)
{
    // the rest of the code

    EmpList = SortedList.OrderBy(o => o.Name).ToList();

    foreach(var employee in EmpList)
    {
        employee.SetLocation();
    }
}

Footnote

Your question shows a basic confusion on OOP principles, but the code itself shows a different level of grasp on OOP principles. I suspect that you didn't write this code yourself, but a colleague did.

I'm mentioning this because I noticed the comment in your example code:

//private void setLocation(){  } 

Notice how its signature is that of a method definition, not that of a method call!

What I think has happened is that your colleague annotated the code and placed a reminder for you to create a method, and you've ended up implementing this method in the Employee class instead of in the other class (the one with the CreateEmpList method in it).

Creating the method in the other class makes a lot more sense than putting it in the Employee class. Something along the lines of:

public void SetLocation(Employee employee)
{
    employee.Location = "US";
}

Upvotes: 2

AlanK
AlanK

Reputation: 1974

To answer your question: public void SetLocation(List<Employee> EmpList) allows the code inside SetLocation() to access the list object (passed by reference) but I doubt this is what you really want to do. (No offense;-)

Your logic isn't clear but surely within CreateEmpList(),

emp.Email = ... 
if (emp.Email...) emp.Location = "..."

or within Employee, something like

public string Email { get {} set { Email = value; if (value...) Location = "..."; } }

Upvotes: 0

Rousonur Jaman
Rousonur Jaman

Reputation: 1271

If your main concern is set location value and if empty then set hardcode value then consider this solution:

    private string _location;
    public string Location
    {
        get { return _location; }
        set
        {
            if (string.IsNullOrEmpty(value))
            {
                _location = "US";
            }
            else
            {
                _location = value;
            }
        }
    }

Upvotes: 0

Anoop R Desai
Anoop R Desai

Reputation: 720

One possible solution, based on my comments:

Declare EmpList as: public List<Employee> EmpList { get; private set;}

Then in your Employee class:

public void SetLocation()
{
    var otherClassObj = new otherClassObj(); // Or perhaps some other way of getting the object of the other class.

    otherClassObj.CreateEmpList(client); // You may have to change this.

    foreach (var item in otherClassObj.EmpList)
    {
        if (item.Email == "[email protected]")
        {
            item.Location = "US";
        }
    }
}

Upvotes: 0

Related Questions