Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Showing posts with label C# statements. Show all posts
Showing posts with label C# statements. Show all posts

Monday, December 15, 2008

Using static strings and objects to increase performance and maintainence

I found this global variable article below to help me with using static strings and objects to increase performance and maintainence.

For example, suppose I have an error message, "Error: ", that I might want to change to "There was a problem: ". Instead of search and replace n times, I could just use a static string variable to change the string value.

Sample Global class:

/// <summary>
/// Summary description for Global
/// </summary>
public static class Global
{
public static string CSS_HIDDEN
{
get { return "hidden"; }
}
public static string CSS_EMPTY
{
get { return string.Empty; }
}
public static string CSS_CONTENT
{
get { return "content1"; }
}
public static string ERROR_HEADER_HTML
{
get { return "<red>There was a problem:</red></br>"; }
}
public static string ERROR_HEADER
{
get { return "There was a problem:"; }
}
}
Then one just accesses it with Global.ERROR_HEADER. Notice that there is no declarative instantiation for the static class.

Reference: http://dotnetperls.com/Content/Global-Variables-ASPNET.aspx

Friday, September 14, 2007

C# switch statement for DropDownList to update Image url.

Simple control-flow statement to reference C# switch statement, which is similar to other language's case statement.


switch (DropDownList1.SelectedValue)
{
case "Image 1":
imgFoo.ImageUrl = "images/photo1.jpg";
break;
case "Image 2":
imgFoo.ImageUrl = "images/photo2.jpg";
break;
case "Image 3":
imgFoo.ImageUrl = "images/photo3.jpg";
break;
default:
imgFoo.ImageUrl = "images/photo_blank.jpg";
break;
}