user5462886
user5462886

Reputation:

Getting confused: I cannot pass parameters to a method of a PSObject

I created an PSObject, it has several properties and several methods. Only problem is to pass parameters to the methods.

after creating the object, its properties and its methods I can see the members with the get-members method. The Equals method has a parameter, my methods do not.

function get_ExcelObj {
         param ([string] $SheetName, [string] $AppDir, [string] $NameApp )

         [string] $SheetName  = "DB"
         [string] $BaseDir    = ""
         [object] $ExcelHash  = @{}  
         [object] $Index      = @{}

         $EE_DB = New-Object -TypeName PSObject

         Add-Member -InputObject $EE_DB -MemberType NoteProperty -Name ExcelDB        -Value $ExcelHash
         Add-Member -InputObject $EE_DB -MemberType NoteProperty -Name IndexDB        -Value $Index
         Add-Member -InputObject $EE_DB -MemberType NoteProperty -Name SheetName      -Value $SheetName
         Add-Member -InputObject $EE_DB -MemberType ScriptMethod -Name InitExcel      -Value $InitExcel
         Add-Member -InputObject $EE_DB -MemberType ScriptMethod -Name GetSheetName   -Value $GetSheetName
         Add-Member -InputObject $EE_DB -MemberType ScriptMethod -Name OpenExcelFile  -Value $OpenExcelFile
         Add-Member -InputObject $EE_DB -MemberType ScriptMethod -Name OpenExcelsheet -Value $OpenExcelsheet

         return  $EE_DB

}
$OpenExcelFile = { 
     param ( [string] $ExcelFile )
     $WorkBook = $this.ObjExcel.Workbooks.Open($ExcelFile)
     Add-Member -InputObject $this -MemberType NoteProperty -Name WorkBook  -Value $WorkBook 
}
$ExcelObj = get_ExcelObj -SheetName "DB" -AppDir $MedGovDir -NameApp "MedGov"
$ExcelObj | Get-Member # Lots of members, eg: Method bool Equals(System.Object obj)  
$ExcelObj.OpenExcelFile() -ExcelFile $file # does not work

Any ideas ? Thx a lot for any help. I am really confused.

Upvotes: 0

Views: 142

Answers (1)

kpogue
kpogue

Reputation: 700

When calling a method on a .NET object, you pass parameters the way you would in C# or VB.NET, comma separated in the order that the parameters appear in the method signature, within the parentheses. Try something like the following:

$ExcelObj = get_ExcelObj -SheetName "DB" -AppDir $MedGovDir -NameApp "MedGov"
$ExcelObj | Get-Member # Lots of members, eg: Method bool Equals(System.Object obj)
$ExcelObj.OpenExcelFile($file) # this should work
$ExcelObj.OpenExcelFile() -ExcelFile $file # does not work

Upvotes: 1

Related Questions