Forums

static class problem

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Re: static class problem

Postby todd.a » Sun Nov 08, 2009 10:06 pm

hopscc: I think I found a reason for having "is shared" on the class other than "More for completeness/symmetry than anything else." It has to do with the constructor in the static class. Having the shared umbrella makes each method underneath it static and provides a public constructor, which is all correct. A truly static class [in C#] prevents any misuse of the type by hiding the default, no-arg constructor (plus other restrictions).

Chuck: I know you weren't convinced that it is needed but it could be a good way to enforce that a type contains only static members, as using "shared" in the umbrella context allows having instance members within the type.

Code: Select all
class StringUtil // just for e.g. purposes. Like Chuck pointed out "extends Type" makes most utility classes obsolete
  shared
    def foo(str as String)
      pass

# generates
public class StringUtil : Object {
  public StringUtil() ...
  public static void Foo...
}

# but

class StringUtil is shared # define a truly static class
  def foo(str as String) ..

# generates
public class StringUtil : Object {
  private StringUtil() ...
  public static void Foo(String str) ...
}
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Previous

Return to Discussion

Who is online

Users browsing this forum: No registered users and 52 guests