Questions tagged [option-strict]

The Option Strict Statement restricts implicit data type conversions to only widening conversions which disallows late binding, and also disallows implicit typing which results in an Object type.

Option Strict turns on strong type checking in the Visual Basic .NET programming language.

50 questions
82
votes
8 answers

VB.NET equivalent for C# 'dynamic' with Option Strict On

Is there an equivalent for the C# 4 'dynamic' keyword when using type safe VB.NET, i.e. with Option Strict On?
jeroenh
  • 26,362
  • 10
  • 73
  • 104
55
votes
3 answers

What do Option Strict and Option Explicit do?

I saw this post: Typos… Just use option strict and explicit please.. during one software development project, which I was on as a consultant, they were getting ridiculous amounts of errors everywhere… turned out the developer couldn’t spell and…
yretuta
  • 7,963
  • 17
  • 80
  • 151
19
votes
3 answers

VB.NET Turning Option Strict Off In-Line

Is there a way to turn option strict off for just a single line of code? I'm doing some maintenance work and I need to "cheat" in just one place and I don't want to lower the standard for the entire file.
Jeff
  • 8,020
  • 34
  • 99
  • 157
10
votes
2 answers

What is the best way to mix VB.NET's Option Strict and the new Option Infer directives?

In a related question, my team is about to (hopefully) start using LINQ, and I'd like to take advantage of anonymous types. What is the best way to mix VB.NET's Option Strict (which we've been using through the life of the project) and the new…
Bob King
  • 25,372
  • 6
  • 54
  • 66
5
votes
4 answers

Option Strict on by default in VB.NET

Whenever I created a new VB.NET program I must go into the project's properties and set 'Option strict' on. Can I do that once so it is a default for every time I create a new project?
pho
  • 53
  • 3
5
votes
3 answers

Why does the compiler think this is an Object instead of a DataRow?

I'm using a LINQ query to translate data inside a DataTable object to be a simple IEnumerable of a custom POCO object. My LINQ query is: Dim dtMessages As DataTable '...dtMessages is instantiated ByRef in a helper data access routine...…
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
4
votes
4 answers

Using early binding on a COM object

I have this piece of code that works very well and gives me the path the user's start menu: Dim oShell As Object = CreateObject("Shell.Application") MsgBox(oShell.NameSpace(11).Self.Path) This obviously uses late binding. Now say I want to…
Laurent
  • 5,953
  • 14
  • 43
  • 59
4
votes
2 answers

VB.NET: Which As clause to use with anonymous type with Option Strict On?

Consider the requirement to always declare Option Strict On. We'll always need to declare variables with the As keyword. What would be the type of an anonymous type? Example : Dim product As ... = New With { Key .Name = "paperclips", .Price = 1.29…
Melursus
  • 10,328
  • 19
  • 69
  • 103
4
votes
3 answers

Tools to convert option strict off code into option strict on?

I have to take over a project written in vb.net, which contains more than 400k lines of code written in option strict off mode. I want to build it under option strict on first before I do anything else -- which maybe converting it into C#. I found…
deerchao
  • 10,454
  • 9
  • 55
  • 60
3
votes
0 answers

Option strict... with exceptions?

I like using option strict, for the "discipline" it forces upon you. However there are a few cases, you just don't find any option strict compatible code. For example when you use WScript, I know no way but a CreateObject ... Is there a way to…
Pierre
  • 1,046
  • 7
  • 21
3
votes
1 answer

Does 'Option Strict' rely on 'Option Infer' to detect undeclared variables?

Here are peculiar results I get from this simple piece of code. Say you want to create a variable of type string, without declaring it as a string. You can do this and get no errors from the compiler: Option Strict On ' Produces no errors: Dim…
Eminem
  • 870
  • 5
  • 20
3
votes
8 answers

Do you use strict off in your projects?

Do you use 'strict off' option, 'explicit off'? Or may be 'strict custom' and some other options like 'Implicit type. Object assumed', 'Late binding', 'Implicit conversion'?
olka
2
votes
1 answer

Option Strict On disallows implicit conversions from 'String ' to 'Char' VB.NET

I'm using Option Strict On (and sometimes wishing I wasn't!) but have a piece of code that works as I want it to without it but not with it. It should be quite simple I think but I couldn't find an answer here. My code that works with Option Strict…
shakydd
  • 61
  • 1
  • 8
2
votes
1 answer

Converting enum tostring of underlying type in VB.Net (Option Strict On)

I'd like to get a string representation of the underlying type of the enum. Dim target As System.ConsoleColor = ConsoleColor.Cyan Dim actual = 'What goes here? Dim expected = "11"
Larsenal
  • 49,878
  • 43
  • 152
  • 220
2
votes
1 answer

How to use System.Linq extension method in VB.NET project with Option Strict ON

I'm using .NET 3.5 In my DataLayer class I have references of System.Core,System.Data.Linq, System.Data.DataSetExtensions. But I cantnot use this feature in Linq query if I have Option Strict ON: Dim query = From st In db.Students _ …
Narazana
  • 1,940
  • 15
  • 57
  • 88
1
2 3 4