[enum]::Getnames($xlConstants) fails

Welcome Forums General PowerShell Q&A [enum]::Getnames($xlConstants) fails

Viewing 3 reply threads
  • Author
    Posts
    • #159375
      Participant
      Topics: 86
      Replies: 138
      Points: 677
      Rank: Major Contributor

      I have this one line of code:

      [enum]::GetNames($xlConstants)
      
      

      the purpose being to enumerate all symbolic names (in EXCEL).

      I am getting the error message:

       Exception calling GetNames with "1" arguments. "Value cannot be null.  Parameter name: enumType"

      Why?  Would be grateful for any tips and advice.

    • #159377
      Participant
      Topics: 12
      Replies: 527
      Points: 1,244
      Helping Hand
      Rank: Community Hero
      #Requires -Version 5
      
      enum xlConstants {
          This
          is
          an
          example
      }
      
      [enum]::GetNames([xlConstants])
      

      You’re trying to pass an empty variable to the GetNames method of the enum class. That method accepts an enum type as in the example above.
      Enum values can have no spaces, special characters, or numbers.

    • #159380
      Participant
      Topics: 2
      Replies: 1013
      Points: 2,093
      Helping Hand
      Rank: Community Hero

      This is not a PowerShell see, you just happen to be doing that here.
      This is a raw .Net namespace call and you are bound to what it expects and PowerShell can only abide by that.

      Enum.GetNames(Type) Method
      Namespace: System
      Assemblies: System.Runtime.dll, mscorlib.dll, netstandard.dll
      https://docs.microsoft.com/en-us/dotnet/api/system.enum.getnames?redirectedfrom=MSDN&view=netframework-4.8#System_Enum_GetNames_System_Type_

      # C#
              Console.WriteLine("The members of the Styles enum are:");
              foreach(string s in Enum.GetNames(typeof(Styles)))
                  Console.WriteLine(s);
      

      So, like this articles shows, you need to be passing an array.
      Working With Enums In PowerShell

      How are you populating …

      $xlConstants
      

      See also this:
      #PSTip Working with Excel constants

      # create Excel object
      $xl = New-Object -ComObject Excel.Application
       
      # create new PowerShell object
      $xlEnum = New-Object -TypeName PSObject
       
      # get all Excel exported types of type Enum
      $xl.GetType().Assembly.GetExportedTypes() | Where-Object {$_.IsEnum} | ForEach-Object {
       
          # create properties from enum values
          $enum = $_
          $enum.GetEnumNames() | ForEach-Object {
              $xlEnum | Add-Member -MemberType NoteProperty -Name $_ -Value $enum::($_)
          }
      }
      

      And this…
      Using enums in Powershell

    • #159411
      Senior Moderator
      Topics: 9
      Replies: 1263
      Points: 4,571
      Helping Hand
      Rank: Community Hero

      Better, if you could post the whole function/script you have to avoid guessing.

Viewing 3 reply threads
  • The topic ‘[enum]::Getnames($xlConstants) fails’ is closed to new replies.