In code you want to assign one variable to another, but only if the variable is not null. If it is null, you want to populate the target variable with another value. The code normally may look like this using the C# Conditional Operator:
string fileName = tempFileName != null ? tempFileName : "Untitled";
If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.
This can now be abbreviated as follows using the Null-Coalescing Operator:
string fileName = tempFileName ?? "Untitled";
The logic is the same. If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.
The Null-Coalescing Operator comes up a lot with nullable types, particular when converting from a nullable type to its value type:
int? count = null;
int amount = count ?? default(int);
Since count is null, amount will now be the default value of an integer type ( zero ).
// underscore.js mixin to find list class, not relevant to finding the index in jquery.tmpl, but useful otherwise.
_.mixin({
indexClass: function(index, length) {
var result;
result = index % 2 === 0 ? "even" : "odd";
if (index === 0) {
result += " first";
} else if (index === length - 1) {
result += " last";
}
return result;
}
});
// find an loop through all the elements in the document
for (i = 0; (a = document.getElementsByTagName("link")[i]); i++)
{
// disable all the stylesheet
if (a.getAttribute("rel").indexOf("style") != -1
&& a.getAttribute("title"))
a.disabled = true;
// if it is the stylesheet requested, then enable it
if (a.getAttribute("title") == title)
a.disabled = false;
}
}
// Jumble addresses
function decryptEmail() {
for(i=0; i<= document.links.length-1; i++) {
var str = new String(document.links[i].href);
document.links[i].href =
str.replace(/^mail:([^\?]+)(\?.+)?$/,"mailto:$
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
$2");
}
}
var __PNGReplace = "png.gif";
function changePngForIEWin()
{
imgs = document.getElementsByTagName("img");
for (var i = imgs.length; i-- > 0;)
{
if(imgs[i].src.indexOf(".png") > 0)
{
imgs[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/ + imgs[i].src + ",sizingMethod=scale)";
function PreloadImages()
{
//alert("number of images to load: " + PreloadImages.arguments.length);
// var arr = new Array(PreloadImages.arguments.length);
for(var c = PreloadImages.arguments.length; c -- > 0;)
{
_img = new Image();
_img.src = PreloadImages.arguments[c];
}
}
function setLinkedStyleSheet(title)
{
var linkNodes = document.getElementsByTagName("link");
for ( i = 0; i < linkNodes.length; i++ )
{
linkNode = linkNodes[i];
relAttr = linkNode.getAttribute('rel');
if ( relAttr && ( relAttr.indexOf("style") != -1 ) && linkNode.getAttribute("title") )
{
linkNode.disabled = true;
if ( linkNode.getAttribute("title") == title )
linkNode.disabled = false;
}
}
}
// function for changing stylesheets using document.styleSheets
function setStyleSheet(theme)
{
for ( i = 0; i < document.styleSheets.length; i++ )
{
if ( document.styleSheets[i].title )
{
document.styleSheets[i].disabled = true;
// Here is a quick and dirty class to create a bindable list of states and abbreviations for vb.net which can also be used in Silverlight
Public Class clsStates
Sub New(sName As String, sAbbrev As String)
_StateName = sName
_Abbreviation = sAbbrev
End Sub
Private _StateName As String
Public Property StateName() As String
Get
Return _StateName
End Get
Set(ByVal value As String)
_StateName = value
End Set
End Property
Private _Abbreviation As String
Public Property Abbreviation() As String
Get
Return _Abbreviation
End Get
Set(ByVal value As String)
_Abbreviation = value
End Set
End Property
End Class
Public Class Globals
Private _States As List(Of clsStates)
Public Property States() As List(Of clsStates)
Get
If _States Is Nothing Then
_States = New List(Of clsStates)
End If
If _States.Count = 0 Then
FillStates()
End If
Return _States
End Get
Set(ByVal value As List(Of clsStates))
_States = value
End Set
End Property