Clarification on c# Class of Classes

Hi folks,

I had previously been developing in js, then I started using Daikon Forge (love it btw) which uses c# and kinda created too much grief with references between js and cs scripts.

So, I’m converting all to c#, however I’m having trouble with a “class” of classes.

I had previously put a lot of classes in a single js script that were referenced in many scripts in many scenes. And there didn’t seem to be a problem.

Now that I’ve converted it to c# I can no longer just use the individual class name I seem to need to use the “class” of classes name with it.

For example:

In js

public class Scene {
  var name 		: String;
}

Then in js code I would reference it like so:

public var scene : Scene;

function x(){
  scene = new Scene;
  scene.name = "beginning";
}

However in c# it is like this:

public class NrG_Classes {

	public class Scene {
		public  string 		name;
	}
}

And now I have to reference it like this:

    public NrG_Classes.Scene scene;
 
    public void x(){
      scene = new NrG_Classes.Scene;
      scene.name = "beginning";
    }

My question is this:

Is it really necessary to include the “base” class name for the “subclasses” (not sure if I’m using that term correctly)?

Or is there a different way in c# to just use “Scene” instead of “NrG_Classes.Scene”?

Thank you for helping me out on this question.

Base classes and subclasses is called inheritance and are used to have the subclass inherit the properties of the base class.

Example:

public class BaseClass
{
}

public class SubClass : BaseClass
{
}

And you would reference or create it normally like this:

public void Test()
{
    SubClass c = new SubClass();
}

You have nested classes, which is classes declared within a class.
There is no other way to reference a class within a class other than going through the top class itself like you have done, which sort of is the whole idea of nested classes.

You can of course reference the nested class directly if you are in the top class or are inheriting from the top class.

public class TopClass
{
    public void Test()
    {
        NestedClass c = new NestedClass();
    }

    public class NestedClass
    {
    
    }
}

//Or like this
public class Other : TopClass
{
    public void Test()
    {
       NestedClass c = new NestedClass();
    }
}

Depending on your situation a solution to your issue could be to put the nested classes outside of the top class and remove the top class altogether. The previously nested classes will then be global classes and can be used anywhere.

If you still need to group them in any way you could use a namespace and then import that namespace where you want to use the classes.

namespace CustomGroup
{
    public class Class1
    {
    }
    
    public class Class2
    {
    }
}

Then in any other class script reference and use the classes like this:

using CustomGroup;

public class TestClass
{
    public void Test()
    {
        Class1 c = new Class1();
    }
}

Or without importing the namespace you can use them by referencing the namespace itself:

public class TestClass
{
    public void Test()
    {
        CustomGroup.Class1 c = new CustomGroup.Class1();
    }
}

Although this brings us back to the original issue again.

Sorry for the long answer. =P