-3

I used to be able to do this:

public class Something
{
    public class SomethingElse
    {
        public static class ThisThing
        {
            public static string aoidj {get;set;}
        }
    }
}

But it no longer works.

My desired result (and what I've always been able to do) is:

Something.SomethingElse somethingElse = new Something.SomethingElse();
somethingElse.ThisThing.aoidj = "yay";

Console.WriteLine(somethingElse.ThisThing.aoidj);

But that no longer works. Instead of being able to access ThisThing from somethingElse, it's now appearing in SomethingElse.!

Has the C# language changed or something? The behaviour is definitely different and I don't know when it changed.

User 12345678
  • 7,714
  • 2
  • 28
  • 46
jay_t55
  • 11,362
  • 28
  • 103
  • 174
  • 13
    I am almost certain that you are mistaken. – User 12345678 Aug 05 '14 at 18:10
  • Why are you creating SomethingElse when you want access to a static class and property? – LarsTech Aug 05 '14 at 18:12
  • @ByteBlast _perhaps_ I have forgotten exactly _how_ I do it, but I have always used nested classes and the desired result has always been the same. I don't know what's happening. Maybe one whole side of my brain has died - I dunno! – jay_t55 Aug 05 '14 at 18:12
  • @LarsTech you need to understand it in context but context is not important here, just the desired result - which I can no longer seem to get! – jay_t55 Aug 05 '14 at 18:13
  • Do you have working code anywhere where you've been able to do this? – Dan Drews Aug 05 '14 at 18:15
  • 1
    Is it possible you may have confused the `static` keyword as it applies to members other than classes vs nested classes? – Mark Cidade Aug 05 '14 at 18:15
  • It's possible, @MarkCidade I'm not really sure what's happening. I think my brain is screwed. I actually don't want `static` - I just need the code mentioned in the `desired result` bit of my question to be the way it is written (e.g. `somethingElse.aoidj = "yay"`) – jay_t55 Aug 05 '14 at 18:20
  • It sounds like you need to change it so that you are accessing the `ThisThing` through a property or field. I updated my answer to accommodate your desired result. – Mark Cidade Aug 05 '14 at 18:28
  • "Always used nested classes" **Why?** – David Crowell Aug 05 '14 at 18:31

3 Answers3

3

You need to do this:

Something.SomethingElse.ThisThing.aoidj = "yay";

Console.WriteLine(Something.SomethingElse.ThisThing.aoidj);

Or otherwise change your code to this:

public class Something
{
        public class SomethingElse
        {
            public Whatever ThisThing = new Whatever();

            public class Whatever 
            {
                public string aoidj {get;set;}
            }
        }
}

And then you could do this (your desired result):

Something.SomethingElse somethingElse = new Something.SomethingElse();
somethingElse.ThisThing.aoidj = "yay";

Console.WriteLine(somethingElse.ThisThing.aoidj);
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • But I can't do that. It would be bad. I need to do it from somethingElse. Not SomethingElse. – jay_t55 Aug 05 '14 at 18:17
  • 1
    @Aeron: Than make it a property, not a class. – Will Marcouiller Aug 05 '14 at 18:19
  • A class is a static entity. It can't be used as a instance member. Your code would work even if the `ThisThing` class didn't have the `static` keyword, which is different than how that applies to its `aoidj` static member. A static class is the same as an abstract class with only static members. – Mark Cidade Aug 05 '14 at 18:20
  • @WillMarcouiller thanks for your reply, I need it to be a class though, because there is more stuff to go inside of `ThisThing` – jay_t55 Aug 05 '14 at 18:21
  • I added an alternative to my answer to accommodate your usage code. – Mark Cidade Aug 05 '14 at 18:25
  • @Aeron: Than make it a class per itself, in its own file, and write a property typed as ThisThing instead. – Will Marcouiller Aug 05 '14 at 18:30
3

It has to appear in the SomethingElse., otherwise, how can you acces it, it is a Nested Type!

C# has not changed in this way. Nested Types have always been accessible through their parent types.

See my answer here: Cannot access nested classes or members of base class.

Besides, there is no point in having static classes as a Nested Type, since static classes are more commonly used as managers or providers, so they are mainly used elsewhere in your system.

Aside, if you want to access your static class members, you have to type in its name and access it once and for all.

Something.SomethingElse.ThisThing.aoidj

But I can't do that. It would be bad. I need to do it from somethingElse. Not SomethingElse.

Than make it a property rather than a class.

public class Something {
    public class SomethingElse {
        public OrEventSomethingElse ThisThing { get; set; }
    }
}

public class OrEventSomethingElse {
    public string aoidj { get; set; }
}

This way, you shall not be able to access it through your Nested Type SomethingElse, but rather through only an instance.

Some resources to help you understand OOP.

** I need it to be a class though, because there is more stuff to go inside of ThisThing**

Make it a class outside of SomethingElse so that you may access it as a simple instance member/property.

public class ThisThing {
    public string Stuff { get; set; }
    public int SomeMoreStuff { get; set; }
    public DateTime EvenMoreStuff { get; set; }
    // ...
    public string ThisClassIsGettingHuge { 
        get {
            return "Time to refactor because big classes tend to break SRP";
        }
    }
}


public class Something {
    public class SomethingElse {
        public ThisThing ThisThingAsAProperty { get; set; }
    }
} 

It is then, and only then that you shall only be able to access your instance.

var somethingElse = new Something.SomethingElse;
Console.WriteLine(somethingElse.ThisThingAsAProperty.ThisClassIsGettingHuge);

I have developed information and process for years, and I rarely use Nested Types. They generally cause more damage than they help.

Community
  • 1
  • 1
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
0

Nothing is changed, but your code is wrong. And luckily I found answer too.

class Something
    {
        public class SomethingElse
        {
            public SomethingElse()
            {
            }
            public static class sm
            {
                public static void set()
                {
                }
            }
        }
    }

Use the class in this manner-

Something.SomethingElse.sm.set();
Ananth
  • 108
  • 6