I have third party DLL(written in c#)
That has a class called DBConnection
and static method called Connect
When I call that method from my C# console application like below it will work fine.
DataPackage.Db.DBConnection.Connect()
Next I tried to do the same thing using powershell.
[DataPackage.Db.DBConnection]::Connect()
But that gives me error
Exception calling "Connect" with "0" argument(s): "Object reference not set to an instance of an object."
What is the reason for this and how to fix this??
Output. GeeksforGeeks. Static Method. Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.
PowerShell provides no way to create new types that contain static variables; however, objects of such types may be provided by the host environment. Memory for creating and deleting objects containing static variables is managed by the host environment and the garbage collection system.
A "method" is a set of instructions that specify an action you can perform on the object. For example, the FileInfo object includes the CopyTo method that copies the file that the FileInfo object represents. To get the methods of any object, use the Get-Member cmdlet.
Static methods are often used to create utility functions for an application.” In other words, static methods have no access to data stored in specific objects. Note that for static methods, the this keyword references the class. You can call a static method from another static method within the same class with this.
I hate to drop an answer without a reference beyond personal experience, but I have never found an explanation for this behavior, so sorry this is not more informative.
When calling a parameterless method leave off the parens. So for your example do:
[DataPackage.Db.DBConnection]::Connect
instead of ...Connect()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With