Powershell module class usage within a class - Stack Overflow

admin2025-03-31  5

I have a script like this, in a classes.psm1 file:

class A {
    A([string] s) { }
}
class B : A {
    B([string] s) : base($s)
}

Then I try to import it in a .ps file:

using module classes.psm1
class C : B {
    [A] $p

    C([string] s) : base($s) {
        $this.p = A::new($s)
    }
}

Above, B is known, but not A, throwing Unable to find type [A]. How is it possible to work with both classes in the latter script?

Note: Moving C in the same module as A and B makes it unknown in the latter script.

I have a script like this, in a classes.psm1 file:

class A {
    A([string] s) { }
}
class B : A {
    B([string] s) : base($s)
}

Then I try to import it in a .ps file:

using module classes.psm1
class C : B {
    [A] $p

    C([string] s) : base($s) {
        $this.p = A::new($s)
    }
}

Above, B is known, but not A, throwing Unable to find type [A]. How is it possible to work with both classes in the latter script?

Note: Moving C in the same module as A and B makes it unknown in the latter script.

Share Improve this question asked Jan 10 at 19:51 mikemike 4845 silver badges20 bronze badges 1
  • This should be on StackOverflow, it has not much to do with systems or software and is much more of a programming/scripting question. I can't vote to move because I misclicked and the stupid system doesn't know what misclicking and correcting that is, hence this comment. – Destroy666 Commented Jan 10 at 23:16
Add a comment  | 

1 Answer 1

Reset to default 0

Basically, @Destroy666 is right, although I've come from a similar issue also posted here.

Yet, it turns out the code is right, but the Powershell seems to have problems with processing it correctly - but I am a newbie with Powershell, so I can't be sure now. So, after trying and assessing, I've found that closing and re-opening Powershell clears some cache or interprets the code from scratch, so it works fine. Also, right now it behaves in a way that it needs a Powershell closing/re-opening each time the base class (called A above) code is changed, otherwise false positive errors occur. To me, although it looks to be related to programming, this knowledge may be useful to any system admin that writes some Powershell code like this.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743401587a213009.html

最新回复(0)