Tips for golfing in Visual Basic

2

This page is not for Visual Basic .NET Tips or VBA tips

Visual Basic (VB or VB6) is an object-oriented and event-driven dialect of that was developed and released by Microsoft in 1991. The last release of Visual Basic came as Visual Basic 6.0 in 1998, and the language was declared a legacy language in 2008. Visual Basic .NET replaced Visual Basic in 2002.

All answers on this site that are written in Visual Basic are assumed to be written for Visual Basic 6.0 unless otherwise stated.

Some helpful links

You may compile and execute Visual Basic code using Visual Studio Community.

There is no online resource for compiling VB code.

Taylor Scott

Posted 2018-02-15T06:45:06.227

Reputation: 6 709

IDK VB, but isn't this just https://codegolf.stackexchange.com/questions/5175/tips-for-golfing-in-vba?rq=1

– Pavel – 2018-02-15T07:27:46.920

@Pavel The short answer is no, there are several differences between VB and VBA, including, but not limited to the availability of the certain packages, data types, non-variant arrays, threading and proper graphics in VB that are not available to VBA and version specific tweaks available to VBA - such as the Cells command in Excel VBA. You can find out more about the difference here

– Taylor Scott – 2018-02-15T18:09:17.223

consider using the normal [tips] format from other tips questions as the main part of your question (applies also to the other recent tip question) – Uriel – 2018-02-15T20:55:46.440

@Uriel you mention a [tips] format but as far as I can see there are several that are currently in use on the site, could you please be a bit more specific about exactly what format you are referring to? – Taylor Scott – 2018-02-15T21:02:59.023

like the one on https://codegolf.stackexchange.com/questions/54/tips-for-golfing-in-python. I think that is the one I've seen the most. Language info usually goes into the showcase question, not the tips.

– Uriel – 2018-02-15T21:06:19.317

Answers

1

Use DefXXX statements when declaring multiple variables

The statement

DefDbl A-D

Defines all variables that start with A through D as being of the type Double, which means this can shorten cases such as

Dim A As Double
Dim B As Double
Dim C As Double
Dim D As Double

Similar commands exist for the following types

DefBool     ''  Boolean
DefByte     ''  byte
DefCur      ''  Currency
DefDate     ''  Date
DefDbl      ''  Double
DefDec      ''  Decimal
DefInt      ''  Integer
DefLng      ''  Long
DefObj      ''  Object
DefSng      ''  Single
DefStr      ''  String
DefVar      ''  Variant

Taylor Scott

Posted 2018-02-15T06:45:06.227

Reputation: 6 709

0

Use Console.Write over Console.WriteLine

There is quite a large amount of overhead code necessary to get a snippet of code to execute in VB, and by default this overhead is configured to take up quite a large amount of bytes - for instance

Module M
Sub Main
Console.WriteLine("Hello, World!")
End Sub
End Module

May be golfed down by 4 bytes by the Console.Write(...) command over Console.WriteLine(...).

Module M
Sub Main
Console.Write("Hello, World!")
End Sub
End Module

Taylor Scott

Posted 2018-02-15T06:45:06.227

Reputation: 6 709

Tips question are usually required not to offer whitespace and name shortening tips, as they apply for all languages and go under the generic tip question. I don't think this answer should appear here. – Uriel – 2018-02-15T20:55:01.173

@Uriel - you are completely correct, I've modified the response; please continue to let me know anything I can do to improve this resource. – Taylor Scott – 2018-02-15T20:58:58.227

I still don't think that existence of Write method is worth an answer, but that's debatable. anyway, please refrain from long codes. You could make just fine with only the central line - it just clutters pages – Uriel – 2018-02-15T20:59:40.480