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();

Thursday, December 02, 2004

Joining on objectSID or using objectSID in a WHERE Clause

This one drove me crazy for the better part of a day.
When SQL Server shows you an objectSID it looks something like this:

0x010500000000000515000000317B347F75771D5BB254B44007080000

If you take that string and type something like:

SELECT * FROM viewADEmployees WHERE objectSID = 0x010500000000000515000000317B347F75771D5BB254B44007080000

It works just fine.

But if you take that string and stuff it in a VARCHAR (or any other type you think might be appropriate) you get an empty result set.

So I thought, duh, I need to cast it to the same type as objectSID. It only took a little while to figure out that objectSID is of type VARBINARY. It turns out, however, that simply casting the string to VARBINARY does NOT work! In fact I could not find any CAST or CONVERT that did the trick. I performed more casts than the entire fishing population on opening day of trout season.

Google was of no help either. I was on my own on this one.

I finally stepped back reasoned it out:

1. objectSID is VARBINARY
2. I need to convert my comparison value to VARBINARY
3. I should create a user function that takes a string representation of a series of hex byte values
4. each two-character pair of hex digits reprsents a single binary byte
5. I need to build up a VARBINARY value by successively converting the pairs to integers and then adding them to my VARBINARY return value

This is my resulting function which took a suprisingly short period of time to conceive, write and debug :

CREATE FUNCTION dbo.fncFormatSID (@vchFromSID VARCHAR(300))
RETURNS VARBINARY(85)
AS
BEGIN
DECLARE @binOut VARBINARY(85);
DECLARE @intLen INT;
DECLARE @intIndex INT;
DECLARE @intLow INT;
DECLARE @intHigh INT;
DECLARE @vchLow VARCHAR(1);
DECLARE @vchHigh VARCHAR(1);

SELECT @intLen = LEN(@vchFromSID)
SELECT @intIndex = 2

WHILE (@intIndex < @intLen)
BEGIN

SELECT @vchHigh = SUBSTRING(@vchFromSID, @intIndex, 1)
SELECT @vchLow = SUBSTRING(@vchFromSID, @intIndex + 1, 1)

IF(@vchLow >= '0' AND @vchLow <= '9')
BEGIN
SELECT @intLow = ASCII(@vchLow) - ASCII("0");
END

IF(@vchHigh >= '0' AND @vchHigh <= '9')
BEGIN
SELECT @intHigh = ASCII(@vchHigh) - ASCII("0");
END

IF(@vchLow >= 'A' AND @vchLow <= 'F')
BEGIN
SELECT @intLow = ASCII(@vchLow) - ASCII("A") + 10;
END

IF(@vchHigh >= 'A' AND @vchHigh <= 'F')
BEGIN
SELECT @intHigh = ASCII(@vchHigh) - ASCII("A") + 10;
END

IF @binOut IS NULL
BEGIN
SELECT @binOut = CONVERT(VARBINARY(1), (@intHigh * 16) + @intLow) ;
END
ELSE BEGIN
SELECT @binOut = @binOut + CONVERT(VARBINARY(1), (@intHigh * 16) + @intLow) ;
END

SELECT @intIndex = @intIndex + 3

END /* While */


RETURN(@binOut);

END

Accessing Active Directory From SQL Server

Figured out how to query active directory from SQL Server. Its pretty well documented but the first step is to create what is know as a 'Linked Server'. This involves executing one line of code in Query Analyzer:

sp_addlinkedserver 'ADSI', 'Active Directory Service Interfaces', 'ADSDSOObject', 'adsdatasource'

Then you can execute stuff like this:

SELECT * from openquery(ADSI, 'SELECT cn,objectsid FROM ''LDAP://atlas/CN=Users,DC=doublestarinc,DC=com''')

I'm not sure if its possible to perform a join directly with this. To save time I created a view using something similar to the above SELELCT statement. Then using that view we can all join to our heart's content.

Friday, November 05, 2004

Passed 70-316 Today !

Scored a 920 !

Didn't think I did better than the 70-315 test but somehow I did.

Now on to 70-320.

Tuesday, September 28, 2004

Whacky, Nutty Keyboard Antics

My keyboard layout got whacked out for Office products only.
a would print q, comma would print : stuff like that.

Solution:

All Programs -> Microsoft Office -> Microsoft Office Tools -> Microsoft Office 2003 Language Tools.

All Programs -> Microsoft Office Tools -> Microsoft Office XP Language Tools.

In both locations found the French to be the Enabled and Active Language.

Deleted it from the the Active Lists.

Viola!

Wednesday, September 15, 2004

Tools I Used Today

Made my first professional forray into VBA / Access Front end development.

Yuck!

Did a good job though.

Monday, August 02, 2004

Tuesday, July 20, 2004

Tools I've used recently

Wrote my first Fortran Program!
My nephew is taking a course in it and was having trouble.
I found an online Fortran 90 reference and quickly had the program working.
 
I even pulled a Unix command line tool out of my rear-end to kill a couple of processes (ps).
 
When done, he called me a 'Programming God'.
 
Boy, am I now all puffed up.
 
 
Successfully debugged a VB / Windows Scripting Host app.  
 
cscript.exe

How to Debug Classic ASP in VS.NET

Make the following configuration mods:
 
 
In VS.NET:    
 
     project - properties        
            configuration properties            
                    debugging
                         - enable ASP debugging
                         - enable ASP.NET debugging
 
 
 
In the IIS snap-in:    
 
      default website - properties        
             home directory tab            
                  configuration                
                        app debugging                    
                                - enable ASP server-side script debugging    
 
 
Add IIS process account to Debugger Users group:    
 
       In Computer Management snap-in:        
                       System Tools            
                               Local Users and Groups                
                                      Groups                    
                                           Debugger Users - properties                        
                                                  Add user IWAM_machine-name (the "Launch IIS process  account")

C# Sucks!

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