none
public static Object List count is showing zero when accessed from another class

    Question

  • I am populating an public static object from an SQL query via setters, adding the object to an public static object List<>.  Table has 1 row, so one object, List count is 1 when adding object to list.  All good.  I close the reader, close the connection then call a method in another class file ClassName.MethodName() and use the list count in a for loop as shown.  The list count is 1, but its showing 0 when the cursor is on ClassName or Count in the for loop.  What could be causing this?  

    for(int x = 0; ClassnName.ListName.Count; x++)


    • Moved by 宝宝徐 Wednesday, April 12, 2017 6:00 AM
    Tuesday, April 11, 2017 7:01 PM

Answers

  • Hi RichardDunneBSc,

    Thank you for posting here.

    For your question, I make a simple example that create a list in a class. And then get it in another class. You could make a method to return the list. And invoke the method in another class to access the list.

      class Program
        {
            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";
                return ListName;
            }
        }
        public class Card
        {
            public static string cardName;
            public string CardName
            {
                get { return cardName; }
                set { cardName = value; }
            }
        }

    I hope this would be helpful.

    If I misunderstanding what you want, please feel free to let me know.

    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 12, 2017 6:18 AM
    Moderator

All replies

  • RichardDunneBSc

    ClassName objClass = new CLassName();
    var list = objClass.MethodName().toList();
    
    for(int x=0;list.count; x++)
    {
    
    }

    Regards

    if util mark answer

    Tuesday, April 11, 2017 10:05 PM
  • Hi RichardDunneBSc,

    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 12, 2017 5:59 AM
  • Hi RichardDunneBSc,

    Thank you for posting here.

    For your question, I make a simple example that create a list in a class. And then get it in another class. You could make a method to return the list. And invoke the method in another class to access the list.

      class Program
        {
            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";
                return ListName;
            }
        }
        public class Card
        {
            public static string cardName;
            public string CardName
            {
                get { return cardName; }
                set { cardName = value; }
            }
        }

    I hope this would be helpful.

    If I misunderstanding what you want, please feel free to let me know.

    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 12, 2017 6:18 AM
    Moderator