none
List<> count different when accessed from another class.

    Question

  • When a public static List<Object> is created in a class with multiple objects in the list, is there any reason why the list count would be different when accessing the list from another class?  I'm creating an object list from a database.   I'm creating a new object for each read, so I have multiple objects in a list.  Yet when I access the list from another class, the list  count is only 1.  What could be causing this?  I've tried creating a new list, same count, 1.
    • Moved by 宝宝徐 Wednesday, April 19, 2017 5:59 AM
    Tuesday, April 18, 2017 8:52 PM

All replies

  • Hi,

    Welcome to MSDN forum.

    This forum is discussing Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor. I will move your thread to corresponding forum for a professional support.

    Best regards,

    Joyce


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 19, 2017 5:57 AM
  • Hi RichardDunneBSc,

    Thank you for posting here.

    For your question, you could try the following code for reference.

      static void Main(string[] args)
            {        
                ClassName className = new ClassName();
                List<Card> newList = className.Name();
                for (int i = 0; i < newList.Count; i++)
                {
    
                }
                Console.ReadKey();
    
            }
        }
        public class ClassName
        {
            List<Card> ListName = new List<Card>();
            public List<Card> Name()
            {
                //Card Card = new Card();
                //ListName.Add(Card);
                //Card.CardName = "A";
                //Card.CardName = "B"; if you add the CardName to the list with Card.CardName, you will get only one in the count of the list.
    
                ListName.Add(new Card() { CardName = "crank arm", CardId = 1234 });
                ListName.Add(new Card() { CardName = "chain ring", CardId = 1334 });       
                return ListName;
            }
    
        }
        public class Card
        {
            public static string cardName;
            private static int cardId;
            public string CardName
            {
                get { return cardName; }
                set { cardName = value; }
            }
    
            public int CardId
            {
                get { return cardId; }
                set { cardId = value; }
            }
        }

    If it could get what you want, could you provide some demo code for us to test your code.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 19, 2017 7:56 AM
    Moderator