Search code examples
c#arraysclassnested-properties

Unable to access nested property of array of static class


Givens:

  1. The class must be accessible from inside another class that must be static (no choice).
  2. The FOO class must be recursable supporting a nested array of a collection of items given the structure in the example.
  3. FOO must be serializable in full including all nested arrays and subsequent items inclusive to XML (without using fancy tricks -- just straight serialize to xml which seems to support only one class and will auto-find nested classes / structures accordingly)

The problem is I can not do the following as the properties of FOOTabItem are not accessible.

// can not be static
public class TestClass : SomeFancyInheritedObject { 

    // must be static
    public static void Test() {
        FOO.FOOTabItem bar = new FOOTabItem();

        bar.Count = 100;  // This is inaccessible
    }

}

This is the class code I am having trouble making the objects exposed from :

public static class FOO {
    public static int Money { get; set; }

    public static List<FOOTab> Tabs { get; set; }


    public static struct FOOTab {
        public static List<FOOTabItem> Items { get; set; }

        public static FOOTab TabInformation { get; set; }
    }

    public static struct FOOTabItem {
        public static ItemInfo Item { get; set; }
        public static int Count { get; set; }

    }

    public static FOO() {
        Tabs = new List<FOOTab>();
        Money = 0;
    }

}

I have also tried using nested classes instead of struct in FOO, however this resulted in FOO complaining that I can not use a class as an object parameter which is why I am using struct instead.

There are no syntax errors with this code. I can expose the properties/etc of FOO itself and instantiate new() of the structs, however I can not see the properties of the structs.

I believe this is likely a permissions issue (public / static / internal / etc), but I am unsure what exactly to do to correct this. I have searched SO and there are issues similar in nature however they do not offer any clear insight regarding nested Lists of objects inside static classes and how to properly access them from within other static classes.


Solution

  • I've removed all the static keywords in the code below. Here, bar.Count is accessible.

    public class TestClass : SomeFancyInheritedObject 
    { 
        public static void Test() {
            FOO.FOOTabItem bar = new FOO.FOOTabItem();
            bar.Count = 100; // This is now accessible
        }
    }
    public class FOO {
        public int Money { get; set; }
        public List<FOOTab> Tabs { get; set; }
    
        public FOO() {
            Tabs = new List<FOOTab>();
            Money = 0;
        }
    
        public struct FOOTab {
            public List<FOOTabItem> Items { get; set; }
            public FOOTab()
            {
                Items = new List<FOOTabItem>();
            }
        }
    
        public struct FOOTabItem {
            public ItemInfo Item { get; set; }
            public int Count { get; set; }
        }
    }
    

    UPDATE: In response to your comment, here is code that I've tested in VS:

    class Program
    {
        public static void Main(string[] args)
        {
            var foo = new FOO();
            var x = new XmlSerializer(typeof(FOO));
            using (var memoryStream = new MemoryStream())
            {
                using (var writer = new StreamWriter(memoryStream))
                {
                    x.Serialize(writer, foo);
                }
            }
        }
    }
    
    public class FOO
    {
        public int Money { get; set; }
        public List<FOOTab> Tabs { get; set; }
    
        public FOO()
        {
            Tabs = new List<FOOTab>();
            Money = 0;
        }
    
        public class FOOTab
        {
            public List<FOOTabItem> Items { get; set; }
            public FOOTab()
            {
                Items = new List<FOOTabItem>();
            }
        }
    
        public class FOOTabItem
        {
            public ItemInfo Item { get; set; }
            public int Count { get; set; }
        }
    }
    
    public class ItemInfo
    {
    }