Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    June 2003 - Posts

    Easy use of Caching

    Steve Smith blogs about the "standard" way of accessing items in the cache.

    I use this all the time.  I usually do it like this so that you can access it like it as  Property (but in VB.NET of course  ;))

    Public Shared ReadOnly Property MyData() As DataTable
        
    Get
             
    Dim Temp As DataTable = DirectCast
    (HttpContext.Current.Cache("MyData"), DataTable)

              If Temp Is Nothing
    Then
                  
    Temp = GetData()
                   HttpContext.Current.Cache.Insert("MyData", Temp, Nothing
    , DateTime.Now.AddHours(1), TimeSpan.Zero)
              End
    If

             
    Return
    Temp
         End
    Get
    End Property

    You could probably setup a macro to whip out these things pretty fast.  This concept of encapsulating things into Properties also works great on appSettings items.

    Public Shared ReadOnly Property MyProperty() As String
        
    Get
             
    Return
    ConfigurationSettings.AppSettings("MyProperty")
         End
    Get
    End Property

    It might be kind of cool too, to see someone create a plug-in for Visual Studio .NET that would generate a class with a bunch of shared methods to reflect the settings in the web.config (or app.config) file every time you build your project.  A new PowerToy?  Maybe integrate it so it generates a "proxy class" for resources too?  Anybody up for the challenge?  ;)

    Meeting with DGG

    As Marcie mentioned we had a lunch meeting today at Red Robin in Ft. Wayne, Indiana.  She had some troubles finding it, but most of that was because I'm not familiar enough with the area and couldn't give very good directions.

    We talked about a project we're working on, many different areas of Whidbey & PDC this year.

    Had a great time chatting with her.  She's really cool and one of the many .NET'ers everybody should meet!  :)

    Encapsulating UI code in ASP.NET

    I'm working a project right now and I'm currently working on the Admin section, which is basically some hyped up data entry.  I just thought I'd go ahead and share a technique I've been using for a while and see if it might help anybody or if you all think it's a bad idea, etc.

    First, let's setup the structure for a Data Entry Screen.

    Private Enum EditTypes As Byte
        
    Normal = 0
         Add = 1
         Edit = 2
         Delete = 3
    End Enum

    Private Property EditMode() As EditTypes
        
    Get
             
    If ViewState("EditMode") Is Nothing
    Then
                  
    ViewState("EditMode") = [Enum].ToObject(GetType
    (EditTypes), EditTypes.Normal)
             
    End
    If
             
    Return DirectCast([Enum].Parse(GetType
    (EditTypes), ViewState("EditMode").ToString), EditTypes)
        
    End
    Get
        
    Set(ByVal Value As
    EditTypes)
              ViewState("EditMode") = [Enum].ToObject(
    GetType
    (EditTypes), Value)
              UpdateMode()
        
    End
    Set
    End Property

    Basically what all that stuff does is declare an enum with our different types of editing that can be done on our WebForm then sets up a Private Property of the type of that Enum and sets up the Get and Set to interact with the ViewState and store the value of the selected Enum (which in case this would be a Byte) and when set, calls an UpdateMode Method that will update the UI appropriately like so...

    Private Sub UpdateMode()
        
    Select Case
    EditMode
             
    Case
    EditTypes.Normal
                   btnDelete.Enabled =
    False
                  
    btnUpdate.Text = "Add"
                   btnUpdate.Enabled =
    True
                   btnCancel.Visible =
    False
                  
    lstItems.Enabled =
    True
                  
    pnlEditFields.Visible =
    False
                  
    lstItems.SelectedIndex = -1
              Case EditTypes.Add
                   btnDelete.Enabled =
    False
                  
    btnUpdate.Text = "Add"
                   btnUpdate.Enabled =
    True
                  
    btnCancel.Visible =
    True
                  
    lstItems.Enabled =
    False
                  
    pnlEditFields.Visible =
    True
              Case EditTypes.Edit
                   btnDelete.Enabled =
    True
                  
    btnUpdate.Text = "Update"
                   btnUpdate.Enabled =
    True
                  
    btnCancel.Visible =
    True
                  
    lstItems.Enabled =
    True
                  
    pnlEditFields.Visible =
    True
              
    Case
    EditTypes.Delete
                   btnDelete.Enabled =
    True
                  
    btnUpdate.Text = "Add"
                   btnUpdate.Enabled =
    False
                  
    btnCancel.Visible =
    True
                  
    lstItems.Enabled =
    False
                  
    pnlFields.Visible =
    False
         End Select
    End Sub

    Now that we have our Property and UI code setup, making changes to the UI is easy and more readable.

    Private Sub lstItems_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstItems.SelectedIndexChanged
         If lstItems.SelectedIndex > -1
    Then
              'Update the fields in pnlEditFields
             
    EditMode = EditTypes.Edit
        
    End
    If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        
    Select Case
    EditMode
             
    Case
    EditTypes.Add
                  
    'Add a new Item to lstItems
                  
    EditMode = EditTypes.Normal
             
    Case
    EditTypes.Edit
                  
    'Update the underlying Data
                  
    EditMode = EditTypes.Normal
             
    Case
    EditTypes.Normal
                   EditMode = EditTypes.Add
        
    End
    Select
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
         EditMode = EditTypes.Normal
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        
    If EditMode = EditTypes.Delete
    Then
             
    'Delete selected Item
             
    EditMode = EditTypes.Normal
        
    Else
             
    'Let the user know they must
              'click Delete again to delete the Item
             
    EditMode = EditTypes.Delete
        
    End
    If
    End Sub

    There are of course a few other things you'd have to do, but that's basically all the code you'd need for manipulating the UI.  If you add more controls and need to make changes, you'll always know right where to go to make the changes.

    I think by doing things this way it really helps encapsulate UI code (adding almost another virtual layer to your programming model, which is always good for organization), makes it more descriptive (so next time you come back into your code you know exactly what's going on and don't have to remember where any code is) and also makes it really to simply inherit from this page for other pages that have similiar functionality needs.

    What do you all think?  Great idea?  Decent idea?  Horrible idea?

    Posted: Jun 29 2003, 06:25 PM by HumanCompiler | with 2 comment(s)
    Filed under:
    More good times

    Last day was another good one.  Got to meet up with a couple more Microsoft employees.  Andres Aguiar found me in the hallway today, which was cool!  :)  Aaron and Jessica of We're Here picked me up for dinner.  Had a great time hanging out with them.  They're both really cool and very nice people (and so graciously picked me up at the aiport...thanks again!).  Jessica is going to start down the .NET path now that she has a copy of VS.NET and that is great!  :D

    As always, my MVP lead hung out with us MVPs that attended this event and was great.  Had a good time hanging out with him & fellow MVPs, talking about .NET, talking about his son & just about everything else.  Thanks Rafael and the MVP Program!

    Ok, I promise this is the last non-technical blog entry for a while.  ;)

    Posted: Jun 27 2003, 02:30 AM by HumanCompiler | with 2 comment(s)
    Filed under:
    I am no longer a gamer!

    It's official...I am no longer a gamer.  I suck as bad as people who have never even played Halo before.  I totally lost out in the first round of Halo (as mentioned here).  Out of 6 guys, I got 5th place (by 1 kill) and 5th and 6th place were the two dropped out of the tournament!  So that's it...when Halo 2 comes out, I am not sleeping until I'm an expert.  Luckily, I think I have another 6 to 9 months to wait for that!  ;)

    I'll be meeting up with a few new people today.

    Thanks a lot everyone for your comments on yesterday's post. (I didn't know that anyone actually read my blog  ;))  All I can say is you should be pumped about PDC and definitely go this year!  It will be a year to remember (like PDC 2000 was)!  :D

    Posted: Jun 26 2003, 09:14 AM by HumanCompiler | with 2 comment(s)
    Filed under:
    In Seattle

    I'm in Seattle at Microsoft (for the Whidbey SDR [Software Design Review], but can't blog about it since it's an NDA event) and as always am having a great time.

    I've been having fun learning a lot and being amazed by the new stuff (as always) and meeting lots of cool people.  So far:

    • Jacob Grass
    • Ken Getz
    • Mike Harsh
    • Jessica Fosler
    • Duncan Mackenzie
    • Kent Sharkey
    • Joel Semeniuk

    All great people and am having a great time.  There's an XBox tournament tonight.  One is playing Halo.  I think I'm the youngest one here.  I must win!!!  ;)

    TaskVision on GotDotNet Workspaces

    TaskVision, a very nice example of a WindowsForms application, has been added to the GotDotNet Workspace roster.

    /me is signed up!
    (username on GotDotNet is "mybutt")

    Check it out

    VBCommenter 1.0.1 Released

    Today, we released the 1.0.1 version of the VBCommenter PowerToy.  There was a lot of work put in by myself and many others to getting this new version going.

    Basically, this new version is written totally in VB.NET (the original 1.0 version was written in C# by Microsoft employees) and has many bug fixes, a few performance enhancements as well as more support for things like Delegates, ParamArray Parameters, Member Variables, Interfaces, etc.

    The documentation that comes with it, goes into more detail.

    Check out the latest version, with all source included or the Workspace if you're interested in helping out.

    Posted: Jun 18 2003, 07:34 PM by HumanCompiler | with no comments
    Filed under:
    Getting Attributes using Reflection

    Just a quick note to all of you out there who are not Reflection Gurus (I'm sure not yet).  I was working the VBCommenter and I noticed that the COM Name as hard coded in quite a few places for the Add-In.  That wasn't going to work for me, so since the Name for the Command in the IDE (VS.NET) is the COM ProgId given to the Class + "." + the Name (in this case "VBCommenter"), I thought it would be cool to build it dynamically, by grabbing the ProgId.

    So I'm looking around at the Type Class to see how I can get at the ProgId so I can build the string for the Command Name.  I saw the Attributes Property (TypeAtrributes) and couldn't find it there, so following the naming conventions, I started looking for a GetAttributes Method, but there wasn't one...thought this was strange and started searching the help for information on Attributes.  Not much help for what I was looking for.  After a while though, I happened to stumbled across GetCustomAttributes, which is exactly what I need and it worked great!

    Now my problem is why is it called GetCustomAttributes.  It doesn't seem to follow the same naming conventions (i.e. GetMethods, GetProperties, etc).  Luckily a few MVP's came to my rescue in schooling me as to why it's called GetCustomAttributes and not GetAttributes.  Also, Eric Gunnerson of Microsoft gave me and in depth explanation:

    “When we originally designed the CLR, there were no [] attributes.

    In the metadata, the compiler can set attributes on the various items, so
    when we added the [] attributes, we needed a name that differentiated
    between the two.

    So,
    Attributes = bits on objects in the metadata. Things like accessibility,
    virtuality, etc.
    Custom Attributes = customizable annotations next to objects in the metadata

    There are also a few pseudo-custom attributes, where the user model is []
    attributes, but the compiler actually sets bits in the metadata instead of
    using the usual [] attribute mechanism. If you've come across an attribute
    in the code that you can't get to through GetCustomAttributes(), you've run
    into one.

    We now return you to your regular programming, already in progress.”

    So, just wanted to post about that and for anybody else looking to get the attributes that you've set for a class, method, etc, you can do so using GetCustomAttributes.

    VBCommenter PowerToy for Visual Studio .NET 2003

    I have recently become very involved in the VBCommenter PowerToy.  Many programmers are working to improve on what has already been graciously provided by Microsoft.  We have a lot of plans for it and you should check it out if you'd like to do XML Commenting in VB.NET.  We are currently in the process of finishing up testing for the new version of it (1.0.1), which is rewritten in VB.NET (the original by Microsoft was written in C#) and quite a few bug fixes.  If you want to get involved, feel free to join up!  The more coders, the better!  :)

    Check out the VBCommenter PowerToy GotDotNet Workspace

    For a list of all the PowerToys, go here.

    Posted: Jun 13 2003, 04:14 PM by HumanCompiler | with 3 comment(s)
    Filed under:
    More Posts Next page »