THE SQL Server Blog Spot on the Web

Welcome to - The SQL Server blog spot on the web Sign in | |
in Search

Peter DeBetta's SQL Programming Blog

Peter DeBetta's blog about programming in SQL Server using technologies such as T-SQL, .NET, CLR, C#, VB, Visual Studio, and SQL Server Management Studio.

Joy of Overloading

This blog is not just about coding in SQL Server. It's also about the data access client. This post is for all you VB.NET and C# developers who are former VB programmers. Having worked with VB since its early days, there were many times when I had wished I could do overload functions and make for a nicer, more respectable interface.

Now I had resisted .NET at first; I didn't think the return on investment for learning C# or VB.NET was worthwhile. Why make the change? I am glad I dug into this technology because not only does my world now revolve around it, but also because it's just so much better.

For example, in the following pseudo-code, I have the overloaded GetRecord method. The two public methods take either a String or Int32, representing, respectively, the Description or ID of the record I am trying to fetch. The private method takes only a SqlCommand object that is created in the other two public methods.

public DataSet GetRecord(String Description)
    SqlCommand sqlCmd =
new SqlCommand("prGetRecordByDescription", this.dbConnection);
new SqlParameter("@Description", (Object)Description));
    return (GetRecord(sqlCmd));

public DataSet GetRecord(Int32 RecordID)
    SqlCommand sqlCmd =
new SqlCommand("prGetRecordByID", this.dbConnection);
new SqlParameter("@RecordID", (Object)RecordID));
    return (GetRecord(sqlCmd));

private DataSet GetRecord(SqlCommand sqlCmd)
    SqlDataAdapter sqlAdapter =
new SqlDataAdapter(sqlCmd);
    DataSet ds =
new DataSet("MyDataSet");

    Int32 ret = dbAdapter.Fill(ds, "MyDataSet");
    return (ds);

The code is simple, clean, elegant. I'm sure there are some VB junkies who are thinking “I could do this using optional parameters and keep the code in a single procedure.” You are right - heck, I've done just that many, many times. It's just that this code is so much nicer. For example, when examining the object, the object browser would show the method twice, once with the RecordID Int32 parameter and once with the Description String parameter. It's practically self documenting. The VB method would appear once with two optional parameters. Meaning, none, either or both could be passed. So, since both parameters are optional, you would have to write additional code to check for the undesired conditions, such as neither parameter being passed and both being passed.

Why am I writing about this. I believe ther are many VB developers making the switch to .NET and I want them to be aware that they have new choices when it comes to writing code and accessing data. VB folks - I'm here for you!

Published Monday, June 26, 2006 12:59 PM by Peter W. DeBetta


No Comments
New Comments to this post are disabled

About Peter W. DeBetta

Peter DeBetta works as a Data and Cloud Solution Manager at DB Best, a Microsoft gold partner company. Peter has written courseware, articles, and books on topics such as development and SQL Server. Peter has spoken at conferences around the world, including TechEd, SQL PASS Community Summit, DevTeach, SQL Connections, DevWeek, and VSLive!

When Peter isn’t working, you can find him singing and playing guitar, taking pictures, working on DIY projects, woodworking, or simply enjoying life with his kids.
Privacy Statement