Last post Dec 05, 2006 09:31 PM by Russ Helfand
Dec 05, 2006 08:49 AM|speednet|LINK
Here is a tip for people using the VB.NET version of the CSS Control Adapters (as opposed to the C# version).
VB.NET 2.0 introduced a new operator, "IsNot", which is obviously the opposite of "Is". It is especially useful when testing for when something is not "Nothing". (I love the double-negative!)
So the full new syntax when testing for Nothing is "value IsNot Nothing".
The VB version of the control adapters do not utilize this new syntax. Instead, they use "Not IsNothing(value)".
There are two reasons I like the new syntax, rather than the old. First, it more readable, so when you're scanning a page of code you have quicker recognition, and it is the perfect opposite to "value Is Nothing". Second, it is much closer to
the C# syntax, for those of us who go back and forth between the languages. I have no idea iif one iis faster than the other.
It is very easy to change the entire CSS Control Adapter library of VB code to use the new syntax:
If you want to verify that it's working correctly, you can always click the Replace button a couple of times (instead of Replace All) so you can see what it's doing before replacing everything.
I hope this is helpful to all the VB coders out there.
Dec 05, 2006 09:12 AM|speednet|LINK
I wish there was a way I could edit my post to make a correction!
The Regex expression in step 3 needs one more character added, so that it becomes this:
Dec 05, 2006 09:26 AM|speednet|LINK
After doing a build with the replaced text I got a couple of errors, but it turned out to be a bug in the adapter code (I think), that was revealed when the IsNot operator was used.
In the file TreeViewAdapter.vb, lines 383 and 384 are:
Dim bItemCheckBoxDisallowed As Boolean = ((Not IsNothing(item.ShowCheckBox)) AndAlso (item.ShowCheckBox.Value = False))
Dim bItemCheckBoxWanted As Boolean = ((Not IsNothing(item.ShowCheckBox)) AndAlso (item.ShowCheckBox.Value = True))
When the Regex was applied it became:
Dim bItemCheckBoxDisallowed As Boolean = ((item.ShowCheckBox IsNot Nothing) AndAlso (item.ShowCheckBox.Value = False))
Dim bItemCheckBoxWanted As Boolean = ((item.ShowCheckBox IsNot Nothing) AndAlso (item.ShowCheckBox.Value = True))
That code produced the error, "'IsNot' requires operands that have reference types, but this operand has the value type 'System.Nullable(Of Boolean)'."
The bug in the code (as I see it) is that the ShowCheckBox property of the TreeNode should never have to be tested for null. It will always be true or false, never null.
So the corrected code should be:
Dim bItemCheckBoxDisallowed As Boolean = (Not item.ShowCheckBox.Value)
Dim bItemCheckBoxWanted As Boolean = (item.ShowCheckBox.Value)
Dec 05, 2006 09:34 AM|speednet|LINK
Dec 05, 2006 09:43 AM|speednet|LINK
OK, so those two lines of code (383 and 384 in TreeViewAdapter.vb) were still wrong, and should be fixed. The original code was testing if the ShowCheckBox property was Nothing (null), rather than testing if the VALUE of the ShowCheckBox property is "Nothing".
(Because ShowCheckBox can be Nothing, True, or False.)
So here's what those two lines of code should look like:
Dim bItemCheckBoxDisallowed As Boolean = (item.ShowCheckBox.Value = False)
Dim bItemCheckBoxWanted As Boolean = (item.ShowCheckBox.Value = True)
We still don't need to test for "Nothing" as a value in this case, so it can be taken out.
Dec 05, 2006 02:47 PM|Russ Helfand|LINK
I am so glad that we finally are getting people who really know VB.NET to chip in with suggestions like these. I am NOT a VB.NET guy historically, though I've been trying to learn quickly.
SpeedNet, I know it's a little much to ask, but could post a reply here with the final recommendation for what needs to get fixed. I want to be sure I don't do it wrong since it took a few tries (above) for you to figure everything out. :) THANKS.
Dec 05, 2006 09:25 PM|speednet|LINK
On this particular issue, my last past was the final fix. I had never seen a boolean property act as a tri-state value by giving it a value of Nothing for the third value. In researching this issue, Microsoft was initially not going to do this. In the
beta versions the used an Enum value representing the three values, and then changed it to the current method for the RTM version. There is something new to learn every day.
With the value of Nothing being used as the third possible value, if you wanted to test for "Nothing", you would use
Dim bItemCheckBoxNull As Boolean = (item.ShowCheckBox.Value = Nothing)
Note the use of the equal sign to test for Nothing, rather than Is or IsNot, because we are testing a simple scalar value, not an object reference.
Dec 05, 2006 09:31 PM|Russ Helfand|LINK