The differences between Get, Set and Let

By | 2007-01-13

One question a VB programmer is asked a lot about, especially in a job interview, is the differences between let, set, and get.  Now some of the advanced programmers out there might laugh, but you”d be surprised at how many beginning developers trip up on this.  I”ve asked the question of interviewees myself.  So, in hopes of helping out budding programmers, here is the explanation.

Let Statement

[Let] varname = expression

Assigns the value of an expression to a variable (varname). The Let keyword is usually omitted and is assumed by Visual Basic.

For example:

Let intMyNumber = 5
Let strMyString = "Text Goes Here"

Get Statement

Get [#]filenumber,[ recnumber,] varname

Reads data from the open disk file corresponding to the filenumber argument into a variable (varname). Get works with files open as Random or Binary, and a record number (recnumber) can be specified when retrieving data from a Random file. When using Binary files, recnumber can alternatively be used to specify the byte position from which the data is to be read.

Set Statement

Set objectvar = {[New] objectexpression | Nothing}

Assigns an object reference (objectexpression) to a variable or property (objectvar). The optional New keyword can be used to indicate that the object should be created implicitly. To disassociate objectvar with a specific object and free up the resources it is using, assign it the Nothing keyword.

For example, using your textbox example:

Set MyTextBox as Textbox

This is also used to instantiate new instances of classes, recordsets, and other things. Basically, it allows you to treat just about anything as an object.

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.