C# non-static class inside static class JINT

551 Views Asked by At

Hi trying to make a class inside a static class to use in JINT but when it's referenced I get an error

C# code


namespace Hi {
    public static class Ok {
        public class Wowa {
            public Wowa(){}
        }
    }
}

But when I try to make a new one in JavaScript I get an error "the object cannot be used as a constructor" from JINT

var k = new Hi.Ok.Wowa()

Am I doing this right? How can I set up the C# to be able to use the above code in JavaScript from JINT?

BTW IF instead of "Ok" being a static class, rather a namespace, it works, but I want it as a class because I want to have static methods in it also

2

There are 2 best solutions below

1
On

you cant use none-static class in a static class (ReadThis) but if you remove (static) in your frist class

  namespace Hi {
    public class Ok {
        public class Wowa {
            public Wowa(){}
        }
    }
}

and it can be said that it does not make much difference because (Static) only makes subcategories of your class have to use (Static). But if you want your class to be impossible to build on variables, you can use abstract(ReadThis)

namespace Hi {
    public abstract class Ok {
        public class Wowa {
            public Wowa(){}
        }
    }
}

and

Main()
{
    Ok k = new Ok();//Error
}
0
On

Imagine you have this:

namespace Hi
{
  public static class Ok
  {
    public class Wowa
    {
         public Wowa() { }
         public static string MyStaticMethod() => "Hello from 'Static Method'";
         public string MyNormalMethod() => "Hello from 'Normal Method'";
    }
  }
}

It's possible to use non-static class Wowa by making an instance of it , and then you can call MyNormalMethod of that instance (you can only call not-static method within instance of that class).

 Hi.Ok.Wowa wowa = new Hi.Ok.Wowa();
 wowa.MyNormalMethod();     

And without making any instance of Wowa you can call static method within it, like this:

Hi.Ok.Wowa.MyStaticMethod();

Finally you can see working code here.