Wednesday, August 24, 2005

Friendly casting using 'as'

Running this code will NOT cause a runtime error if 'b' is not of type 'myclass'. 'a' will simply be assigned null :

myclass a;

a = b as myclass;

if(a == null)
Console.WriteLine("Oh well. b is not a myclass.");
else
Console.WriteLine("Hey, guess what! b is a myclass! Wow!");


This code will throw an invalid cast exception:


myclass a;
someotherclass b;

a = (myclass) b;


One might say "What's so cool about using 'as'? I can do this with roughly the same amount of code. Like this...":

myclass a;
someotherclass b;

try{
a = (myclass) b;
Console.WriteLine("Hey, guess what! b is a myclass! Wow!");
}
catch{
Console.WriteLine("Oh well. b is not a myclass.");
}


That's neat, but guess what? That's using the .Net framework's exception handling mechanism for flow control. In all the examples above the implication is that there is not just the possibility that 'b' is not a myclass but rather there is some likelihood that 'b' is not a myclass. Using the exception handling for flow control is very bad form and a bad habit to get into. Its very slow and can make the debugger stop for no good reason. Don't do it!

Friday, August 19, 2005

Using C#/.net Reflection to call Private and Protected methods

Today I learned that it is possible to call private and protected methods by using reflection.
This is a great technique for Unit Testing such critters for proper behavior when 'code that shuold never be reached' gets reached.

Example:


internal string GetAString(int stringId)
{
foreach(DataRow dr in _theDataSet.Tables["mySTringTable"].Rows)
{
if((int)dr["strID"] == stringId)
return((string)dr["theStringValue"]);
}

//Impossible or very difficult to create a test situation
// where this code would ever get executed. The methods
// that DO have access to this method ALWAYS pass a valid stringId.
throw new NotFoundException("String not found!");
}


Check out this link:

http://www.codeproject.com/csharp/TestNonPublicMembers.asp

Or google:

"reflection unit test private method"

Tuesday, August 09, 2005

Saving View State to a Place OTHER THAN a Hidden Field on the Page

If you want to keep the view state server-side or in some other place on the rendered page.
You might, for instance, determine that the wire cost exceeds the server-side memory cost and therefore want to keep View State on the server.

Override these two methods :


Saves any view-state information for the page.
Override this method if you want to save the Page view state in anything other than a hidden field. If you choose to do so, you must also override the Page.LoadPageStateFromPersistenceMedium method.

protected virtual void SavePageStateToPersistenceMedium(object viewState);



Loads any saved view-state information to the Page object. Override this method if you want to load the Page view-state information in anything other than a hidden field.

If you want to specify something other than hidden fields to save view state when using this method, you must also override the SaveStateToPersistenceMedium method.

protected virtual object LoadPageStateFromPersistenceMedium();

C# Sucks!

JK!! Seriously, though, somewhere around C#-3 we should have inculcated ourselves with the question: "Does 'CAN' == 'SHOULD...