Category: Visual Studio 10

Getting the meta data for a table from Visual Basic

The last time I did this, I needed to build a custom Visual Basic entity generator. After two hours of coding, I had a tiny Windows Forms application that took some connection information and a table name, and returned an entity class with some properties and methods, and a collection class. With initialization, database write-back and all.

From the Management Studio, a call to the sp_help procedure and pass the name of the table you want to receive meta data about, like this:

EXEC sp_help 'dbo.spt_values'

(I still haven’t created any databases on the computer that I am writing this from, so I am grabbing the meta data from the spt_values table in the master database.

The procedure returns a few sets, and the second one contains a list of the table columns.

To call this from Visual Basic, you should know that the procedure is located in the sys namespace, and the parameter that it expects is called @objname. So, if you are using a dataset, the table with index 1 contains the column information. If you are using a data reader, you can call the NextResult function, like so (in a console application):

Using Cn As New SqlClient.SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True")
    Cn.Open()
    Using Cmd As New SqlClient.SqlCommand("[sys].[sp_help]", Cn)
        Cmd.CommandType = CommandType.StoredProcedure
        Cmd.Parameters.AddWithValue("@objname", "dbo.spt_values")
        Dim R As SqlClient.SqlDataReader = Cmd.ExecuteReader()
        R.NextResult()
        Dim ColumnNameColumn As Integer = R.GetOrdinal("Column_name")
        Dim ColumnTypeColumn As Integer = R.GetOrdinal("Type")
        Dim ColumnLength As Integer = R.GetOrdinal("Length")
        While R.Read()
            Console.WriteLine(R.GetString(ColumnNameColumn))
            Console.WriteLine(R.GetString(ColumnTypeColumn))
            Console.WriteLine(R.GetInt32(ColumnLength).ToString())
        End While
        R.Close()
    End Using
    Cn.Close()
End Using

If you are using this to create something like a code generator, remember that Length column holds the column size in bytes. This means that a nvarchar (Unicode string columns) with the length set to 10, only can hold 5 characters.

The above code targets .NET Framework 3.5, but it would look exactly the same in the version above and below. It is written in Visual Basic 10 (VBx).

Hardware accelerated graphics through XNA: Getting started

There are some features of the XNA Framework that is unavailable from Visual Basic, but this should not stop you from writing descent games in Visual Basic. On my machine, I have installed XNA Game Studio 3.1 (a game developing environment from Microsoft) and I also have a beta of Visual Studio 2010 that I am going to use. This example will just contain the code necessary to get something on the screen, a sprite floating across.

From VS2010, I am using a regular console application and the target platform for the project is .NET Framework 3.5.

Now I must add two references: Microsoft.Xna.Framework and Microsoft.Xna.Framework.Game. I use version 3.1, the version that got installed when I installed XNA Game Studio 3.1.

The next step is to create the game class. I call my class TestGame. TestGame should inherit from the Microsoft.Xna.Framework.Game class. In here I create a Main method to get the program started, and I select that method to be the starting point for the program in the Project Settings window. This is the code so far:

Public Class TestGame
    Inherits Microsoft.Xna.Framework.Game

    Public Shared Sub Main()

    End Sub

End Class

In the Main method, I create my game (the TestGame class) and from the constructor, a graphics device manager for the game. I use the graphics device manager to set my preferred resolution (800×600) and to switch to fullscreen mode. Note that I want to keep the reference to the graphics device manager as a member of my game class.

Public Class TestGame
    Inherits Microsoft.Xna.Framework.Game

    Private Gfx As Microsoft.Xna.Framework.GraphicsDeviceManager

    Public Shared Sub Main()
        Dim Game As New TestGame()
        Game.Run()
    End Sub

    Public Sub New()
        Me.Gfx = New Microsoft.Xna.Framework.GraphicsDeviceManager(Me)
        Me.Gfx.PreferredBackBufferWidth = 800
        Me.Gfx.PreferredBackBufferHeight = 600
        If Not Me.Gfx.IsFullScreen Then
            Me.Gfx.ToggleFullScreen()
        End If
    End Sub

End Class

The next thing to do is some overrides from the base class. These methods will be overloaded:

Protected Overrides Sub Initialize()
    MyBase.Initialize()
End Sub

Protected Overrides Sub LoadContent()
    MyBase.LoadContent()
End Sub

Protected Overrides Sub UnloadContent()
    MyBase.UnloadContent()
End Sub

Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
    MyBase.Update(gameTime)
End Sub

Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
    MyBase.Draw(gameTime)
End Sub

Just to make something happen on the screen, I am adding these members:

Private Sb As Microsoft.Xna.Framework.Graphics.SpriteBatch
Private SpriteTexture As Microsoft.Xna.Framework.Graphics.Texture2D
Private SpriteX As Integer = 0
Private SpriteY As Integer = 0

The SpriteBatch will manage my sprites and the Texture2D is the sprite graphics. In the LoadContent function, I will load a sprite from my hard drive.

Protected Overrides Sub LoadContent()
    Me.Sb = New Microsoft.Xna.Framework.Graphics.SpriteBatch(Me.Gfx.GraphicsDevice)
    Me.SpriteTexture = Microsoft.Xna.Framework.Graphics.Texture2D.FromFile(Me.Gfx.GraphicsDevice, _
         "mysprite.png")
    MyBase.LoadContent()
End Sub

The Update function is for changing the game scenery.

Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
    SpriteX += 1
    SpriteY += 1
    MyBase.Update(gameTime)
End Sub

And the Draw function is for screen rendering.

Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
    Me.Gfx.GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Black)
    Me.Sb.Begin(Microsoft.Xna.Framework.Graphics.SpriteBlendMode.AlphaBlend)
    Me.Sb.Draw(Me.SpriteTexture, New Microsoft.Xna.Framework.Rectangle(Me.SpriteX, Me.SpriteY, 32, 32), _
         Microsoft.Xna.Framework.Graphics.Color.Red)
    Me.Sb.End()
    MyBase.Draw(gameTime)
End Sub

This is the complete code that produces a sprite that floats over the screen in Visual Basic using XNA:

Public Class TestGame
    Inherits Microsoft.Xna.Framework.Game

    Private Gfx As Microsoft.Xna.Framework.GraphicsDeviceManager

    Private Sb As Microsoft.Xna.Framework.Graphics.SpriteBatch
    Private SpriteTexture As Microsoft.Xna.Framework.Graphics.Texture2D
    Private SpriteX As Integer = 0
    Private SpriteY As Integer = 0

    Public Shared Sub Main()
        Dim Game As New TestGame()
        Game.Run()
    End Sub

    Public Sub New()
        Me.Gfx = New Microsoft.Xna.Framework.GraphicsDeviceManager(Me)
        Me.Gfx.PreferredBackBufferWidth = 800
        Me.Gfx.PreferredBackBufferHeight = 600
        If Not Me.Gfx.IsFullScreen Then
            Me.Gfx.ToggleFullScreen()
        End If
    End Sub

    Protected Overrides Sub Initialize()
        MyBase.Initialize()
    End Sub

    Protected Overrides Sub LoadContent()
        Me.Sb = New Microsoft.Xna.Framework.Graphics.SpriteBatch(Me.Gfx.GraphicsDevice)
        Me.SpriteTexture = Microsoft.Xna.Framework.Graphics.Texture2D.FromFile(Me.Gfx.GraphicsDevice, _
              "mysprite.png")
        MyBase.LoadContent()
    End Sub

    Protected Overrides Sub UnloadContent()
        MyBase.UnloadContent()
    End Sub

    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
        SpriteX += 1
        SpriteY += 1
        MyBase.Update(gameTime)
    End Sub

    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
        Me.Gfx.GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Black)
        Me.Sb.Begin(Microsoft.Xna.Framework.Graphics.SpriteBlendMode.AlphaBlend)
        Me.Sb.Draw(Me.SpriteTexture, New Microsoft.Xna.Framework.Rectangle(Me.SpriteX, Me.SpriteY, 32, 32), _
                Microsoft.Xna.Framework.Graphics.Color.Red)
        Me.Sb.End()
        MyBase.Draw(gameTime)
    End Sub

End Class

The ups and downs of Visual Studio 2010: Auto list members

In Visual Studio 2010, the auto list members feature is much more intelligent than in previous versions. For example, assuming that X is an object with a visible function called DoSomeWork, in earlier versions of Visual Studio, you would have to know that the method started with Do to find it in the list. If you didn’t know that, you would have to scroll through the list of members and try to recall what you are looking for, or search in the object browser. Now, if you type X.Work, Visual Studio lists DoSomeWork as a suggestion, because matching is done within the name, not only from the beginning of the name. Also, you type an acronym of the function name. DSW would match DoSomeWork.

Previous versions of Visual Studio applied a “suggest and complete” policy, which is the default in Visual Studio 2010. This is usually the most effected mode. If you are aiming to call the DoSomeWork function, you can type something like X.DoSo and then what ever character you was going to type thereafter, likely an opening parenthesis. The problem with the “suggest and complete” policy shows up when you’re trying to call a function that isn’t implemented yet. The text editor will force your typing to be a call to something other than you are aiming at, something that already exists, at least if the not-yet-existing member has a name that is a subset of an existing name. I always end up grabbing my computer mouse when this happens. This is one example where the “suggest only” policy can be useful. This mode lets you type whatever you want, because it will not force you to choose from something in the list.

To toggle completion mode, press Ctrl+Alt+Space or look at the IntelliSense sub menu under the Edit menu. Just as the call hierarchy feature, this feature is available no matter what language you’re using, but (at least in the Beta) it only works in the C# editor, not the Visual Basic editor.

Now, there is no reason to have a mouse connected to the computer anymore. Thank you, Microsoft!

The ups and downs of Visual Studio 2010: Fonts and scaling

The first time I head of the zoom feature of VS2010 was in a talk by Dag König at Microsoft. The level of readability is a very important question for me, and the font rendering of Windows (Clear Type) is superb compared to the blurry font rendering that Macintosh has. The big question, does the zoom feature affect the font rendering of the text editor? To the left, Visual Studio 9 with Consolas 8. To the right, Visual Studio 2010 with Consolas 8.

VS2010 still uses clear type, and the readability is still there. The hue is slightly reduced, if the anti aliasing is too gray, the text will appear blurry as it does on a Macintosh that only uses a gray scale for smoothing edges. The upper side of reducing the hue is that smoothing works better with text in different colors.  In VS2010, the default color for a class keyword in Visual Basic is cyan as opposed to black in VS9.

And the zooming feature: Hold down Ctrl and use your mouse wheel. Not a feature I use every day – I use a font size that I can read without zooming, but really cool to use in presentations.

The .NET Framework lets you control how text is rendered in your programs. The property of the Graphics object you use to control this is called TextRenderingHint. Examine this code:

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
    Handles Me.Paint
    Using B As New System.Drawing.Bitmap(300, 300)
        Using G As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(B)
            Using F As New System.Drawing.Font("Consolas", 10)
                G.FillRectangle(Brushes.White, 0, 0, 300, 300)

                'Regular smoothing
                G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
                G.DrawString("Hello", F, Brushes.Black, 10, 10)

                'Regular smoothing with grid fit
                G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
                G.DrawString("Hello", F, Brushes.Black, 10, 25)

                'Clear Type smoothing with grid fit
                G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
                G.DrawString("Hello", F, Brushes.Black, 10, 40)

            End Using
        End Using
        e.Graphics.DrawImage(B, 0, 0)
    End Using
End Sub

The option called AntiAlias renders text in a similar way to the Macintosh. The text appears blurry, but the outcome has true proportions, meaning that what you see on screen is a blurrier version of what you would see on a printed version. This option works best in high resolutions and for print previewing.

The AntiAliasGridFit makes the text less blurry, because letters align to the pixel grid of the screen. The downside is that character spacing is incorrect, but readability is much higher. This option works best with CRT screens.

The ClearTypeGridFit also aligns characters to the pixel grid, but it uses clear type. This option works best with a flat screen.

The output from the above code is shown here:

The ups and downs of Visual Studio 2010: Call Hierarchy

The function that finds all references to a function or a variable has been in Visual Studio for a few versions now. VS2010 has a similar feature called Call Hierarchy. Right click on any function definition or function call, and click View Call Hierarchy. This shows the Call Hierarchy window with a tree structure that displays what other functions are called from the function you clicked on, and who is calling it. This can help discover unused code and understanding the call stack of a program. In C#, not in Visual Basic. The View Call Hierarchy isn’t there if you right click on a Visual Basic function in VS2010 Beta 2. A quick look at Microsoft Connect tells me that the subject has been brought up, but nothing else has happened yet.

One thing that does work, is the automated generation of sequence diagrams. VS2010 not only does this automatically, but the output is also editable. Objects on the diagram can be resized, moved or deleted, and new items can be added. All you have to do is click Generate Sequence Diagram.

VS at Professional Developers Conference 2008

At PDC2008, Microsoft gave some information about the future of Visual Studio and C#. In 2010, C# and the .NET Framework will be in version 4 and Visual Studio and Visual Basic .NET will be in version 10. For those of you that wasn’t as lucky as mr. Alsing to actually be there, the speeches are available online at http://www.microsoftpdc.com/.

Some of the highlights: Anders Hejlsberg is talking about the future of C# and the game developer Frank Savage talks about XNA Game Studio and the XNA Framework.

Visual Studio 10 and .NET Framework 4.0

Visual Studio 10 and .NET Framework 4.0 is under development and will arrive in 2010. The technology preview is available (VS 10 on a Windows 7 image) and some documentation is also already available. One new cool feature is MGrammar, a grammar modeling language for constructing language syntaxes, a bit like the good old Gold Parser written in VB6.

Microsoft has an introduction to MGrammar on MSDN, and my friend Roger Alsing quickly made a .NET language, Mg Basic, using MGrammar. Check it out!