I'm new to VBScript and rather perplexed as to why the following code works:
set Adapters = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
for each Nic in Adapters
if Nic.IPEnabled then
MsgBox "IP Address: " & Nic.IPAddress(asdf), 4160, "IP Address"
end if
next
When the variable asdf
is undefined, it works. If I define asdf
to an invalid index (e.g. -1 or 4), I get an invalid index error.
I thought: Perhaps undefined variables default to 0 in VBS? Nope, I tried to print it and nothing is written.
Where is the functionality of passing an undefined variable as an array index returning the first item in the array documented? What are some other, similar oddities I may run into while programming in VBScript?
Edit: Some things I've discovered:
- Undefined variables are equal to
Empty
by default in VBScript Empty = 0
is trueNic.IPAddress(Empty)
also returns the first element of the arrayMsgBox 0
will print0
, whileMsgBox Empty
will print nothing
I'm still having trouble finding any documentation stating that array indexing handles Empty
quietly by returning the first element, explaining why it is equivalent to but printed differently from 0, and what other constructs handle Empty
parameters (and what they do as a result).