Tips for golfing in Visual Basic .NET

1

This page is not for Visual Basic Tips

Visual Basic .NET (VB.NET) is an multi-paradigm dialect of that was developed and released by Microsoft in 2002 as a replacement for Visual Basic. The current version of VB.Net as of Feb 2018 is Visual Basic .NET 15.0.

Unlike Visual Basic (VB), Visual Basic .NET (VB.NET) is fully integrated with the Microsoft .NET Framework giving it access to a far wider range of classes to access. Additionally, the bit width of integers and longs in VB.NET are 32 and 64 bits, respectively - twice that available to VB.

Some helpful links

Taylor Scott

Posted 2018-02-15T06:47:35.087

Reputation: 6 709

I think that the tips for golfing in uBasic, Yabasic, Visual Basic and Visual Basic.NET are all pretty similar, possibly enough to dupe close. Even if they aren't, we don't need a tips page for every dialect of a language, such as separate pages for Java 7, 8 and 9. – caird coinheringaahing – 2018-02-15T07:50:07.847

Caird, this issue with that is that while logically similar they have completely different syntax - for instance, uBasic requires line numbers, does not require spacing between any commands and barely has strings, let alone proper graphics and events implemented where as for VB.NET there are not required line numbers, modules are required, you have to use Console.Write instead of ?, it has proper graphics implemented events out the wazoo and the entirety of the .NET library available to it - to say that they should be treated the same is simply illogical – Taylor Scott – 2018-02-15T17:53:50.640

@cairdcoinheringaahing That said, I would agree that VB and VB.NET are very similar - I mean VB.NET is the replacement for VB, but they are completely different and distinct languages - that is, code written in VB may not run in VB.NET and code written in VB.NET may not run in VB. Further between the two there is currently 20 years worth of continuous professional development time between them, and this is reflected in the paradigms of writing in the two – Taylor Scott – 2018-02-15T17:59:40.663

Answers

1

Dim Variables implicitly as variants

VB.NET is capable of assuming object type at compile time - with numerics being dimmed into 32-bit spaces this means that

Module M
Sub Main
Dim i As Byte
Dim j As Short
Dim k As Integer
Dim l As Long
For i=128To 2^8-1 Step 64
For j=2^7*i To 2^15-1 Step 64^2
For k=2^16*j To 2^31-1 Step 2^24
For l=2^31*k To 2^62-1 Step 2^48
Console.Write(i & j & k & l)
Next
Next
Next
Next
End Sub
End Module

Try it online! (266 bytes)

May be trimmed down by dimming i, j, k as variants, (note that because the default numeric type of a variant is a 32 Bit Integer l must still be dimmed as a Long as it shall hold a 64 bit value)

Module M
Sub Main
Dim i,j,k,l As Long
For i=128To 2^8-1 Step 64
For j=2^7*i To 2^15-1 Step 64^2
For k=2^16*j To 2^31-1 Step 2^24
For l=2^31*k To 2^62-1 Step 2^48
Console.Write(i &j &k &l)
Next
Next
Next
Next
End Sub
End Module

Try it online! (226 bytes)

or even further to 218 bytes by combining Next statements

Module M
Sub Main
Dim i,j,k,l As Long
For i=128To 2^8-1 Step 64
For j=2^7*i To 2^15-1 Step 64^2
For k=2^16*j To 2^31-1 Step 8^8
For l=2^31*k To 2^62-1 Step 2^48
Console.Write(i &j &k &l)
Next l,k,j,i
End Sub
End Module

Try it online! (218 bytes) For a total savings of 40 bytes without impacting the output.

Taylor Scott

Posted 2018-02-15T06:47:35.087

Reputation: 6 709

22^24 is also 8^8. – recursive – 2018-03-17T23:19:55.113

1

Combine Dim Statements

The VB6 command

Dim i As Double
Dim j As Double
Dim k as Double
Dim l As Double

Can be rewritten as

Dim i,j,k,l as Double

Note that this is dissimilar from VB6 where only l would be defined as a double and i,j,k would be defined as variants.

Taylor Scott

Posted 2018-02-15T06:47:35.087

Reputation: 6 709