0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

変数固定化

Posted at
Constants.ps1

function Set-Constant {
    param (
        [string]$Name,
        [Object]$Value
    )
    
    if (-not (Get-Variable -Name $Name -Scope Global -ErrorAction SilentlyContinue)) {
        Set-Variable -Name $Name -Value $Value -Option Constant -Scope Global
    }
}

# 定数を定義
Set-Constant -Name "PI" -Value 3.14159
Set-Constant -Name "EULER" -Value 2.71828
Set-Constant -Name "GRAVITY" -Value 9.80665

# 配列を定義
Set-Constant -Name "PRIMES" -Value @(2, 3, 5, 7, 11, 13)

# 列挙型を定義
Add-Type -TypeDefinition @"
public enum Colors {
    Red,
    Green,
    Blue,
    Yellow
}
"@
Set-Constant -Name "Colors" -Value [Colors]

MainScript.ps1

# Constants.ps1をインポート
. .\Constants.ps1

# 定数を使用
Write-Host "PIの値は: $PI"
Write-Host "オイラー数の値は: $EULER"
Write-Host "重力加速度の値は: $GRAVITY"

# 配列を使用
Write-Host "素数の配列: $PRIMES"

# 列挙型を使用
Write-Host "色の列挙型: $([Colors]::Red), $([Colors]::Green), $([Colors]::Blue), $([Colors]::Yellow)"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?