.NET Framework - meaning of -eq $null
Asked By LeoTohil
15-Nov-07 10:33 PM
a comparison of an empty list to $null doesn't return anything. Not true,
not false, just nothing. Example:
$t1 = new-object System.Collections.ArrayList
$t1 -eq $null
(returns nothing)
$t1 -ne $null)
(returns nothing)
I'm familiar with three-valued logic in database systems but I suspect that
something else is going on here. Could someone explain?
Database
(1)
Sincerley
(1)
Prosser
(1)
Tohill
(1)
Leo
(1)
Singleobject
(1)
Propertygrid
(1)
Arraylist
(1)
Karl Prosser[MVP] replied...

Ok this is one of the serious gotchas in powershell.. which is serious
because you'll often make this mistake, and its not intuitive coming
from other languages but its actually a powerful feature when used right..
basically with the comparison operation.. its based on the type of the
left operand. If its a single object then the comparison is going to
return true, or false.. but if the left operand is a collection or
array, its going to do pattern matching, and then just spit out the
matches. that is why when i am doing explicit compares i always do what
looks unnatural, and put the single item on the left i.e
if (2 -eq $a)
instead of what you'd naturally put
if ($a -eq 2 )
now lets go a little deeper with this.
@() -eq $null
$null -eq @()
$null -eq @($null)
anyway Null is a comfusing example.. but lets look at your one..
if $t1 is undefined then
$t1 -eq $null
and
$null -eq $t1
both show true and an undefined variable will resolve to a single true..
but hte minute you turn it into a collection
$t1 = new-object System.Collections.ArrayList
$t1 -eq $null
you get NOTHING... because you are not taking each item in the arraylist
(which there are none) and doing pattern matching and just returning the
ones that match the comparision..
however for your test
$null -eq $t1
returns false, because t1 does indeed exit.
if lets add some items to $t1
$t1.Add(2)
$t1.Add(5)
$t1.Add($null)
$t1.Add(2)
and now
$t1 -eq 2
returns 2 lots of 2.... because its matching the items in the array
which
2 -eq $t1 still returns false, since an arraylist is not an integer let
alone a 2
interestingly enough
$t1 -eq $null still returns nothing.. this here i don't
understand/remember at the moment.. maybe its stripping it out somewhere
but you do see the pattern matching behaviour if the left is an array or
collection and why if you want to be sure you are doing a true single
object comparison, put the singleobject on the left..
this pattern matching if the left is a collection works for all/most of
the comparison operators
1..10 -gt 5
etc
well have to look into the issues of that one $null in the collection
not coming through, but i hope you get the general picture now..
Sincerley,
Karl
One of the Architects of Powershell Analyzer and PowerShell Plus
http://www.powershell.com
LeoTohil replied...
As usual, an excellent response. Thanks Karl.
I'd phrase the important point as "comparison operators are not left-right
symmetrical". Once you accept that, things become clearer.
Let me know when you figure out that remaining question on "$t1 -eq $null" .
There must be another rule in effect.
I don't suppose the language specification is public, is it?
- Leo
LeoTohil replied...
Some more thoughts on this...
$t1 = new-object System.Collections.ArrayList
$t1 -eq $null
returns nothing, because a comparison is never performed! The comparison is
not performed, because the left side is an empty collection.
LeoTohil replied...
BTW, I am just restating what you implied. I just wanted to point out that
the
just a bit.
Karl Prosser[MVP] replied...
i like the way you worded the observations..
what i find interesting though is with $null things are still different i.e
1,4,"hello",$null,1,$null -eq 1
returns 1 ones, from the collection/array
but
1,4,"hello",$null,1,$null -eq $null
doesn't return anything even though the array contains nulls.. to me
this is strange. a side effect i discovered when answering your questions.
Keith Hill [MVP] replied...
Nice explanation!
It does return $null but $null doesn't render to any text on the console
e.g.
15> $t1 -eq $null
16> $t1 -eq $null | %{if($_ -eq $null){"We have a null"}}
We have a null
--
Keith
Karl Prosser[MVP] replied...
thanks keith you are right
(1,4,"hello",$null,1,$null -eq $null).length
shows
two
i should have just gone to the results explorer view in powershell
analyzer because its as clear as day there, but i was looking at the
propertygrid, which i exclude nulls from, for good reason.

articles I have read about TDD, a good unit test does not rely on the database or other such external / environmental conditions. More generally, a good unit test is atomic and developing unit tests for a DAL - whose purpose in life is to interact with a database? Your thoughts and opinions are appreciated. C# Discussions ADO.NET (1) LLBLGen (1) ASP.NET (1) IIS (1) Application (1) Database (1) Adapter (1) Report (1) I would test the behavior in the DAL. I m Model View * Patterns* view parts 1-5 The unit test needs to create a new database in a known state, open it using the DAL under test, verify the results, clean up the database. Ben, Then it wont be a unit test in the TDD sense. In fact i still develop the test before the code. You could even use the test to drive database layout (this might not be very effective at normalization though). Absolutely, I agree. In fact thats how i would even design my Database tables. Similar to what Ben said, We used to have a "test database (or one of each, mssql, mysql, oracle) that each time would be cleaned out and
NET database inspection API? .NET Framework Is there a straightfoward API in .NET that allows for inspection of a database? That is, to look at the structure of the database, without knowing anything in advance about it? For example, retrieving a list of tables in the database. Doing a little browsing in MSDN, I see that the abstract representation of a database appears to be the DataSet class. I also see that I can use database to a DataSet. Because for example DataSet.Tables returns a collection of tables in the database, I thought this might be useful. However, it looks to me as though the connection can only be made with some advance knowledge of the database. It appears that to get a data adapter hooked up to a database requires the
for table .NET Framework Hi, How do I check if a table exists in my database? - - Thanks Morris ASP.NET Web Controls Discussions SQL Server (1) CALLMasterMDB (1) OleDbConnection.GetSchema (1 TABLES Check the result you can see all the base tables and views of the database. Please let me know if it works. If you have further questions please feel free column to be visible if a table (mailboxactivitylog) exists in either the sql or access database bound to the gridview via datasource. How do I check if this table exists? I correct you have two questions: 1. How to know if a tables exists in the database. 2. How to hide a column of a GridView manually. To the first question, we can query the database to see all the tables in it. Then check if the table exists in the database. Generally database has some system tables that can help to do this. For SQL Server, it's So we can use the following query: SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES For Access database we use MSysObjects. Here's the query for Access: Select Name from MSysObjects We can
am trying to write a program that will save a connection string for any ODBC database. I cannot find the equivalent of 'cnn.Properties("Prompt") = 2' in ADO.Net. I am thirteen years ago by OleDb: http: / / msdn.microsoft.com / en-us / library / ms810892.aspx http: / / database.ittoolbox.com / documents / odbc-vs-oledb-18150 The very first version of the .NET Framework Providers about 10 years ago and these providers are available in .NET. Depending on the database you are using, you will need a specific provider with a specific connection string to them up each time. In this particular case, my program will conect to an Oracle database, but I do not have Oracle here. I am too lazy to make a trip my program (to convert an XML file into records in a table) against an Oracle database. There does not seem to be a .NET provider for MS Access, and even if there were, I would have to write different code depending on the database that I was connecting to: dim cnn as JetConnection 'or something like that and change is expected to work with, so I do not have to support an arbitrary unknown database, I still want to be able to use the program with a database other than Access or Oracle. If the OleDB provider has its own login dialog box
Any Option to Save Changes to Database? .NET Framework Okay, I finally got databases working with my ASP.NET projects. Now, I m back to WinForms so here we go again. I understand that when creating a database file in my project, the default settings is to silently wipe out any existing data I'm creating a test project in VS 2005 that uses a SQL Server Express database. I then created a Windows form and bound my edit controls to a table in the database. I'm able to add any number of records and browse through them. But, again want to keep any data they created. Does anyone know the trick to making my database changes permanent? Thanks. - - Jonathan Wood SoftCircuits Programming http: / / www.softcircuits.com ADO.NET Discussions SQL Copy to Ouput" option. Kerry Moorman Kerry is right. When you incorporate the SQL Server database (file) into your project, it too is subject to the infamous "Copy to Output" setting s Guide to Visual Studio and SQL Server (7th Edition) _ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ _ If I select the database under Data Connections in the Server Explorer, there's only a couple of properties and re all greyed out anyway. As I think I mentioned, I'm not attaching a database file. It's a SQL Server Express database. So where would I got to find