Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Compile options in VB9 – explicit and strict

2008-12-13

Compile options can be set for all source code files at the same time in the project file, and they can be overridden in each file. If you want to override a compile option for file, just make sure that the compile option(s) are the first line(s) in the file, before any code. The compile options are option explicit, option strict, option compare and option infer. This post covers option explicit and option strict. 

Option explicit
This option can be on or off, and choosing anything but on is just silly. When option explicit is set to on, all variables have to be explicitly declared. Imagine a function with these two lines:

X = 10
Console.WriteLine(X)

This will only compile if option explicit is set to off. And this is just stupid. All variables will be declared automatically at the first time they are referred to, and mistakes that normally would be caught by the compiler (like a misspell or a value assign of the wrong type) will be accepted by the compiler.

Option strict
This option can be on or off, and you should have a really good reason for choosing anything but on. This has to do with type casting. When option strict is set to off, you can do implisive casting any way you like, just as long as the value fits in the destination variable. If you have the number five stored in a 32 bit integer variable, you can assign it to a 16 bit integer. It’s the same way with objects. You can assign an object to a less specific variable and back to a more specific. You will get a runtime exception if the target type is not suitable. When option strict is set to on, you can only assign values from more specific to less specific variables, without using explicit type casting. This will work independent on option strict:

Dim X As Short = 10
Dim Y As Integer = X

This requires that option strict is set to off:

Dim I As Integer = 4
Dim S As Short = I

And if you want to do the last operation and you are working with option strict set to on, you can do an explicit cast of the type:

Dim I As Integer = 4
Dim S As Short = CType(I, Short)

In a way, working with option strict set to on is good, because that means that you have to prove that the code you’re writing is what you intend to do. That you have to actually do the cast, means that you know that you are doing something that (depending on the value of I) might fail.

Categories: Visual Basic 9

Tags: Compile options

Leave a Reply

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



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips