Hi,

I've created a class called MyClass which just contains some data:

public class MyClass
        {
            #region Private Variables
            private string _ValueA;
            private string _ValueB;
            #endregion

            public MyClass(string ValueA, string ValueB)
            {
                _ValueA= ValueA;
                _ValueB= ValueB;
            }

            public string ValueA
            {
                get { return _ValueA; }
                set { _ValueA= value; }
            }

            public string ValueB
            {
                get { return _ValueB; }
                set { _ValueB= value; }
            }
        }

In my code i've got some data and made an ArrayList of MyClass containing the data. That all seems to work fine.

In the next bit of code I want to go through the ArrayList, but I'm having trouble getting anything from it.

ArrayList TestArrayList = GetData();
             for (int i = 0; i < TestArrayList .Count; i++)  <---- this count is 14
            {
                MyClass anElement = (MyClass)TestArrayList [i];  <--- is null here all the time
             }

If I do the following and debug it, I get a Count of 14, but each returned element is null. Yet, when I break and watch the TestArrayList I can see each element is there, it's just when I MyClass anElement = (MyClass)TestArrayList ; it seems to return null.

I'm only doing it like this because when I tried to use List<MyClass> in GetData() I get an exception saying the object is not of MyClass type and I can not add it.

So, Questions:

1) Why can't I see what is in the ArrayList?
2) Less importantly, is there any commonly known reason why I couldn't add this class to a List<MyClass>?

Please ask any questions which will help as i'm not really sure what else I can say (otherwise I would have solved this myself ;) )

Many thanks in advance,

Phil

Edited 7 Years Ago by 12d3: n/a

Could you show us the code of GetData?

Hi, Yes... GetData() uses Entity Framework to get some data and just puts results in the ArrayList. Transaction is an entity and TransactionBL is the business layer. I'm definately getting things back in audits.

public static ArrayList GetData()
    {
        ArrayList results = new ArrayList();

        TransactionBL transactionBL = new TransactionBL();
        List<Transaction> audits = transactionBL.GetTransactionRange(DateTime.Now);

        foreach (Transaction ta in audits)
        {
            MyClass transLog = new MyClass(ta.CustomerName, ta.BranchName);
            results.Add(transLog);
        }

        return results;
    }

Something else to note is that i'm using GetData() as a data source for a gridview, which is showing the contents without any problem.

I tried this code and it works:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList TestArrayList = GetData();             
             for (int i = 0; i < TestArrayList .Count; i++)  //<---- this count is 14
            {
                MyClass anElement = (MyClass)TestArrayList [i];  //<--- is null here all the time
                Console.WriteLine(anElement.ValueA);
             }
             Console.ReadKey();
        }

        public static ArrayList GetData()
        {
            ArrayList results = new ArrayList();
            string N = "Name";
            string B = "Branch";

            for (int i = 0; i < 14; i++)
			{
                string S = i.ToString();
                MyClass transLog = new MyClass(N + S, B + S);
                results.Add(transLog);
            }
            return results;
        }

        public class MyClass        
        {
            #region Private Variables
            private string _ValueA;
            private string _ValueB;
            #endregion

            public MyClass(string ValueA, string ValueB)
            {
                _ValueA= ValueA;
                _ValueB= ValueB;
            }

            public string ValueA
            {
                get { return _ValueA; }
                set { _ValueA= value; }
            }

            public string ValueB
            {
                get { return _ValueB; }
                set { _ValueB= value; }
            }
        }

    }
}

So I suppose the null values must come from the TransactionBL stuff. As a remark: use List instead of ArrayList if you can.

I tried this code and it works:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList TestArrayList = GetData();             
             for (int i = 0; i < TestArrayList .Count; i++)  //<---- this count is 14
            {
                MyClass anElement = (MyClass)TestArrayList [i];  //<--- is null here all the time
                Console.WriteLine(anElement.ValueA);
             }
             Console.ReadKey();
        }

        public static ArrayList GetData()
        {
            ArrayList results = new ArrayList();
            string N = "Name";
            string B = "Branch";

            for (int i = 0; i < 14; i++)
			{
                string S = i.ToString();
                MyClass transLog = new MyClass(N + S, B + S);
                results.Add(transLog);
            }
            return results;
        }

        public class MyClass        
        {
            #region Private Variables
            private string _ValueA;
            private string _ValueB;
            #endregion

            public MyClass(string ValueA, string ValueB)
            {
                _ValueA= ValueA;
                _ValueB= ValueB;
            }

            public string ValueA
            {
                get { return _ValueA; }
                set { _ValueA= value; }
            }

            public string ValueB
            {
                get { return _ValueB; }
                set { _ValueB= value; }
            }
        }

    }
}

So I suppose the null values must come from the TransactionBL stuff. As a remark: use List instead of ArrayList if you can.

I've already checked what is coming from transactionBL and there is something there. There is also something returned from GetData in the ArrayList (when I watch it I can see all 14 items in it). This is why i'm very confused. The Count in the for...loop also shows that there is something there (14 items).

When using a List<MyClass>, I try to Add an item and get an "Attempted to access an element as a type incompatible with the array." exception. - Which is why i'm using an ArrayList now.
I've looked over my code a few times (which is as simple as yours), yet I can't see anything obvious. I'm now seeing that using an ArrayList isn't going to help and that there is a problem higher than that. ...I need to recheck everything.

Thanks for the replies ddanbe. I think i'm going to need some time to go over everything again or some sleep and then go over everything :).

I will report back when I find what the problem is.

Ok, think I know what's going on. First of all I need to point out that the code above has some renamed classes etc, as it's not my code to post (as in i'm working for a company so can't post exact code), which probably is never a good idea on a forum since you only see part of the problem.

Anyway, the problem is that there were two MyClass's in different namespaces but for some reason it was getting confused even though I explicitly named it in my code (MyNameSpace.MyClass). So, I renamed it and it works fine now - I've gone back to using a List too.

Thanks for the replies though.

This question has already been answered. Start a new discussion instead.