CodeGuru
Earthweb Search
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Recent Entries

Monthly Archives

Internet.com / blogs

Free Tech Newsletters
Free Tech Newsletters
ADO.NET Entity Framework Tutorial and Basics
Discover an ADO.NET Entity Framework tutorial covering basic data operations for applications, including LINQ To Entities, Method Expressions, stored procedure mapping, and a little discussion about its usage in an enterprise environment.

Asynchronous Socket Programming in C#: Part I
This is a simple Client-Server application developed to explain the concept of asynchronous sockets in C#. This extends the socket program to accept multiple clients. Because this example uses Asynchronous methods, threads are not necessary to communicate to multiple clients (though internally the asynchronous communication mechanism uses threads at the OS level).

.NET Web Services Tutorial
Writing .NET Web services without using Visual Studio can be tricky. Learn how to create a Web Service without using Visual Studio .NET.

Free Tech Newsletters
Deploying and Configuring SQL Server Integration Services Packages Without the Wizard
Ever get confused by or dislike using the SQL Server Integration Services deployment wizard? Learn how to manually deploy the packages to SQL Server 2005 and configure the packages in a multitude of ways as well.
Improving Portal Page Load Performance
Portals provide users with access to more applications from a single point of entry, but if it takes too long to get in,, they will go elsewhere. Learn specific techniques to improve page load time and track down what is slowing your pages down.
Handling that Pesky Windows ControlBox
Some things that seem hard are actually quite easy, and some things that seem easy take a little work, like that little [X] on a Windows Form, the ControlBox. Learn how to know just when that little bugger is pressed.
Getting Up and Running with the Composite UI Application Block for WPF
Discover how to build complex Windows Presentation Foundation (WPF) applications with reusability and componentized design in mind by using ready-made patterns.
NEW and IMPROVED: A Lesson in Change Management and Project Management
What is the most effective way to introduce change within a department or organization?

And the Visual Basic Type Is....

| Comments (1) | TrackBacks (0)

Continuing on my last very simplistic blog post, I thought I'd show something seemingly even more basic. While the following is simple from our perspective, I imagine the coding done by Microsoft on the back-end to make this simple was much more challenging.

 

Consider a scaled down version of the Visual Basic 10 listing I created in my last blog post:

 

Module Module1

    Sub Main()

        Dim scores() As Integer = {10, 20, 30, 40}

        Console.WriteLine(scores.GetType)

    End Sub

End Module

 

Let me ask you a simple question. What is the type of the scores array? It should be obvious that it is an Integer. What, however, is the type of the scores() array if we change the third line to the following:

 

Module Module1

    Sub Main()

        Dim scores()= {10, 20, 30, 40}

        Console.WriteLine(scores.GetType)

    End Sub

End Module

 

The answer is no longer clear. In fact, the answer is different if you are using Visual Basic "10" instead of an earlier version. In Visual Basic 2005/8, the answer would be that it is an array of type System.Object. In Visual Basic 10, it would actually be of type Integer still.

 

Type is an Integer

 

The type is inferred in Visual Basic "10". If I change the line to the following, the type will again change:

 

Dim scores()= {10, 20, 30, 40, 3.14}

 

Because a number with a decimal place has been added, in Visual Basic "10" the type will now be inferred to be Double.

 

Type is now Double

 

 

Making one more change, what would the type of scores be for the following?

 

Dim scores()= {10, 20, 30, 40, <strong>"Now what?"</strong> }

 

If you guessed string, you'd be wrong. At this point, there are too many possibilities from the type of array that scores could be to start guessing. As such, scores falls back to being of type System.Object.

 

This, of course, is how the pre-beta works. Chances are this will make it into the final release. This is one more way that Visual Basic provides great ease-of-use functionality. It is also one more way that Visual Basic can help you shoot yourself in the foot.

 

0 TrackBacks

Listed below are links to blogs that reference this entry: And the Visual Basic Type Is.....

TrackBack URL for this entry: https://swarm.jupitermedia.com/mt-tb.cgi/6243

1 Comments

January 7, 2009 7:37 AM Richard said:

VB9 already has type inference for non-array types, so long as you turn on "Option Infer":

Dim i = 5
Console.WriteLine(i.GetType)
'System.Int32


I'm surprised it doesn't support arrays - C# does:

var scores = new[] { 10, 20, 30, 40 };
Console.WriteLine(scores.GetType());
// System.Int32[]

var scores2 = new[] { 10, 20, 30, 40, 3.14 };
Console.WriteLine(scores2.GetType());
// System.Double[]

var scores3 = new[] { 10, 20, 30, 40, "A" };
// Compilation error: No best type found for implicitly-typed array

Leave a comment

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers