June 2004 - Posts
Ken's article has already been pointed out by a few folks, but I just wanted to point it out myself and say how great of an article this is. As always, Ken's writing talents have been put to good use. If you want to know about pretty much everything that's new in VB 2005, this should be your first stop.
I do have a couple things I'd like to point though (other than the fact that it's a great read). There is a place in it when he talks about how when writing a Catch now, it will filter the Intellisense list to only classes that inherit from the Exception Class. Until recently I thought this was a great addition, until some other MVPs corrected me and pointed out that this feature is actually in VB today. Shows how often I write Catch's ;)
Also in addition to what Ken mentions in the article about the Using statement, you can also declare multiple variables that have a Dispose Method in a Using Statement. Just separate them with comma's. Again, great article, Ken! :)
Let's say we have a Boolean Array.
Dim
Temp() As Boolean = New Boolean() {True, False, True}
It would currently read like this:
Temp(0) = True
Temp(1) = False
Temp(2) = True
Now call Array.Sort on it (i.e. Array.Sort(Temp)) and it will now read like this:
Temp(0) = False
Temp(1) = True
Temp(2) = True
Great...that's what I'd expect it to return, but now on the original array, call Array.Reverse and it will now read like this:
Temp(0) = True
Temp(1) = False
Temp(2) = True
Nothing has changed from the original...not exactly what I'd except the results to be! :P If this isn't a bug, I'd love for someone to explain to me why it works like this ;) I realize there probably aren't too many people using Boolean Arrays (just offhand, I can't think of any uses, but I'm sure there are uses somewhere), but it still is one of those things that should just work.
UPDATE: Check out the comments below. This has been another “Erik's an idiot attack”. I didn't really understand what Array.Reverse does (not related to Array.Sort at all like I thought) :(
If you read my blog at all, you know that my attitude towards blogging is very lax and whimsical. I make spelling mistakes. I make coding mistakes. I talk about controversial coding debates. I talk about something that totally ends up not being necessary. You get the idea.
Deep inside, I am in constant turmoil (with just about everything I do) about how much or how little I should censor, clean-up and perfect a blog entry before posting it. The perfectionist in me keeps nagging, saying “Your entries are a reflection of you. If you look like an idiot, people will think you don't know how to program very well. Your blog is like a living resume.” My pragmatic self usually wins, however.
I do very much agree with everyone that talks about blogging as a permanent version of your thoughts. Anything you blog about will be there for the rest of your life. Even when sites go down, good ol' Google is there with the “cash“ (as in, cache, since my jokes are never very funny). So it probably is a very good idea not to make crazy claims (my favorite is, “nobody would ever want that“) or just being a jerk because you don't agree with someone or think they're dumb (deal with your issues somewhere else ;)).
That said, one big problem I see with the idea of blogs being “your living resume“ is that they don't necessarily really emit your true intelligence. Anyone can take the time to do research, read other blogs, come up with witty blurbs and whatever else to make a fantastic and super intelligent looking blog entry, but what does that say about the person. The first reaction that comes to mind is, “wow, this person really knows their stuff. I'll bet they're super smart and I would bow down to them if I ever met them.“ For some bloggers this is an absolutely correct assumption, but not necessarily true.
The way I see it, is that looking things up, putting a “paper“ together with everything spelled correctly, talking about facts you've learned from different sources, etc doesn't say jack about you! It's like a really good resume. It could be the best resume (the real one :P) in the world, but it doesn't mean you're going to get the job. A good recruiter can “find“ the real you. A resume is a “foot in the door“ from my perspective. Blogging is the same thing.
To contradict myself though, having a great blog can also help you. See my inner struggle? *sigh*
So what do I have to say about blogging? Well, I really think the best thing you can do is just share what's in your head. I think the best entries I read are the ones with “the little gems“. The tips and tricks that I never knew existed or a new idea for something I never thought of. Everything on my blog that I write is 100% brain dump. I do not proofread anything (ok, sometimes I spell check) and I do not try to rework it so it “sounds“ better. True, this may make me look less intelligent than other people, but what you're getting here is 100% me and I'm proud of that. Nothing makes me go “blah“ more than meeting up with a blogger (if I've met you and you blog, don't cry yourself to sleep tonight because I'm probably not talking about you ;)) or just overhearing a conversation from a blogger I know and admire to hear that s/he doesn't meet up to my expectations from the impression I get reading their “work“.
This brings up the idea of why we blog. Is it to build on our ego? “I couldn't make it as a rock star, but I can certainly become famous in the blogosphere.“ Is it to help others? Save the world? Is it because we're bored? Is it because most of us are introverts and don't take much of an opportunity to share “our cool stuff we make“ with others and have a much broader audience here? I wish I could say that I'm just working on becoming the coding version of Mother Teresa, but I'd be lying to myself. I really think it's a little bit of it all. Don't even tell yourself you don't enjoy reading compliments about your work from people you admire or just someone who you helped! ;)
There are two main reasons why I blog:
- I enjoy helping others. I love to see that some of my entries have been found sooo many times through a Google search. Gives me warm fuzzy feelings! :P
- I love to learn. Being a college dropout you'd probably immediately think I'm lazy and hate learning. That's 100% not the case. I love it. College just wasn't my “style“ of learning, but really do enjoy it. I try to learn as much as I can every day.
Blogging is a great learning tool from both perspectives. Just being corrected by someone on my blog for something I said that wasn't correct is the ultimate learning experience. I know I'll never make those mistakes again. Watching people make mistakes is also a great way to learn. Thinking to yourself, man, I'll never make his/her mistake!
My name is Erik Porter and I make mistakes and I'm proud of it!
On my current project, I needed to have a User Control dumped out a variable amount of times in a Panel with AutoScroll set to True. I thought hey, why not just write a loop to add each item to the Panel's Controls Collection and after adding each item set the Dock Property to Top. After that I can do anything I want with spacing, like put some space in between each item. Something like this in my loop seemed to work pretty well.
With Item
.Dock = DockStyle.Top
With .DockPadding
.Left = 4
.Right = 4
If i > 0 Then
.Top = 4
End If
If i < Count - 1 Then
.Bottom = 4
End If
End With
End With
Yes, I'm a perfectionist and accounted for not having any padding above the first item and after the last item. Even though it's only a few pixels, the details can really stand out and “make the difference” between a good app and a great app.
After running it I noticed there was a big problem: The Items were reversed in my Panel. Not good. I'm not sure if this is a bug in WindowsForms (I made sure to add the item to the Controls Collection THEN Dock it and move onto the next Item) or my mistake, but it just didn't work. I figured I could probably set the Top Property of each item to the value of my loop indexer so each item would be one pixel lower than the previous so the docking would work properly, but I'm not a big fan of workarounds. If you make a mistake, don't cover it up, FIX IT! Turns out one line of code will do it (decided worrying about padding on the left and right wasn't necessary):
Item.Top = (i * Item.Height) + (i * VerticalPadding)
I originally wanted to stay away from this because it seems hoaky and reminds me of my VB6 days, but damn...1 line doing it manually myself? I'll take that over “Docking weirdness” any day. I do love Docking, but it just wasn't right for the job, so I had to send it out the door with a pink slip.
On a personal note, I'm very excited working on this app. I've mostly done ASP.NET lately and as great as a product as it is, I looooong for a Smart Client! ;)
This is a pretty specific case and pretty simple so I'm not sure anyone will get any use out of it (just simple math), but I thought it was interesting and would share (who knows, maybe I'll forget it and need to look it up again).
In an application I'm working on right now, I store a list of parent items in a forward order. When displaying calculations on child items of those parent items, I sometimes need to reverse their order inside the main parent loop (when printing them out). For other purposes the child items are always in a forward order. So basically what I needed was a way to, when looping through the parents backwards for display, get the opposite index of the loop from where I currently was to look up things in the children items. This might seem confusing, so here's an example of what I wanted.
| Actual Loop Index |
Desired Loop Index |
| 0 |
4 |
| 1 |
3 |
| 2 |
2 |
| 3 |
1 |
| 4 |
0 |
This ended up being really easy, but I had to think about it for a minute.
Dim j As Integer
Dim Count As Integer = 5
For i As Integer = 0 To Count - 1
j = Math.Abs(i - (Count - 1))
Next
This code would have the same values for i and j (respectively) as the above table. Pretty simple, but handy.
Sunday night I finally got a chance to play around a bit with the Speech SDK. Pretty cool stuff. I thought first it would be a good idea just to check out some code. Unfortunately, one of the samples I was interested in is in VB6. I haven't had VB6 on any machine for 2 or 3 years and trying to read through the code files is really crappy! :P So I went ahead and installed another XP insance in Virtual PC and installed VB6 (whoa the memories ;)) and check out the ListBox sample. I figured it would help me to just go through every line of code in it. While I was at it, I just made my own version of it in VB.NET.
I kind of got to wondering if one of the reasons that speech stuff for smart clients hasn't really been that popular yet (at least not that I've seen), I decided to post this sample (translated to VB.NET) if anyone else is interested in checking out the Speech SDK 5.1. Just make sure you have the SDK installed and you should be good to go.
Speech SDK 5.1 ListBox Sample .NET
This version has some minor UI differences and I also pulled the main chunk of the code that was in the Sample Control before, so it's just part of the application. Makes it easier to read, navigate, etc...enjoy! :)
One more quickie I just thought of, you can now in VB 2005 have multiple attributes on different lines.
This will work in VB 2005, but not previous versions.
<Attribute1()> _
<Attribute2()> _
Public Class Test
End Class
Previously, you'd have to do it like this...
<Attribute1(), Attribute2()> _
Public Class Test
End Class
Kind of cool! :) This way you can write it either way now and I'd imagine it makes it easier for code generation tools to dump out attributes.
I see Scott talking about Using for VB in VS 2005 and thought I'd throw in another new thing (that I absolutely love and I think also works in C#, but I haven't had time to try it yet).
Private _Test As String = ""
Public Property Test() As String
Get
Return _Test
End Get
Friend Set(ByVal Value As String)
_Test = Value
End Set
End Property
So in this case, inside of my Class Library (the Assembly where my code sits), I can Read and Write to the Property (in case I happen to have any other logic in my setter) and then anytime someone tries to write to the Property outside of the Assembly, they won't be able to (it will appear to be a ReadOnly Property. This is perfect and much needed for we Class Library junkies and I am super pumped about it! :)
Sorry if this has already been talked about. I haven't been able to keep up on reading blogs much lately! :(
This may be categorized under the “Erik is a big idiot and should've known this” section and most people probably already know it, but I think it's one of those not so obvious things that may help some people.
If you've ever dynamically created controls in WindowsForms like filling a Panel with user controls or something along those lines, you may have noticed some really ugly drawing when your form loads. Almost as if it's redrawing every time a control is added. At my last job on my first big project I tried calling the .Hide() Method (on the Panel I'm adding the controls to) before doing anything with the Panel and calling .Show() afterwards. This helped some, but not really. Talking with my bud Jacob Grass about it, he suggested SuspendLayout and ResumeLayout. As soon as he said it I smacked my forehead. If you look in the designer generated code, that's basically what the InitializeComponent Method does for the form; suspends its layout (so the Layout Event isn't fired), adds all the controls to the form and sets them up, then calls ResumeLayout. Changed my code to use those Methods instead anf voila...no constant redrawing and it looks more presentable.
Basically, I would suggest using this the highest most parent when loading. So if you have two Panels with dynamic content, trying using those Methods on the Form they're both on before and after loading. If all you're doing is loading say in one panel, just call them on the Panel before and after loading the controls. Simple tip, but hopefully someone will get a little use out of it.
So I decided to write a plug-in for Visual Studio doing some things with voice recognition (details coming later if I can find enough time to work on it) and went to check out the Speech Applicaton SDK 1.0. Turns out that is for developing Telephony and ASP.NET applications. The thing that I need to use is the Speech SDK 5.1. This SDK is all COM, btw, and has no managed interfaces.
First off, what the heck is up with that? Why are there no managed classes for working with speech “stuff” in Windows applications. To me that's an indicator of either “it's not ready for the big time yet” or “our audience doesn't care about managed code” or “we haven't had time to create a managed version yet”. I may be way off on this, but since living in the world of .NET since RTM, I really don't enjoy doing anything that's not managed.
The other thing about this is why are there two SDK's (other than the obvious that one is managed and one's not)? The answer I got from the newsgroups was along the line of “two products for two different needs”. Again, I'm brand new to anything related to speech, but that seems kind of silly to me. It would like saying it would be a better idea to have a WindowsForms SDK and an ASP.NET SDK instead of just the .NET SDK.
Anywho...enough bitching...it's time to get to it! :) Maybe I'll post another beginners guide like I did for .NET CF once I've used it a bit and get some observations.
More Posts
Next page »