1

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(){  } 
}
4
  • You need to make EmpList available outside of the class you have defined it in - either using a property or a method, depending on your case. Only then will you be able to access it within the Employee class. Commented Sep 3, 2019 at 10:54
  • @AnoopRDesai I made a property public List<Employee> EmpList{ get; private set; } but in location i'm getting null. Commented Sep 3, 2019 at 11:01
  • Yes, you need to have called CreateEmpList() before you access it in the Employee class. Commented Sep 3, 2019 at 11:03
  • Happy to help! :) Commented Sep 3, 2019 at 11:07

4 Answers 4

2

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";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yup I was working on my colleague's code. I Was confuse in a concept, now I've got it clear. Thank you so much for help :)
0

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";
        }
    }
}

Comments

0

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;
            }
        }
    }

Comments

0

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 = "..."; } }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.