WMF5.0 and classes, but no namespaces?

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Paal Braathen Paal Braathen 1 year, 7 months ago.

  • Author
    Posts
  • #33241
    Profile photo of Paal Braathen
    Paal Braathen
    Participant

    Hi

    I want to create a class in a namespace so I can do something like: [MyNamespace.MyClass]::new()

    The problem is that there is no "namespace" keyword.

    This (old) blog post says that I can declare classes like: class MyNamespace.MyClass {}

    But this doesn't seem to be the case with WMF5.0

    Is it impossible to create classes in namespaces with 5.0?

  • #33247
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Well... maybe I'm misunderstanding the question, so apologies in advance.

    You're correct in that there's no "namespace" keyword. Your class name includes an implicit namespace, e.g., MyCompany.MyClass would have a namespace of "MyCompany," but it's really just part of the class name. Kinda like the "AD" prefix in Get-ADUser is a sort of ersatz namespace. But you don't declare a namespace per se.

    The class support in v5 is still a little nascent, so it's not going to do everything you're accustomed to from other languages. The team's plan seems to be to improve it over time.

  • #33293
    Profile photo of Paal Braathen
    Paal Braathen
    Participant

    Hi @donj

    I'm sorry. I might have done some wrong assumptions. I haven't written that much C# (which I find very similar to PowerShell in many ways) and I'm not sure about the full meaning of the "namespace" keyword in C#.

    Let me try to explain more what I'm trying to do.

    What I would like to be able to do in PowerShell is something similar to the following C# code:

    using System;
    
    namespace MyNamespace1 {
    	class MyClass {
    		public static void MyMethod() {
    			Console.WriteLine("This is class: {0}", typeof(MyClass));
    		}
    	}
    }
    
    namespace MyNamespace2 {
    	class MyClass {
    		public static void MyMethod() {
    			Console.WriteLine("This is class: {0}", typeof(MyClass));
    		}
    	}
    }
    
    class MyProgram {
    	static int Main() {
    		MyNamespace1.MyClass.MyMethod();
    		MyNamespace2.MyClass.MyMethod();
    		
    		return 0;
    	}
    }

    From your blog post and reply I understand that you should be able to write something like this in PowerShell:

    class MyNamespace1.MyClass {
        MyMethod() {
            Write-Host "This is a test"
        }
    }

    Sadly this does not work and will provide the error: "Missing 'class' body in 'class' declaration.".

    I looks like periods aren't allowed in the class name in WMF5.0. Maybe this has changed since the preview you used when writing your blog post?

    So if the "namespace" keyword doesn't exist and periods aren't allowed in the class name, how are you supposed to declare classes in a namespace?

  • #33294
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yeah, you're just trying to do a bit more with it than PowerShell's ready for, yet ;). Classes were introduced _mainly_ to support class-based DSC Resources, which are a very specific use case. While they can be used for more than that, they're not a fully fleshed-out feature, as yet. And yes, it's changed a lot in the various preview releases.

    is probably a good, and more recent example. PowerShell understands "namespace" in the context of the "using" keyword, as shown, but that's where we are right now. https://channel9.msdn.com/Shows/MsftPowerShell/PowerShell-v5-Classes–Introduction-to-PowerShell-Classes is also a good, and more recent (although pre-Using) example of how to correctly build a non-DSC Resource class.

  • #33308
    Profile photo of Paal Braathen
    Paal Braathen
    Participant

    Ok. While the "using namespace" part might be nice in some cases we are concluding that we can't put our own classes in our own namespace (yet)?

You must be logged in to reply to this topic.