A little ‘bit’ more on Int32 vs Integer


Darren’s

comments on

my previous post:

I certainly don’t want to get into a debate over this but, shouldn’t the
question be: “what is it best to use?”.

I’m probably biased because I
never really programmed in any typed languages before .NET, but, to me at least,
being as explicit as possible is always a good thing.

I know that Int32
will map to System.Int32 even in version 30 of the Framework. God knows what
Integer or int will map to by then.

Instead of replying
back to him directly or in the comments, I thought I would try replying in
another blog entry (there certainly are some issues to be
worked out regarding the best way to comment/discuss)..

Good point Darren, thanks. The problem I
see is that the question of “best” isn’t an easy one to answer. If you are
writing your code to be dependent on the size of the underlying data type, then
the desire to be explicit is certainly understandable, but when programming in
C# or VB.NET you do not always need to be aware of this implementation detail.
(that hurt to even type, I’m a big fan of always knowing about the details…
but my point is that you shouldn’t have to know about them in
this case). In fact, I think you should try to avoid being implementation
dependent as a general rule.

As I mentioned in my last post, if I’m
working in a situation, such as a Win32 API call, where the exact size of
the integer, the number of bits involved, is relevant then I use it. In all
other situations, I need to know that my data type (Integer) is big
enough
to hold the range of values I want to work with. Personally, I
noticed and even wrote about the fact that Integers and Longs have changed from
VB6 to VB.NET, but that change has had almost no effect on most VB6 code
upgraded to VB.NET, except when calling out to external libraries. So, if in
future implementations of the VB.NET compiler, they decide that (for efficiency
on the standard processors of the day, perhaps) Integer should map to Int64,
then I would hope that wouldn’t cause me much of a problem… if they decided,
and I don’t think they would, to change the mapping to Int16, then I think I
would be screwed.

Right after the PDC in 2000, I had just
installed the .NET Framework “alpha” onto a machine and was busily writing a
book on VB.NET… I decided that I should always use fully qualified types
(System.Text.StringBuilder, instead of using an Imports and then just
StringBuilder)… .NET was so new to many people that I wanted to err on the
side of clarity. I also decided that I shouldn’t use things like Integer or
MsgBox and that I should instead use System.Int32 and
System.Windows.Forms.MessageBox.Show()… in time though I realized that I
prefer to work with the terms/keywords that I am familiar with, and that even
with Intellisense, I prefer to work with relatively short and easy to remember
terms as well. So, now I use Imports for common namespaces (on a code file by
code file basis, I really find the “default” Imports in VS.NET confusing)
and I use language specific data types. I think that this is natural for
people, and I wouldn’t want to try and change developers to Int32 or to change
VB6 developers so that they stop using MsgBox(“Test”). Developers have
habits; for example, I still use hungarian notation in lots of my own code
(sorry Brad, I did read the
design guidelines, I really did… I even use FxCop… my public members are
generally ok, I just can’t stop using hungarian in my private variables!) even
though it is no longer recommended.

I am still undecided about using fully
qualified types in samples, sometimes people copy/paste a snippet of code from
one of my articles that it is not a complete class or code file (and therefore
does not include any Imports statements) and get quite confused when it doesn’t
compile and they have all of these squigglies under the class names. When I can
write code samples in such a way that the required Imports are automatically
attached (even to a single code snippet) then I won’t have to use fully
qualified class names, but for now I think I will have to change my coding style
for snippets.

Darren, you said you didn’t want a
debate and here I am replying with such a long post… but there really is no
debate, at least not from me. I don’t know what the “best” way is, I
just hope that some of my comments help explain why I have chosen a certain
set of coding rules.

Int32 or Integer/int?


Brad
Abrams posted about the use BCL types vs. language specific type names (in
sample code) today
, which just happens to be a topic I’ve had to think about
myself when writing sample code for my own articles, so I thought I would
make a comment.

I’m with Morty
on this one… I have the general rule that sample code should be written
following the style and convention guidelines for the language. I’m a VB guy, so
this usually takes the form of “Write VB code the VB way”, but there are
exceptions…. For example, I use Integer in my Visual Basic .NET code, not
Int32, but I like to use Int32 in Win32 API declarations; the specific size of
integer is pretty important at that point so I like to make it clear… Beyond
types it gets a little murky… I always use the System.IO classes for File
access; VB.NET still has the Open/Put commands but I truly believe that those
“legacy” statements are not as good/clear as the new System.IO classes…
whereas in the case of Int32 vs. Integer (or MsgBox vs. MessageBox.Show())
it is merely a syntax/style difference.

I think using Int32 in a VB.NET/C# code
sample makes it slightly less familar to a C++ developer (in
the case of C#) or a VB6 developer (in the case of VB6)… and that makes the
developer a little less comfortable, and therefore a
little less likely to understand what they’ve been shown. The
purpose of a sample is to explain the concept in terms of code. The purpose of
doing in more than one language is to explain the concept in terms that the
developer is already familiar with… well, following that same
guideline and knowing that a VB.NET developer is more familar with Integer than
Int32, they will benefit more from seeing the sample written using the
VB data type names.

Of course, all of this can be ignored if
Int32 was the class being demonstrated 🙂

I hate to get involved at all, but…


There have been a few VB vs C# debates flying around… I’ll refrain
from linking to them all, as they aren’t really saying anything new. One comment
in particular caught my eye though, and I have to
respond.

 

Jesse
Ezell’s post
includes a quote that says “Microsoft is standardizing on C#”
and then goes on to use that quote as a reason to suggest you shouldn’t use
VB.NET… well, I work for Microsoft and I am in discussions with the .NET
groups every day and I know exactly what we are working on and working with, and
I can tell you that Microsoft is not “standardizing” on anything.   Yes, much of our internal .NET
development is done with C#, and why not? We are a huge campus full of C++
developers. If we did standardize on anything, I would expect it to be Managed
C++.  Also, it’s worth pointing out
that we do development in VB as well, including some of our internal business
applications.

 

Before .NET, Microsoft used C++ almost exclusively (and we still do
almost everything in C++) but that doesn’t mean that everyone should use C++ to
build their line of business applications. People also point out that much of
the Framework was written in C#, but remember, it was written by C++ developers.
There isn’t a single class, method, property, or event, anywhere in the
Framework, that you can’t use from VB.NET. If someone builds a library in C#
that you can’t use from VB.NET, (and vice versa) they probably did it wrong…

 

C# is the flashy new language that has arrived with .NET so it gets a
lot of attention (which led to more samples being written in C#, etc…), but
Visual Basic is by far the more popular language and I don’t expect that to
change. I write code in Visual Basic everyday and nothing in the Framework is
being “hidden” from me, I have full access to the Framework in all its glory. I
don’t think there is any need to ignore C#, but you certainly don’t have to
learn it to work in .NET…

 

Stop debating this issue; pick a language and build something…
you’ll feel better.

One Step Closer to Releasing MusicXP Source

I have a home music
system, that I have dubbed


MusicXP

(mostly
because that is the name of the machine it is on, the actual app that runs
is called “Shell.exe”)… and I’ve been meaning to upload the full source for
some time. I’ve covered bits and pieces of it in


various



articles

but now I’ve decided to create a

GotDotNet Workspace

and get the whole thing up there.

I added the first set of files to the
Workspace today (as a “release”), the mdf/ldf files for the database, which
aren’t all that useful on their own, but I thought it would be good to post
something and start moving down the path to full disclosure:)

Why haven’t I just posted the entire app
up as is? To be perfectly honest, I’m trying to walkthrough the code once before
I post it, to remove anything that ties it directly to my implementation (yep, I
might have hardcoded a path or something deep in there…), and even that simple
task seems to be taking me ages!

First hint of what I’ve been working on for the last few months…

We’ve posted a
little “first look” at the new Developer Centers at


http://msdn.microsoft.com/vbasic/firstlook/

. These sites, well the vbasic one in particular
are my world and we’ve been working hard at them for quite awhile. As the
article at the end of that link says, they will be going up April 24th, and I
really hope you like what you see.

Oh, just fyi: there will also be ones
for the .NET Framework, C#, VStudio, and VC++ … if for some reason you feel
the need to go anywhere outside of the Visual Basic
site.

This isn’t exactly useful, but I find it cool

I’ve been working
on code to pull the currently playing item out of Windows Media Player and in my
quest I ran into


this

, a plug-in that pulls that value out and writes it to a ftp site of
your choosing for display on the web (


here is mine

)… so
everyone can know what you are currently listening to.

I’m still working on my .NET code to
grab this value, but in the meantime… this is pretty
cool.

My column went up early…

It wasn’t really
supposed to be up until Friday, and I doubt you’ll find any links to it anywhere
on MSDN until next week, but you lucky readers of my blog can check out
the next installment of Coding4Fun on MSDN now…

Checking
My E-Mail

Duncan Mackenzie
Microsoft Developer Network

March 26, 2003

Summary: Duncan Mackenzie describes how to
build a tool that uses the System.Net namespace of the Microsoft .NET
Framework to check a POP3 e-mail account for unread messages. (15 printed
pages)

Big thanks to Darren for testing an early build
of my code sample… and I’m very sorry that it set his machine on
fire.

First day and he has figured it out already….

Kent’s first day
with my group (to avoid confusion: “my group” means the group I belong to, I
don’t manage anyone…) and he has already realized the truth about
us.

“Day 1 – spent most of it in the cafeteria, as it
was a move day. Two meetings (much lower than my day would have been). Yes, my
new team is/are lunatics 😉 (They know that already, and yes, I know they
all read this
)” –
Kent Sharkey’s Blog

For those of you who might be wondering,
Kent has just joined MSDN, and more
specifically he has become the ASP.NET Content Strategist. As a group, we
content strategists are involved in planning out the articles (and other
materials) that MSDN should write, link to and/or acquire around various
development technologies. I’m the Visual Basic Content Strategist, Steve Kirk is
our man on Architecture,
Matt Powell covers the exciting world of Web Services, Frank
Redmond
is all over the .NET Framework, Brian
Johnson
just joined us as the Security strategist, Paul
Johns 
is working with something called C#, and Shawn Morrissey (our boss) covers
Visual Studio… oh yeah, Tim Ewald is also
part of our group… but no one really knows what he
does
😉

Macros in VS.NET

I have to admit I
haven’t taken advantage of enough of VS.NET’s capabilities… the ability to
write macros alone should have resulted in a ton of useful little routines, but
I have only written a few. One of the ones I use most often converts between a
list of my internal member variables in a class…

    Dim m_fred As String
    Dim m_counter As Integer

to the bare-bones properties

    Public Property fred() As String
        Get
            Return m_fred
        End Get
        Set(ByVal Value As String)
            m_fred = Value
        End Set
    End Property

    Public Property counter() As Integer
        Get
            Return m_counter
        End Get
        Set(ByVal Value As Integer)
            m_counter = Value
        End Set
    End Property

It is dependent on my particular hungarian-ish (m_) naming
style for internal variables and it doesn’t deal well with arrays or
variables that get instantiated in their declarations… but I find
it a real timesaver to spit out that initial pass at the
properties before I go in and add any validation or whatever else
I was going to do… On the off chance that you might find it useful as
well, or that you want to “finish it up”, here is the source of the
macro:


Sub ConvertProperties()
    DTE.UndoContext.Open("ConvertProperties")
    Try
        Dim txt As TextSelection
        txt = DTE.ActiveDocument.Selection

        Dim line, originalCode As String
        originalCode = txt.Text

        Dim lines() As String
        lines = Split(originalCode, vbLf)
        Dim variableName As String
        Dim publicName As String
        Dim dataType As String
        Dim propertyProcedure As String

        Dim r As Regex
        r = New Regex( _
        "(Dim|Private)\s*(?<varname>\S*)\s*As\s*(?<typename>\S*)", _
        RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)

        For Each line In lines
            line = line.Trim
            If Not line = "" Then
                Dim mtch As Match
                mtch = r.Match(line)
                If mtch.Success Then
                    variableName = mtch.Groups("varname").Value.Trim
                    dataType = mtch.Groups("typename").Value.Trim
                    publicName = variableName.Substring(2)

                    propertyProcedure = _
                        String.Format("{0}Public Property {1} As {2}{0}" _
                            & "    Get{0}" _
                            & "        Return {3}{0}" _
                            & "    End Get{0}" _
                            & "    Set(ByVal Value As {2}){0}" _
                            & "        {3} = Value{0}" _
                            & "    End Set{0}" _
                            & "End Property", vbCrLf, publicName, _
                            dataType, variableName)

                    txt.Insert(vbCrLf & propertyProcedure, _
                        vsInsertFlags.vsInsertFlagsInsertAtEnd)
                End If

            End If
        Next
        txt.SmartFormat()
    Catch
        'don't do anything
        'but I don't want to see an error!
    End Try
    DTE.UndoContext.Close()
End Sub

Anyone have some real cool/useful VS.NET macros?