C# adding items to lost ...

Hello,

here is my code:

This is my class :
  class info
    {

        private List<info> list;

        public static info list(List<info> list)
        {

            list = new List<MonsterInfo>();

            list.Add(new info() { ID = _ID });
            
            
            List<info> SortedList = list.OrderBy(o => o.ID).ToList();

            var firstItem = SortedList.ElementAt(0);

            return firstItem;
        }

Open in new window


This is my class from where i am calling info.

  public static void HandleInfo()
        {
		
			
			List<info> list = new List<info>();
		
                switch (name)
                {
                
                    case "info":
                        into.info(list);
                        break;
                
            }

Open in new window


Again what I am trying to is to have a list of items in it. What is currently happening is that my
list is re-written every time HandleInfo is called.

I have posted this question before (yesterday) but I market it solved to quickly because I still don't quite understand
what expert in this question was trying to tell me: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28662980.html#a40745382

Anyway I followed his instructions and added this line
 List<Info> list; 

Open in new window

into info class. Now i am stuck
because I don't understand where this line of code should be
  list = new List<Info>(); 

Open in new window


Thank you all for reading and sorry for almost double posting!
TagyourareitAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Can you please post the complete class for info and the class where HandleInfo is located in. Also please explain what you want each to be doing.
TagyourareitAuthor Commented:
Hello,

sure I can.

First I call method of a class info:

 public class Handle
    {
     	
         public static void HandleInfo()
         {
            List<Info> list = new List<Info>();

                switch (name)
                {
         
                    case "INFOS":
                        InfoList(list);
                        break;
                }
           }    
    }

Open in new window


Now as you can see i want to add some items to the list of a class Info.

This is my class info with method InfoList:

		
  public static List<Info> InfoList(List<Info> list)
        {

            list.Add(new Info() { ID = _ID });
            
            List<Info> SortedList = list.OrderBy(o => o.ID).ToList();

            return SortedList;
        }

Open in new window


And this is cass where i am storing my list:

public class Info
		{
		   
			public long ID { get; set; }            
			
		}
		

Open in new window


Now as you have already guessed my problem is i am overwritting my ist each time HandeInfo is called.

What i want to do is "to ADD new list element" instead of what i am doing now "overwriting" it.

I hope i am more clear now.

PS this are samples of code i am recreating ... i can not post whole project here. Hope you understand.
AndyAinscowFreelance programmer / ConsultantCommented:
I guess this is meant

class info
    {

        private List<info> list = new List<info>();

        public static info list(List<info> list)
        {

            //This line is not needed     list = new List<MonsterInfo>();
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

TagyourareitAuthor Commented:

I guess this is meant

class info
    {

        private List<info> list = new List<info>();

        public static info list(List<info> list)
        {

            //This line is not needed     list = new List<MonsterInfo>();

Hello,

but how will i call method
public static info list(List<info> list)

Open in new window

without passing parameter list from outside of a class?
Fernando SotoRetiredCommented:
Hi Tagyourareit;

Try moving List<Info> to class level as I show below.

public class Handle
{
    static List<Info> list = new List<Info>();

     public static void HandleInfo()
     {
         switch (name)
         {     
             case "INFOS":
                 InfoList(list);
                 break;
         }
     }    
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TagyourareitAuthor Commented:
exelent answer as allways.
Fernando SotoRetiredCommented:
Not a problem Tagyourareit, glad to help.
AndyAinscowFreelance programmer / ConsultantCommented:
Make certain you understand what the changes made actually do.

Your problem is a lack of understanding of variable lifetime and scope - one of the most basic of things you MUST understand in writing code.  If you don't understand this concept you will constantly write code which fails - or worse, appalling code that works more by chance than design.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.