Welcome to AspAdvice Sign in | Join | Help

.NET Predicates

YourGenericListOfInts.RemoveAll(new System.Predicate<int>( delegate(int val) { return (val == r.Index); }));

A predicate is a function that returns true or false and can be used for example to filter your results.. For example your .NET RemoveAll(...) function can actually do things like ...

Read more at SharpDeveloper
 

Posted by salibhai | 1 Comments
Filed under: , ,

.NET Advice now SharpDeveloper

I decided to open up shop at my own domain - SharpDeveloper.NET

Check it out!

Posted by salibhai | 0 Comments
Filed under: ,

Creating SqlParameters Best Practices

*NEW* See the updated post at Sharp Developer: Sql Parameters Best Practices 

This article summarizes some nice ways to create SqlParameter arrays.  When we use SqlHelper, or even without SqlHelper when we use SqlCommands directly, and we want to pass an array of SqlParameters to the function.  The method I will discuss does not require you to hard-code the number of elements, nor does it require you to create a temporary list of some sort, but it requires you to have all the values up front that you want to insert into the list.

 

Posted by salibhai | 5 Comments

Using ?? and casting To Set Default Values

Often we have some value that may or may not be null, and we want to give it a default value if its null.

An awesome way to do this is to use ?? (only works in C#) and add your default value, and cast the entire thing, for example as follows:

string strCustomerName = (string)(SqlHelper.ExecuteScalar(m_ConnectionString, CommandType.Text, strQuery, sqlParams) ?? string.Empty);

Read more about nullables and the ?? operator

Posted by salibhai | 0 Comments
Filed under: , , ,

Don't eat exceptions

This is very bad!


try

{

    //do something that will throw an exception, like try to commit data to a database with malformed sql

}

 catch

{

    //sweep the dirt under the rug ;)

}

 

Consequences/Results - Your boss will not see any exceptions or code crashes, but it might come back to bite you in the butt when the code doesn't do what it was supposed to!

Additional Sources

Posted by salibhai | 0 Comments
Filed under: ,

Use .TryParse instead of Try {} Catch {}

In certain conditions, we should not be using exceptions.  For example, Try Catch blocks can be avoided in certain cases.  If you have some .NET 1.0 code, you might not be taking advantage of .NET 2.0 new TryParse functions, which avoid exception handling and are much faster.

 

For example:

try
{
    iEditMode =
Convert.ToInt32(strEditMode);
}

catch
{
    iEditMode = 0;
}

 
In this case, we have TryParse functions which were added in .NET 2.0
Better to instead do:

 
if (!Int32.TryParse(strEditMode, out iEditMode))
{
    iEditMode = 0;
}

 
Much better performance, no need to handle clunky exceptions :) Thank you to Brendan for correcting my error above. (missing out keyword)

MSDN in regards to exception handling states, "Do not use exception handling for flow of control, only for failure situations."

Resources 

Posted by salibhai | 1 Comments
Filed under: ,

Use String.Format instead of Chopping Strings

Here is a quick example of why string.Format is just so cool

Here is a quick example of why string.Format is just so cool

For example: 

  • text1.Text = string.Format("<a href=\"Mylink.aspx?abc=0&Color={0}\">my Link text</a>", myColor);

Isn't that much nicer than

  • text1.Text = "<a href=\"Mylink.aspx?abc=0&Color=" + myColor + "\">my Link text</a>", myColor);

Now with two parameters it's still so clean!

  •  text1.Text = string.Format("<a href=\"Mylink.aspx?abc=0&ID={0}&wl={1}\">my Link text</a>", myColor, myWeight);
Posted by salibhai | 0 Comments
Filed under: ,

3 Benefits of Using ConnectionStrings Instead of AppSettings

With .NET 1.0, we are using .AppSettings In .NET 1.0 we used ConfigurationSettings instead of ConfigurationManager

Read the article on SharpDeveloper.net
Posted by salibhai | 2 Comments
Filed under: ,