0

I have defined a class which has List<>. I have shortened my Code. It is too large. There are too many List<>& in Method1() there is lots of code. Here is my code :-

 public class Time : ITime
 {
        public List<Table1> Setts1 = new List<Table1>();
        public List<Tabl2> Setts2 = new List<Table2>();

        public void LoadSettings1(int companyId)
        {
            Setts1 = ctx.tblSett1.Where(a => a.CompanyId == companyId).Select(a => a).ToList();
        }



       public double Method1()
       {
          var data = Setts1.Where(m => m.SetType == "TYPE1").Select(m => m.Value1).FirstOrDefault();
          ......
          ......
       }

     }

I want to use Method1() in another class. My issue is Setts1 which is preloaded in the Time Class. So when it is used in within the Time class it has Records. But when i call it from another class obviously Setts1 will have no records. I tried to initialize it from another class like this :-

public class Class
{
   .....
   Time cls = new Time();
   cls.Setts1 = ....;
   cls.Method1();
}

But Setts1 shows no records when in Method1. How to initialize the List<> from another class?

5
  • Setts1 seems to be filled up inside method LoadSettings1 and you haven't call this method inside your Class (last snippet in question). is that causing an issue? Commented Sep 23, 2015 at 5:20
  • I cannot call LoadSettings1 from another class. So i want to initialize it from the calling class. Commented Sep 23, 2015 at 5:23
  • Why cannot call it? It is because you don't have the company id outside? Commented Sep 23, 2015 at 5:25
  • Because LoadSettings1 has lots of another stuff which is not needed in another class. And there is not just one List<>, it has many. Code is more than 3000 lines. Commented Sep 23, 2015 at 5:28
  • 1
    If that's the situatoin than you have to figure out by yourself how to initialize your list as we cannot guess what other things are in your behemoth class. Commented Sep 23, 2015 at 5:33

4 Answers 4

2

Exposing field members of a class, outside of the class is not a good practice. So I recommend using properties like this:

//Mark the field member as private
private List<Table1> _Setts1 = new List<Table1>();

//Use Property to access the field outside of the class
public List<Table1> Setts1
{
    get
    {
        if (_Setts1==null || _Setts1.Count()==0) //or any other logic you need
        {   
            //Initialize the field memeber
            _Setts1 = ctx.tblSett1.Where(a => a.CompanyId == companyId).Select(a => a).ToList();
        }

        return _Setts1
    }
}

This way you can forget about methods like LoadSettings1 and it doesn't matter whether you use the Setts property inside the class or outside, it will be initialized at the right time.

Sign up to request clarification or add additional context in comments.

2 Comments

He needs a constructor to initialize companyId too.
This is a pattern not an exact solution to his problem, as he said there are more than 3000 lines of code. so we can just say ctx.tblSett1 has to be initialized before (e.g. in constructor). But according to his LoadSettings1 it seems he's main concern is not that point and I recommend him for the rest of the problem.
1

You have to call 'LoadSettings1(int companyId)'. This is the method which brings the records and populates your 'List'.

public class Class
{
    .....
   Time cls = new Time();
   cls.LoadSettings1(1);
   cls.Setts1 = ....;
   cls.Method1();
}

Comments

0
public class Something
{
   private Time cls = new Time();

   public Something(int companyId)
   {
      cls.LoadSettings1(companyId);
   }

   public void CallMethod1()
   {
      cls.Method1();
   }
}

Something like this? Using constructor for your "other class" to LoadSettings.

Comments

0
cls.Setts1 = ....;

Actually I don't see how your code would not work, even if as Hossein said, it's bad practice. Look into how you're setting cls.Setts1 (the .... part). That's most probably the culprit

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.