Archive

Posts Tagged ‘C#’

Programming in C# – Using the ?? Operator on Strings

April 21st, 2010 haXX No comments

In a previous post, I showed a single line IF statement to make things a little neater for true/false operations.  It looks like this…

string2 = string.IsNullOrEmpty(string1) ? "string1 is null" : string1;

This line of code can be understood as “If string1 is null or empty, then set string2 equal to ‘string1 is null’ else set string2 equal to the value of string1″

Here’s another way of writing this line of code assuming you’re checking only for a null condition…

string2 = string1 ?? "string1 is null";

This line of code can be understood as “If string1 is null, then set string2 equal to ‘string1 is null’ else set string2 equal to the value of string1″.  The real difference here is that the ?? operator only returns true for null, not empty.

Categories: Programming Tags:

Programming in C# – Can a Boolean Variable be NULL?

March 30th, 2010 haXX No comments

Can a Boolean data type be NULL?  Well, it can if you say it can.

Ordinarily, Booleans are non-nullable data types. There are many nullable and non-nullable data types in C#, and this article isn’t about those.  It’s about the data type I find to be more useful if I can include NULL as a permissible value. For a Boolean data type, this gives me a three-state flag if I need such a thing.

Let’s talk about the syntax for extending the Boolean type to inherit from Nullable.  It’s implemented by simply appending a question mark (?) after the type declaration when the variable is declared.  Whether defining a variable or a Method argument, it’s done the same way.  Let’s look at how it works.

Read more…

Categories: Programming Tags:

Visual Studio 2010 – New C# v4.0 Default and Named Parameters

March 24th, 2010 haXX No comments

I started out in ‘Visual’ languages using Visual Basic.  One of the features I got used to in Visual Basic was the ability to specify a default value for a method parameter if omitted by the caller.  This capability has been missing in C#.  Instead, the philosophy of method overloading was necessary.

Beginning with Visual C# v4.0, we’ll be able to specify default values for method parameters.  This means that if a parameter is not specified by the caller, a default value is supplied. We’ll still have method overloading, but now we get optional parameters. It’ll look something like this…

Read more…

Categories: Programming Tags: ,

Programming in C# – ASP.NET Custom Single Signon Enforcement

March 16th, 2010 haXX No comments

I have to limit users to a single logon in my web application. This web application uses a custom logon process that validates the user’s credentials, and then stores the User ID and other things in a session object. If a user should log on with the same User ID as an already logged on user, the already logged on user should be terminated.  I also needed to be able to view the list of logged on users.  Your requirements may be a lot different, but this is what I need.

My design approach is based on keeping a list of currently logged on users, and checking this list on every server request. If Session2 logs on with the same User ID as Session1, the next time Session1 requests a page from the application, they will be identified as superseded. Once identified, they will be transferred to a special page indicating the reason for being forced off. My goal is to introduce as little overhead as possible because the code for this process is added to the code length of every request by a logged on user. While trivial in execution, the sheer number of calls will be tremendous and add to the overall server load.

Read more…

Categories: Programming Tags: ,

Programming in C# – The USING statement

March 13th, 2010 haXX 2 comments

The USING statement is a C# code feature that enables us as programmers to limit the scope of resource allocations.  We do this already (hopefully) by declaring a resource, using it, then disposing it when we’re done.  We also have to be sure to dispose of the resource should an error occur and control gets transferred somewhere else in our code.  The USING statement does the same thing, but with fewer lines of code.

Since I use database connections a lot in my web apps, I have to be sure that I allocate a connection for as little time as possible, and clean up behind myself when I’m finished with it.  I don’t want to leave any open connections in the connection pool.  The USING statement should be used anywhere we’re borrowing an expensive resource.  In this example, I’m using a database connection.  You should also consider this method for using Fonts, file handles, ports, etc.

Here’s an example of the USING statement…

using (OracleConnection conn = new OracleConnection(ConnectionString))
{
   conn.Open();
   ...
   ...
   ...
   Do stuff....
   return set.Tables[0].DefaultView;
}

The USING statement declares the OracleConnection and scopes it to the code block contained within the braces.  When the current line of execution exits the braces, the OracleConnection is implicitly Disposed. Even if we get an exception within the scope of the USING statement, we are guaranteed that dispose will run on the Connection.  This is akin to using a Try|Catch|Finally scenario with an explicit Dispose() in the finally block.

Since variables are scoped to the USING’s range, be sure to declare any variables you may want to reference later outside of the USING block.

Categories: Programming Tags:

Programming in C# – IF statements to confuse other programmers

March 13th, 2010 haXX 1 comment

I really don’t code the way I do to confuse other programmers looking at my stuff.  It’s just that I came from an assembly language background and to this day, I can’t help but to write my code in as few statements as possible. That’s why I write the single line version of the IF statement when I can. Not only is it simpler to write, it takes up less space and is just plain elegant.

Here’s how you could write an IF statement to set a variable to true or false depending on another variable.

If (CarMake == "Chevy")
{
    IsChevy = true;
}
Else
{
    IsChevy = false;
}

Now look at this version of the If statement and see what you think…

IsChevy = CarMake == "Chevy" ? true : false;

Cool, huh?

Categories: Programming Tags: