Tuesday, April 05, 2005 11:46:49 PM UTC :: Filed Under ASP.NET | VB.NET

It seems easier than it is… looping through an array of items (collection) and removing items you don’t want.  However, when you remove an item from a collection while looping through it, the size of the collection changes and the loop may fail… depending on how you are looping.

The correct way to loop through a collection is to essentially loop through it backwards, starting with the item with the maximum ID and working down to zero:

    1  For i As Integer = MyList.Count - 1 To 0 Step -1
    2      If MyList.Items(i).Value <> SomeValue Then
    3          MyList.Remove(MyList(i))
    4      End If
    5  Next
Thursday, March 24, 2005 4:38:14 AM UTC :: Filed Under ASP.NET

Have you ever noticed that some pages on Microsoft's web site have an extension of .ASHX (among other things)?

ASHX files are WebHandlers that derive from IHttpHandler. Their advantages are mainly that you can gain access to the HttpContext without all the overhead of loading the Page class if you don't need the UI.

These classes have only two required methods - ProcessRequest, which accepts an HttpContext parameter, and the boolean IsReusable. In short, ASHX files let you create an HTTP endpoint that can return any content type that you want. And, with ASHX, there is no requirement for registration in web.config or machine.config - you just drop them into your project and they are usable "out of the box".

One thing I've noticed is that the C# guys seem to have all sorts of articles on using ASHX files, but the VB.NET guys do not.   So, after a little bit of research, I figured-out how to write an ASHX page in VB.NET.  The code sample below only has the small function of logging a user out of a web site... Maybe it's not the best example, but its all I've got!

    1 <%@ WebHandler Language="VB" Class="MyNamespace.Web.Logout" %>

    2  

    3 Imports System.Web

    4 Imports System.Web.Security

    5  

    6 Namespace MyNamespace.Web

    7     Public Class Logout

    8         Implements IHttpHandler

    9         Public Sub ProcessRequest(ByVal context As HttpContext) _

   10    Implements IHttpHandler.ProcessRequest

   11             FormsAuthentication.SignOut()

   12             Context.Response.Redirect("default.aspx")

   13         End Sub

   14  

   15         Public ReadOnly Property IsReusable() As Boolean _

   16          Implements IHttpHandler.IsReusable

   17             Get

   18                 Return False

   19             End Get

   20         End Property

   21     End Class

   22 End Namespace

Using an ASHX page is just the tip-of-the-iceberg when it comes to using HTTP Handlers.  Scott Mitchell's article, “Serving Dynamic Content with HTTP Handlers“ shows how HTTP Handlers can be used to format code on web pages, prevent people from “hot-linking” to images on your web site, and re-writing URLs to more user-friendly names.  

Using ASHX and Code-Behind

The method described above shows how to use an ASHX as a single file. However, VisualStudio.NET doesn't recognized the file type, hence no Intellisense support :( I don't know about you, but I can't live without Intellisense, and there is hope! An ASHX file can have a code-behind class file just like an ASPX page. The actual ASHX page should only have a line like the following in it:

<%@ WebHandler Language="VB" Codebehind="Logout.ashx.vb" 
Class="GMR.eFelix.Web.Logout" %>

The ASHX code-behind page would be a normal class file.

Monday, March 21, 2005 7:28:16 PM UTC :: Filed Under Misc

Okay, so I stole the following article from MSN’s Women site, but I wanted to keep the information handy in case I need it in the future!

Secrets of Super-Happy Couples

 

Twelve ways to keep your relationship thriving.


Smart Strategies

Why do some couples seem so head-over-heels? It's not that their lives are any easier or more perfect than yours-- but they do know how to keep the daily grind from eroding their relationship. Get some of what they have by incorporating these happy-couple strategies into your love life.

1. Fall in love all over again. Make a conscious decision to be in love. The more you act as if you are in love, the more you will feel like you are.

2. Remember the good times. Treat your partner like you did at the beginning of your relationship. Make a list of all the things you used to enjoy doing together and add any new fantasies to the list. Plan for them and make them happen.

3. Help your partner feel more loved and secure in your love so that he or she can open up to you and express feelings and ideas without fear of being attacked or judged. Compliment, praise, give a hug. Small gestures make the grandest statements.

4. Don't make unilateral decisions. You're a team in many ways, so act like one. Check in and make decisions together about things large and small. Be willing to compromise.

5. Be present. Train your mind to stay in the moment -- not at work, thinking about the new color you want to paint your kitchen, or how it's time to take the dog to the vet.

6. Pay attention to your physical appearance. Take the time to stay in shape and look good for each other. It does matter.

7. Boost your compatibility. Couples in crisis focus on all the ways they are different, whereas those who are in love zero in on their similarities and think their differences are cute. Build compatibility by taking turns planning activities to do together. If you don't like your partner's choice, don't complain; it's your turn next.

8. Do not place blame. Replace blame and criticism with solutions and tenderness. Problem-solve together -- sit close, hold hands, touch each other's face or hair. Be playful. When was the last time you laughed together? Rent a comedy movie to tickle your funny bone.

9. Plan for sex. Spontaneity is great but smart couples know that good sex doesn't just happen. Like everything else, it takes time and planning.

10. Fact-find -- don't mind-read. You may think you know but you can't assume. You may believe he should know, but that's not fair, either. Always clear up misinterpretations and misunderstandings to make sure they don't throw you both off course.

11. Fight fair -- and by appointment only. Schedule a limited time to discuss a problem and confine your comments to that issue only. It's easier to relax and feel free to enjoy each other when you know you won't be ambushed by a litany of complaints and criticisms.

12. Prepare for checkouts. Even in the closest marriage, everyone needs time alone. Don't take it personally and don't make each other feel guilty if you need to spiritually and emotionally regroup. Just be sure to tell each other when you are checking out (max, one day) -- and when you're checking back in.

Sunday, March 06, 2005 12:41:23 AM UTC :: Filed Under Geek Tips
The problem with my new MPx220 microphone not working started driving me nuts because it was happening on every call.  To me, having to switch to the speaker phone was not an acceptable work-around to a $300 phone not having a working speaker! (I also found-out that the caller on the other end gets a really loud "beep" sound when my phone is switched from the microphone to the speakerphone.) Before my 30 day return policy was up, I took the phone back and exchanged it.  So far, my new phone hasn't had this microphone problem, but I have it configured differently so I'm not sure if the old phone was really broken?
 
With the new phone, I configured ActiveSync to sync with my company's Exchange server on 4 hour intervals instead of having the phone sync every 10 minutes.  I don't use my phone to receive emails, I just like the Exchange calendar sync feature, so I don't have any real need to have the phone constantly syncing. 
 
I feel that by having the phone sync every 10 minutes, there is a pretty good chance that when a call comes-in or when I make a call that the phone may be in the middle of performing a sync in the background and creates a problem that prevents the microphone from working.  Another idea regarding the cause of the problem is that since the phone is running the Windows Mobile OS, perhaps there is a memory leak somewhere and after a full day of making calls to the Exchange server, things start malfunctioning and must be fixed by a reboot (the solution some propose to *solve* this problem.) Rebooting seemed to be a good way to fix issues in previous versions of Windows!
 
One other advantage I've found to setting the sync interval to 4 hours is that any speakers that are near the phone don't pick-up the signal that the phone emits when it's attempting a sync.  Our Cisco Avvid phones at work buzz and hum all day long if the MPx220 is syncing a lot, and that got really annoying, not to mention that it just seems like a health risk!
Thursday, February 24, 2005 3:42:24 PM UTC :: Filed Under Geek Tips

The company I work for recently decided to get a bunch of the new Motorola MPx220 SmartPhones per the suggestion of one of my co-workers who had owned one for a while and liked it.  At first, I didn’t want to give-up my boring, simple Sanyo phone because it worked well as a phone… something that seems rare these days with mobile phones.

My first impression of the MPx220 was “Wow!”  It’s a camera, it’s a PDA, it’s an MP3 player, it’s a video recorder, and last but definitely least, it’s a phone.  I must say that I really like the phone’s ability to sync with our Microsoft Exchange email server.  It’s great being able to pull-out my entire calendar, contact list, or even email pretty much where ever I am.  It was also very nice to not have to type-in all sorts of names and phone numbers like I did on my previous phones.

I find the user interface pretty easy to use and well laid-out.  Considering all the things this phone does, it must’ve been quite a task to design a user friendly UI.  In addition, seems like there are quite a few free downloads out there to help you design your own UI if you so choose.

I haven’t had any time to tinker with the Windows Mobile 2003 OS as of yet, and I probably won’t… I don’t want to break it!  It does look like many others are tinkering though… I’ve seen various forum posts of people trying to adjust speaker volume issues and the like by editing the phone’s registry.

One simply feature that I love (in addition to the Exchange syncing) is that the phone has the ability to automatically switch between a regular sound profile and a meeting sound profile if there is something scheduled in your calendar.  In other words, if you have an 8:00 AM meeting in your Outlook calendar and have the sound ‘profile’ set to ‘automatic’, the phone will automatically change to the ‘meeting profile’ at 8:00 AM.  The meeting profile is set to ‘vibrate’ by default, but you could change that to no ring or a quite ring if you want.

Now for the bad part.  For all the cool features this phone has, the one thing it seems it’s not good at is being a phone!  On my second phone call, the person I was calling couldn’t hear anything I was saying and hung-up.  After doing some research online, many people have problems with the microphone not working (amongst many other problems.)  Most seem to say that restarting the phone fixes this… but who wants to restart their phone daily?  I found that if I quick switch to speakerphone, I can still talk to the person on the other line, but that’s not an acceptable solution to me.   Hopefully this won’t become an ongoing problem.

Overall, if this phone is a sign of things to come, I’m pretty impressed.  As the technology improves, it’ll sure be nice to not have to carry a mini camera, Palm Pilot, and a phone!  However, if you have a very low tolerance for things not working as they should, don’t get one yet or you’ll go nuts :)

Tuesday, February 15, 2005 2:48:04 PM UTC :: Filed Under ASP.NET | VB.NET

Admit it, you do it to.  You can’t remember the .NET way to do something, so you give-in and put Imports Microsoft.VisualBasic at the top of your .vb file.  It’s time to stop the madness! Where there is help, there is hope.

As of recently, a co-worker explained to me the virtues of not using the old-school VB namespace in favor of learning the newer VB.NET way of doing things.  One of the benefits is that VB.NET code is typically optimized to run faster… and we all love speed, don’t we?    There were two places in particular that I kept resorting to the old VB way: date manipulation and formatting.

For example, here is the old VB way to add 7 days to the current date:

DateAdd(DateInterval.Day, 7, Date.Now)

And here is the VB.NET way:

Date.Now.AddDays(7)

That’s not so bad, is it? Now let’s look at formatting.  Sure, there are quite a few ways to do the same thing, but here’s an example.   If you want to format a date type variable in VB:

Format(Date.Now.ToString, "MM/dd/yyyy")

… or …

FormatDateTime(Date.Now, DateFormat.ShortDate)

And in VB.NET:

Date.Now.ToString("MM/dd/yyyy")

Actually, the VB.NET way seems easier! I wish I would’ve explored the VB.NET method sooner.

Here's some more handy conversions:

To subtract to dates:

Dim dblDateDiff As Double = datEndDate.Subtract(datStartDate).Days

So far, I’ve only come across one VB function (but I’m sure there are more) that I haven’t been able to find a VB.NET replacement for: vbTab.  I found the replacement for vbCrLf is Environment.NewLine (one of the new VB.NET classes that is actually less convenient than its VB counterpart), but I have yet to find a way to put a simple tab space into a string.  Not a huge problem, but odd in my opinion since that means I can’t completely lay the Visual Basic namespace to rest!

To help yourself cure your addiction to Visual Basic (the old-school kind), set the filter in the Microsoft Visual Studio.NET 2003 Documentation library to ‘.NET Framework’ instead of ‘Visual Basic’.  I was under the impression that the ‘Visual Basic’ filter meant VB.NET, but it does not.

Navigation
On this page....
Search
Archives
<April 2005>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
Categories
Contact me
Send mail to the author(s) Contact Todd M. Taylor