Tuesday, April 27, 2004
my first NUnit ecperience, very pleasurable indeed!
Programming | .Net | C# | NUnit
4/27/2004 4:52:34 PM (GMT Standard Time, UTC+00:00)  #    Comments [5]  |  Trackback
Thursday, April 22, 2004
a rant about how my recent misfortune, materialistic though it is, is the result of a lack of morality in regular people
4/22/2004 1:35:50 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Wednesday, April 21, 2004
And figured out how to restore it!
Programming | .Net | C#
4/21/2004 9:40:19 PM (GMT Standard Time, UTC+00:00)  #    Comments [1]  |  Trackback
The things you never consider...
4/21/2004 8:56:34 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Wednesday, April 14, 2004

I wanted to tell my story in detail but I'm finding myself too busy to take the time it deserves.  Instead, I'll say a few words to capture the moment and try to come back to it later.

In a car, the windows are small and the passengers are low and close to the ground.  The view is framed like a television and the world goes by while the occupants feel as though they are basically standing still.  Except for the slight jostling and chages in direction, its hard to tell one is moving at all.  Even in a SUV, the seats are positioned so that the view is tunnelled through the windows.

In a vanagon, you are seated very high and the windows are huge!  The windows go from well above your head to close to your waist.  The steering wheel is horizonally angled rather than nearly straight up and down.  This provides a motorcycle like view of your surroundings.  Add the bumping and shaking and the shear girth of the vehicle and it is very clear that your are the ones moving.  The experience is heightened by the clunkiness of the ride.  The characteristic putt putt of a vanagon engine and whirring of the gears is the final touch to make it a very, very different ride entirely. 

When I say clunkiness, I do not mean junkiness!  The vanagon is a volkswagen in its truest form.  It embodies the definition of a machine.  Elegance is present in pure utility and convenience.  The seats are bolt upright and totally supportive.  A four hour trip is effortless and comfortable and the seats are as comfortable as the othepedist designed Volvo buckets and have the additional joy of classic arm rests.  At first glance, you might notice a lack of modern styling but once you're on the road, that awareness fades away.  You become consumed by the incredible view and the feeling that you are truly going somewhere.  The lack of power can be frustrating if you let it, but accept that as part of its character, and you find yourself grow a fondness for its struggle to climb the hills.  Every incline and stop sign reminds you of the little engine that could.  The drivers behind swerve left and right, certainly pissed off at the damned-hippies who are in their way and making them later than they already are, but you are right on time.  Because your destination was simply the driver's seat and the open road and as long as you are on it, in your westy, you are home.

4/14/2004 3:05:40 PM (GMT Standard Time, UTC+00:00)  #    Comments [4]  |  Trackback

That blasted enter key! Don't press the enter key...

When developing in ASP.Net, much of what we used to do in HTML and classic asp is now wrapped up in the server controls.  One of the things that our qa team finds often is incorrect form submission when pressing the enter key. 

Handling fear of mice

There are several appropriate ways to handle the enter key. 

One thing you can do is define a "default action".  For example, if there are several navigation buttons on your page plus a data entry form, it would make sense that any enter key pressed should fire the form submit action. 

Another thing you could do is cancel the enter key entirely.  If you intend to support more han standard browsers, this is not the best approach.  Some users do not actually use a mouse at all.  Keep this in mind if you use the following example. 

When I have time, I'll show how to implement the first approach using this same script as a starting point. 

For now, we're just going to capture the enter key globally for the form.   In ASP.Net, the form is the usually the entire page.  (Hint: once you've captured the enter key press, you have control of the action. Use control.ClientId to cause a clientside event that will execute the post-back.)

A simple example

<script language=javascript>

            <!--

            var bIsEnterKey = false;

            function checkKeyPress(){

                  return !bIsEnterKey;

            }

            function setKeyPress(){

                  bIsEnterKey = (event.keyCode.toString() == '13');

                  window.setTimeout("bIsEnterKey=false;",1000);

            }

           

            //-->

</script>

 

 

<form id="Form1" method="post" runat="server" onsubmit="return checkKeyPress();" onkeypress="setKeyPress()">

What happens?

When the page renders to the client, anything that ASP.net normally adds to the onsubmit event will precede the custome function call.  Therefore, the last thing to happen is going to be the enter key check.  If you have validators on the page, they will be executed first.  If the validation fails, the enter key check will not be called.  In most cases, this is desired, but you need to be aware of it.  Validators can mess with your head!  The good new is that once validation is successful, the enter key function will be called.  If the enter key was the source of the form submission, what ever you've defined in the function (in this case: cancel the form submit)  is going to happen. 

In the rendered page notice the form onsubmit action.  This show the result of placing a RequiredFieldValidator on the page.  Nothing special needs to happen beyond the code shown above.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

  <HEAD>

                  

                   <script language=javascript>

                   <!--

                   var bIsEnterKey = false;

                   function checkKeyPress(){

                             return !bIsEnterKey;

                   }

                   function setKeyPress(){

                             bIsEnterKey = (event.keyCode.toString() == '13');

                             window.setTimeout("bIsEnterKey=false;",1000);

                   }

                  

                   //-->

                   </script>

</HEAD>

          <body>

                   <form name="Form1" method="post" action="AdSenseEdit.aspx" language="javascript" onsubmit="ValidatorOnSubmit();return checkKeyPress();" id="Form1" onkeypress="setKeyPress()">

<input type="hidden" name="__VIEWSTATE" value="..." />

                

<script language="javascript" src="/aspnet_client/system_web/1_0_3705_288/WebUIValidation.js"></script>

 

 

                             <div id="pnlPropFields" align="Center" style="width:600px;">

 

Programming | .Net | C#
4/14/2004 2:32:43 PM (GMT Standard Time, UTC+00:00)  #    Comments [3]  |  Trackback
Monday, April 12, 2004
oh, ink. now I get it.
Programming | .Net | C#
4/12/2004 5:48:36 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Wednesday, April 07, 2004
A rant about the state of things
4/7/2004 2:46:16 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
the hard way
Programming | .Net | C#
4/7/2004 2:27:08 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Tuesday, April 06, 2004

Serialization is one of my favorite features of .Net and I use it more every day.  Much like Xml, once you've gotten the hang of it, you find new uses every time you turn around. 

Today I wanted to serialize to string.  Simple enough?  The examples I found online often created byteArrays and char arrays and went through more work than necesary.  So, for those who are searching like I was, here is the simplest example I could write.

The simplest overload provided by the Serialize method of the System.Xml.Serialization.XmlSerializer class takes two parameters: TextWriter and an instance of the class to serialize. 

System.IO.StringWriter inherits from TextWriter, therefore, it can be used in this method call.

using System;

using System.Xml.Serialization;

using System.IO;

 

namespace HPCi.ResourceGuide.Google

{

      [Serializable]

      ///

      /// A very simple entity class

      ///

      public class Class1

      {

            ///

            /// A couple of public fields. 

            /// In the real world, I always create

            /// private fields with public accessors

            /// additionally, I usually initialize fields to

            /// default values or empty values rather than null

            ///

            public string MyPublicStringField = String.Empty;

            public bool IsPrettyCool = true;

 

            ///

            /// Serialization of the class

            /// Incredibly simple and can be used for any class,

            /// just change the type

            ///

            /// string

            public string SerializeToXml()

            {

                  // Serialization of Class into string of xml

                  XmlSerializer xmlsSlzr = new XmlSerializer( typeof( Class1 ) );

                  StringWriter sw = new StringWriter();

                  xmlsSlzr.Serialize( sw, this );

                  return sw.ToString();

            }

      }

}

note:  you could easily make this a static method and pass in the class instance to serialize.  Use reflection to create a type to pass to the XmlSerializer contructor. 

The following code illustrates this:

public static string SerializeToXml(object obj)

            {

                  // Serialization of generic Class into string of xml

                  XmlSerializer xmlsSlzr = new XmlSerializer( obj.GetType() );

                  StringWriter sw = new StringWriter();

                  xmlsSlzr.Serialize( sw, obj);

                  return sw.ToString();

            }

      }

}

 

I don't like this approach as much as it reminds me too much of vb and utility function libraries.  A less oo approach.

Programming | .Net | C#
4/6/2004 9:32:28 PM (GMT Standard Time, UTC+00:00)  #    Comments [2]  |  Trackback
My new car stereo has affected my ability to park correctly...
4/6/2004 5:38:18 PM (GMT Standard Time, UTC+00:00)  #    Comments [1]  |  Trackback
Monday, April 05, 2004

While prototyping a generic form generator, I found success when using Type.GetType() to get a System.Type instance.  I passed in the string name of the “Type” I wished to inspect, for instance:

Type type = Type.GetType("MyNamespace.MyClass");
// List of properties that belong to the given Type
PropertyInfo[] itemTypeProperties = type.GetProperties();
// Object property
PropertyInfo property;
// Loop through the properties of the specified Type
for(int i = 0; i < itemTypeProperties.Length; i++ )
{
   ...make some labels and form fields
}

The class MyNamespace.MyClass was defined in a sepecate .cs file within the same project as the Form where I called the above code.  Everything worked great!

I wanted to move the class .cs file to its own project as it will be reused by more than one project (aah!  good, eh) but, when I did, the above code failed.  ...but, for why?!

I checked that the reference was appropriately made to the new project and that the build order and dependencies were correct but I got a null reference after the line:
Type type = Type.GetType("MyNamespace.MyClass");

I then tried the following.

// Get an instance of the desired class
MyNamespace.MyClass MyInstance = new MyNamespace.MyClass;
// Get the Type from the class instance
Type type = MyInstance.GetType();
// ...continue as before
// List of properties that belong to the given Type
PropertyInfo[] itemTypeProperties = type.GetProperties();
// Object property
PropertyInfo property;
// Loop through the properties of the specified Type
for(int i = 0; i < itemTypeProperties.Length; i++ )
{
   ...make some labels and form fields
}

For the life of me, I can't figure out why moving a file outside the project causes a failure.  I assume there is something I am missing about assembly inspection that is probably quite obvious.  If I figure it out, I'll post a follow up.  If you figure it out, tell me why I'm dumb

Programming | .Net | C#
4/5/2004 9:44:58 PM (GMT Standard Time, UTC+00:00)  #    Comments [1]  |  Trackback

Theme design by Jelle Druyts

Pick a theme: