Archive

Archive for July, 2007

advAjax tips and tricks

July 31st, 2007

I’ve been fooling around with advAjax (Site is down. Here’s the js file — I’d highly suggest you look into jQuery instead, if you can.) for a few years now. All for simple calls, which the small amount of documentation sufficed. However, the last few weeks I’ve had to use the library a lot more for work. I know it’s rather odd to be using a 3rd party Ajax library like this instead of something like ASP.NET Ajax (Oh…In case you didn’t know I work with asp.net 2.0 all day).  I started using ASP.NET Ajax but it was causing a lot of problems with existing JavaScript and other 3rd party controls (Note: This was beta times so who knows if it works now).  I also tried ComponentArt Callbacks but found them to be too limited for my needs.  So now back to the reason for this blog.  I needed to check a value before I would submit an asp.net form. This was a huge headache because, admittedly, I’m  not the best at AJAX; I’ve only dabbled.  I was having the problem that it was submitting the form THEN returning my AJAX call. After spending hours  on this I found that I needed to set my XMLHttpRequest object to be non asynchronous Ajax call.
Great, so now I know what I NEED to do. But how the heck do I do it?  I checked the documentation, google, google groups, other people. I turned up with nothing. So I dove into the code and magically it came to me. There is a field called ‘async’ it’s set to true and I needed it false. So simple enough just add that to your call.

As you see below you can modify the methods by adding them to your advAjax call

advAJAX.get({
async :
false,
url:
‘../AjaxCallback/Director.aspx’,
parameters : {
“command” : ‘check_initials’,
“initials” : str,
“username” : get_txtUsername().value
}
});

List of available (undocumented) fields:

obj.async =
true;
obj.mimeType =
“text/xml”;
obj.username =
null;
obj.password =
null;

(I thought the list was going to be longer…should have looked before I started writing :) )

Anywho, that’s the long drawn out version of what I’ve figured out.

General

C# ref and out parameters

July 10th, 2007

I was coding along and suddenly had a duh moment of what’s the difference between ref and out.  I knew how they work and I’ve used them sparingly before but I didn’t really have a concrete idea of the difference.

So I looked it up
Out = An additional return value

string doot;
int rawr;

rawr = SomeFooMethod(out doot);

Note: This is not to be abused as stated in the MSDN article  “As a matter of good programming style if you find yourself writing a method with many out parameters then you should think about refactoring your code. One possible solution is to package all the return values into a single struct.” — Mental note applied!

Ref = Considered initially assigned by the callee

The MSDN has a much better example than I do….

class RefExample
{
      static object FindNext(object value, object[] data, ref int index)
      {
            // NOTE: index can be used here because it is a ref parameter
            while (index < data.Length)
            {
                  if (data[index].Equals(value))
                  {
                        return data[index];
                  }
                   index += 1;
            }
             return null;
      }

      static void Main()
      {
            object[] data = new object[] {1,2,3,4,2,3,4,5,3};

            int index = 0;
            // NOTE: must assign to index before passing it as a ref parameter
            while (FindNext(3, data, ref index) != null)
            {
                  // NOTE: that FindNext may have modified the value of index
                  System.Console.WriteLine("Found at index {0}", index);
                  index += 1;
            }

            System.Console.WriteLine("Done Find");
      }
}

So there you have it. The difference between ref and out

 - Jeremy

General