locked
Option Strict On RRS feed

  • Question

  • There is still quite a bit about this that I do not quite understand, even though I have started using it.

    In this case i have a Public global boolean variable that is declared in a mod file.  Now if I did not have Option Strict On I would use the variable in a form like so:

    If GlobalVariable Then

    Action

    End If

    However, with the Option Strict On I have to do this to be able to use the global variable:

    Private localBoolean = GlobalBoolean

    If localBoolean Then

    Action

    End If

    Why is this so?


    gwboolean

    Friday, July 17, 2015 5:48 PM

Answers

  • GW,

    We've had this discussion before and I realize that you think this is "too much", but I'll suggest again that you use the following:

    Option Strict On
    Option Explicit On
    Option Infer Off

    With those three (and the combination counts also), it will force you to NOT infer anything.

    In this case, the compiler inferred the wrong type.

    I'd guess that it's because when you declared it (you're not showing that) you didn't use the "As" keyword. With all three of those setting as I show above, it won't let you get away with that.

    For what it's worth...


    Still lost in code, just at a little higher level.

    :-)


    Frank,

    The question is about Option Strict not about option Infer. Beside that is it misleading what you write. 

    There is not any relation between Option Strict, Option Explicit and Option Infer beside that the have to be stated as the first rows of a code file.

    The purpose of the forums is not to make easy things more difficult.


    Success
    Cor

    • Marked as answer by gwboolean Friday, July 17, 2015 7:48 PM
    Friday, July 17, 2015 7:02 PM
  • For anyone who actually is interested though, here's a few points:

    • Modules in VB are synonymous with Static Classes in C#
    • A Static Class is a type definition that has no instance methods.
    • "Static" is very much a part of Object Oriented Programming.  There are questions surrounding best practice and testability (which are outside the scope of this thread) but nothing excluding static methods from OOP itself.

    I should also point out, "Modules" weren't just some "silly legacy VB thing".  Static Classes are part of the original C# language specification, and would not have been included if they were some horrific non-oop construct that nobody should use.

    Don't get me wrong, I'm certainly not advocating the use of Static Classes for everything, but they are still part of OOP and they are still applicable under certain circumstances.

    • Proposed as answer by LeonCS Tuesday, July 21, 2015 9:18 AM
    • Marked as answer by gwboolean Tuesday, July 21, 2015 2:51 PM
    Monday, July 20, 2015 4:45 PM

All replies

  • I can't reproduce your problem. The following compiles for me.

    Module1

    Option Strict On
    
    Public Module Module1
        Public GlobalVariable As Boolean 
    End Module

    Form1

    Option Strict On
    
    Public Class Form1
        Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If GlobalVariable Then 
    			
            End If
        End Sub
    End Class

    Friday, July 17, 2015 5:56 PM
  • GW,

    We've had this discussion before and I realize that you think this is "too much", but I'll suggest again that you use the following:

    Option Strict On
    Option Explicit On
    Option Infer Off

    With those three (and the combination counts also), it will force you to NOT infer anything.

    In this case, the compiler inferred the wrong type.

    I'd guess that it's because when you declared it (you're not showing that) you didn't use the "As" keyword. With all three of those setting as I show above, it won't let you get away with that.

    For what it's worth...


    Still lost in code, just at a little higher level.

    :-)


    • Edited by Frank L. Smith Friday, July 17, 2015 6:01 PM ...reworded part of what I had - it was nebulous
    Friday, July 17, 2015 6:00 PM
  • In the future I would recommend you display the actual code you are using in a code block rather than requiring responders to view instructions which do not contain actual code and therefore suspectable to mis-interpretation.

    La vida loca

    Friday, July 17, 2015 6:54 PM
  • GW,

    We've had this discussion before and I realize that you think this is "too much", but I'll suggest again that you use the following:

    Option Strict On
    Option Explicit On
    Option Infer Off

    With those three (and the combination counts also), it will force you to NOT infer anything.

    In this case, the compiler inferred the wrong type.

    I'd guess that it's because when you declared it (you're not showing that) you didn't use the "As" keyword. With all three of those setting as I show above, it won't let you get away with that.

    For what it's worth...


    Still lost in code, just at a little higher level.

    :-)


    Frank,

    The question is about Option Strict not about option Infer. Beside that is it misleading what you write. 

    There is not any relation between Option Strict, Option Explicit and Option Infer beside that the have to be stated as the first rows of a code file.

    The purpose of the forums is not to make easy things more difficult.


    Success
    Cor

    • Marked as answer by gwboolean Friday, July 17, 2015 7:48 PM
    Friday, July 17, 2015 7:02 PM
  • Seems not to be the problem, try to find the real problem 


    Success
    Cor

    Friday, July 17, 2015 7:08 PM

  • Frank,

    The question is about Option Strict not about option Infer. Beside that is it misleading what you write. 

    There is not any relation between Option Strict, Option Explicit and Option Infer beside that the have to be stated as the first rows of a code file.

    The purpose of the forums is not to make easy things more difficult.


    Success
    Cor

    He wants an answer to his question: I answered his question (at least I'm pretty sure).

    You're wrong about the combination: It does matter and I showed that to you several months ago in another thread.

    If you disagree with what I show as a recommendation then ... don't use them! It's still my recommendation to GW.


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 7:08 PM

  • Frank,

    The question is about Option Strict not about option Infer. Beside that is it misleading what you write. 

    There is not any relation between Option Strict, Option Explicit and Option Infer beside that the have to be stated as the first rows of a code file.

    The purpose of the forums is not to make easy things more difficult.


    Success
    Cor

    He wants an answer to his question: I answered his question (at least I'm pretty sure).

    You're wrong about the combination: It does matter and I showed that to you several months ago in another thread.

    If you disagree with what I show as a recommendation then ... don't use them! It's still my recommendation to GW.


    Still lost in code, just at a little higher level.

    :-)

    No I'm not wrong at least study once what those options mean.


    Success
    Cor

    Friday, July 17, 2015 7:09 PM
  • I'm not going to get into an argument with you again Cor.

    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 7:13 PM
  • Sorry, I did not include everything, but I did and am using the options as:

    Option Strict On

    Option Explicit On

    Option Infer Off

    Actually, I did indicate where the variable was declared (in a mod file as a public global variable).  And except for the variable name I used above and the code content within the If statement, that was exactly how my code was setup (for both conditions).  However, I did not show how I declared the variable in the mod and it was done like so:

    glbVariable = False

    I did not use:

    glbVariable As Boolean = False

    I honestly do not even know where I picked up the habit of declaring boolean variables that way.  But thanks for the tip and letting me know why I was having problems with my booleans with Option Strict On.  I guess it really is Strict.


    gwboolean

    Friday, July 17, 2015 7:56 PM
  • One other thing that just occurred to me.  Should I also be using the options in my mod files and namespace files I setup and use?

    gwboolean

    Friday, July 17, 2015 7:57 PM
  • One other thing that just occurred to me.  Should I also be using the options in my mod files and namespace files I setup and use?

    gwboolean

    GW,

    Yes, but a better option is to set them permanently in the options of Visual Studio - then you never need to type them in again. I always show them in code that I post as a reminder, but mine are also set.

    As a final note: Get in the habit of always using an "As" clause. :)


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 8:00 PM
  • Not sure why this is happening but I would highly recommend not using global variables unless they are say in a Singleton class so that there is no guessing if one variable will clash with another one.

    Sample singleton class

    Option Infer Off
    Option Strict On
    Public Class ProjectsVars
        Private Shared _Instance As ProjectsVars
        Protected Sub New()
        End Sub
        Public Shared Function Instance() As ProjectsVars
            If _Instance Is Nothing Then
                _Instance = New ProjectsVars
            End If
            Return _Instance
        End Function
        Public Property SomeBoolean As Boolean
        Public Property SomeInteger As Integer
    End Class
    

    Now let's say you call the following in a button

    ProjectsVars.Instance.SomeInteger += 1

    When seen in a class, code module or another form we would see 1. Press the button again and we see 2.

    The idea here is when absolutely needed to encapsulate variable so we don't class with another one and we can clearly see where the variable is coming from.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    Friday, July 17, 2015 8:02 PM
  • Kev,

    It shouldn't be in a module anyway, if for no other reason than a module isn't OO.


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 8:04 PM
  • It was explained to me, above, why I was having this problem.  It turns out that it was how I was declaring my boolean variables.  I was not using As and apparently Option Strict On is very Strict about that.

    As for the global variables, I like to use them for certain things that I commonly turn on and off.  I know it is not a good practice and I will try, in the future, to eliminate that practice.


    gwboolean

    Friday, July 17, 2015 8:07 PM
  • Frank,

    Are you saying that the options should not be used in a Mod? What is OO?

    As for the Mod, the only thing I use it for is for a handful of global variable that I use.


    gwboolean

    Friday, July 17, 2015 8:10 PM
  • It was explained to me, above, why I was having this problem.  It turns out that it was how I was declaring my boolean variables.  I was not using As and apparently Option Strict On is very Strict about that.

    As for the global variables, I like to use them for certain things that I commonly turn on and off.  I know it is not a good practice and I will try, in the future, to eliminate that practice.


    gwboolean

    Ahh, I thought so. The "As" was missing so it was inferring the wrong type.

    *****

    You don't necessarily have to "get rid of" global variables - if you really do need them then Kevin's notion of a class set up using the Singleton pattern is a good idea. That ensures that you can only ever have one instance of that class.

    But ...

    I'll bring in Scope here:

    Always try to scope things as "tightly" as is practical. If it doesn't need to be at the class level then don't put it there. The sooner something is out of scope, the sooner things get cleaned up (and yea, there's a lot of discussion that can go on just in that one thing that I said there, but the idea is still worth saying).


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 8:13 PM
  • Frank,

    Are you saying that the options should not be used in a Mod? What is OO?

    As for the Mod, the only thing I use it for is for a handful of global variable that I use.


    gwboolean

    There are few things that *have to be* in a module. An extension (Kevin is a pro with those) is one of them but if you don't have to use one then don't. It's the equivalent of a shared class (which doesn't exist in our language) - it has the lifetime of your program. It's essentially "never out of scope".

    "OO" is "Object Oriented". A module cannot be instantiated because it's not a type (in the sense that a class is) and therefore, there is no "object" so of course it's not object-oriented! ;-)

    Please read what I put earlier about scope though. There are a few things you might want to keep "globally" but do limit those because they're never out of scope.


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 8:18 PM
  • Got it!

    A twofer for me.  Now I understand about the Strict declaration (and my bad habit) and why not to use a mod.

    Thanks


    gwboolean

    Friday, July 17, 2015 8:23 PM
  • Got it!

    A twofer for me.  Now I understand about the Strict declaration (and my bad habit) and why not to use a mod.

    Thanks


    gwboolean


    :)

    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 8:26 PM
  • Kev,

    It shouldn't be in a module anyway, if for no other reason than a module isn't OO.


    Still lost in code, just at a little higher level.

    :-)

    I see no issue here, no different then me showing C# developers to use code such as seen in the second code block.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.


    Friday, July 17, 2015 8:30 PM
  • It was explained to me, above, why I was having this problem.  It turns out that it was how I was declaring my boolean variables.  I was not using As and apparently Option Strict On is very Strict about that.

    As for the global variables, I like to use them for certain things that I commonly turn on and off.  I know it is not a good practice and I will try, in the future, to eliminate that practice.


    gwboolean

    Yep "As" is required for Option Strict On

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    Friday, July 17, 2015 8:32 PM
  • Kev,

    It shouldn't be in a module anyway, if for no other reason than a module isn't OO.


    Still lost in code, just at a little higher level.

    :-)

    I see no issue here, no different then me showing C# developers to use code such as seen in the second code block.

    It's like anything really: Know what you're walking into before blindly doing something (in this case, using a module).


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 8:33 PM
  • Yep "As" is required for Option Strict On

    No it isn't. That's what I meant earlier about the combinations mattering.

    Try this:

    Option Strict On Option Explicit Off Option Infer On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim test = "this" Stop End Sub End Class


    It will compile and run just fine.


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 8:37 PM
  • Yep "As" is required for Option Strict On

    I bet that's confusing. ;-)

    Here's the source of the confusion:

    In the absence of being able to use inference, NOW Option Strict will scream about it not having an "As" clause.

    :)


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 8:54 PM
  • Yep "As" is required for Option Strict On

    No it isn't. That's what I meant earlier about the combinations mattering.

    Try this:

    Option Strict On Option Explicit Off Option Infer On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim test = "this" Stop End Sub End Class


    It will compile and run just fine.


    Still lost in code, just at a little higher level.

    :-)

    It is required as I was implying and of course it's not with Option Infer On as you should know by now that is how I code, with Option Infer On with VB.NET as it is more compatible with C# which is my primary language. Only do VB.NET for the forums these days as my shop is pure C#/JQuery/Entity Framework.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    Friday, July 17, 2015 9:44 PM
  • Kev,

    Well anyway, we've pretty well covered this topic ad infinitum. ;-)


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 17, 2015 9:50 PM
  • Kev,

    It shouldn't be in a module anyway, if for no other reason than a module isn't OO.


    Still lost in code, just at a little higher level.

    :-)


    Modules are nothing more than static classes (which is exactly how they translate in C#).  This doesn't make them "non OO", it means they're part of a different aspect of OO. 
    Monday, July 20, 2015 3:37 PM
  • Modules are nothing more than static classes (which is exactly how they translate in C#).  This doesn't make them "non OO", it means they're part of a different aspect of OO. 

    Based on this MSDN document, you're incorrect:

    "Object Orientation.Classes are object-oriented, but modules are not. So only classes can be instantiated as objects."


    Still lost in code, just at a little higher level.

    :-)

    Monday, July 20, 2015 3:54 PM
  • Modules are nothing more than static classes (which is exactly how they translate in C#).  This doesn't make them "non OO", it means they're part of a different aspect of OO. 

    Based on this MSDN document, you're incorrect:

    "Object Orientation.Classes are object-oriented, but modules are not. So only classes can be instantiated as objects."


    Still lost in code, just at a little higher level.

    :-)

    And yet in this MSDN document, entitled "Object-Oriented Programming (C# and Visual Basic)", they reference "Static (Shared) Members and Classes" and directly reference the Module statement.  It does not say that this is "Not OOP", and note that this article is from 2015.

    A Module in VB is just a Static Class (which is how it's defined in C#).  That doesn't make it outside of the definition of OOP, in fact "Static" is very much a part of the OOP definition.

    Monday, July 20, 2015 4:12 PM
  • It does not say that this is "Not OOP"...

    What? Read it!

    It most certainly does say that a module is NOT OO!

    Classes are object-oriented, but modules are not.

    That's pretty clear.


    Still lost in code, just at a little higher level.

    :-)

    Monday, July 20, 2015 4:15 PM
  • It does not say that this is "Not OOP"...

    What? Read it!

    It most certainly does say that a module is NOT OO!

    Classes are object-oriented, but modules are not.

    That's pretty clear.


    Still lost in code, just at a little higher level.

    :-)

    MY Linked article (the 2015 one) does not say that.  Yours does.  Read it! ;-)
    Monday, July 20, 2015 4:26 PM
  • MY Linked article (the 2015 one) does not say that.  Yours does.  Read it! ;-)

    No, I won't read it.

    MSDN shows that it's NOT; that's as far as I need to add to this because that's the basis of what I said Friday.

    You can believe whatever you choose to...


    Still lost in code, just at a little higher level.

    :-)

    Monday, July 20, 2015 4:31 PM
  • MY Linked article (the 2015 one) does not say that.  Yours does.  Read it! ;-)

    No, I won't read it.

    MSDN shows that it's NOT; that's as far as I need to add to this because that's the basis of what I said Frida


    My article is also MSDN.  And it's newer than yours.  But I can see how you would rather not read it, Frank... ;-)
    Monday, July 20, 2015 4:36 PM
  • My article is also MSDN.  And it's newer than yours.  But I can see how you would rather not read it, Frank... ;-)

    I'll say again: The basis of what I posted on Friday was the MSDN document that I referenced.

    A module isn't a "type" like a class is. A module cannot be instantiated. How then can anyone rightfully call it OO?

    Moreover, the absence of something doesn't prove anything, but all of this is just jabbering for no useful purpose.

    You believe whatever you want to. I'm fine with that. ;-)


    Still lost in code, just at a little higher level.

    :-)

    Monday, July 20, 2015 4:40 PM
  • For anyone who actually is interested though, here's a few points:

    • Modules in VB are synonymous with Static Classes in C#
    • A Static Class is a type definition that has no instance methods.
    • "Static" is very much a part of Object Oriented Programming.  There are questions surrounding best practice and testability (which are outside the scope of this thread) but nothing excluding static methods from OOP itself.

    I should also point out, "Modules" weren't just some "silly legacy VB thing".  Static Classes are part of the original C# language specification, and would not have been included if they were some horrific non-oop construct that nobody should use.

    Don't get me wrong, I'm certainly not advocating the use of Static Classes for everything, but they are still part of OOP and they are still applicable under certain circumstances.

    • Proposed as answer by LeonCS Tuesday, July 21, 2015 9:18 AM
    • Marked as answer by gwboolean Tuesday, July 21, 2015 2:51 PM
    Monday, July 20, 2015 4:45 PM
  • A module isn't a "type" like a class is. A module cannot be instantiated. How then can anyone rightfully call it OO?

    Because it's not called "Instance Only Oriented Programming", Frank.  Do you honestly believe everything must be instance-based regardless of whether it has any state or instance requirement, and that if something is Static it cannot be part of the Objected Oriented Paradigm?

    Because you're wrong, if so.  But you believe whatever you want to ;-)

    Monday, July 20, 2015 4:55 PM
  • But you believe whatever you want to ;-)
    I shall.

    Still lost in code, just at a little higher level.

    :-)

    Monday, July 20, 2015 4:57 PM
  • A module isn't a "type" like a class is.

    A Module is a Static Class.  That is how a module is defined in C#.  It's not a "closest equivalent" either, it is the exact same thing.
    Monday, July 20, 2015 5:00 PM
  • You guys are talking way beyond my pay grade.  However, what I seem to be getting out of all of this, is that using a module is probably not the best practice?  The only reason I do it, is that it came out of a book I was reading and it looked OK to me.

    gwboolean

    Monday, July 20, 2015 5:13 PM
  • You guys are talking way beyond my pay grade.  However, what I seem to be getting out of all of this, is that using a module is probably not the best practice?  The only reason I do it, is that it came out of a book I was reading and it looked OK to me.

    gwboolean


    Steering back to what I said Friday, remember that a module is never out of scope.

    Still lost in code, just at a little higher level.

    :-)

    Monday, July 20, 2015 5:17 PM
  • Yes modules are the same as C# static classes.

    The following are the same in (taken from a recent article I wrote)

    VB.NET

    Imports System.Xml 
    Public Module DocumentExtensions 
        ''' <summary> 
        ''' Concert a XDocument to a XmlDocument 
        ''' </summary> 
        ''' <param name="xDocument"></param> 
        ''' <returns></returns> 
        ''' <remarks></remarks> 
        <System.Runtime.CompilerServices.Extension> _ 
        Public Function ToXmlDocument(ByVal xDocument As XDocument) As XmlDocument 
            Dim xmlDocument = New XmlDocument() 
            Using xmlReader = xDocument.CreateReader() 
                xmlDocument.Load(xmlReader) 
            End Using 
            Return xmlDocument 
        End Function 
        ''' <summary> 
        ''' Convert an XmlDocument to a XDocument 
        ''' </summary> 
        ''' <param name="xmlDocument"></param> 
        ''' <returns></returns> 
        ''' <remarks></remarks> 
        <System.Runtime.CompilerServices.Extension> _ 
        Public Function ToXDocument(ByVal xmlDocument As XmlDocument) As XDocument 
            Using nodeReader = New XmlNodeReader(xmlDocument) 
                nodeReader.MoveToContent() 
                Return XDocument.Load(nodeReader) 
            End Using 
        End Function 
    End Module

    C#

    using System.Xml; 
    using System.Xml.Linq; 
     
    namespace ExtensionMethods_CS 
    { 
        public static class DocumentExtensions 
        { 
            public static XmlDocument ToXmlDocument(this XDocument xDocument) 
            { 
                var xmlDocument = new XmlDocument(); 
                using (var xmlReader = xDocument.CreateReader()) 
                { 
                    xmlDocument.Load(xmlReader); 
                } 
                return xmlDocument; 
            } 
     
            public static XDocument ToXDocument(this XmlDocument xmlDocument) 
            { 
                using (var nodeReader = new XmlNodeReader(xmlDocument)) 
                { 
                    nodeReader.MoveToContent(); 
                    return XDocument.Load(nodeReader); 
                } 
            } 
        } 
    }
    The only time modules should be used in a OOP solution is for language extension methods.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    Monday, July 20, 2015 6:05 PM
  • Yes modules are the same as C# static classes.


    @Kevin,

    I don't know if you expressly did this (no problem) but that above is of course in general. 

    To be more precise.

    Both have some legacy to do the same as C++ and VB6 and therefore they are not complete the same.

    Static C# classes can have non static members while the access modifiers of a VB module are in my opinion really weird. 

    However, they have the same goal.  

    And then to add to this discussion of OOP. In my knowledge it can be OO but not OOP it is simply not programming with objects, although the design can be OO. This difference seems to be difficult for many. 


    Success
    Cor


    Monday, July 20, 2015 6:43 PM
  • Static C# classes can have non static members

    No, they cannot.

    Static Classes (MSDN)

    Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated. The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created

    Monday, July 20, 2015 7:09 PM
  • Static C# classes can have non static members

    No, they cannot.

    Static Classes (MSDN)

    Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated. The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created

    You are right about this part of my reply. I mixed up Non Static classes with static members against this. 

    For the rest I keep it and it is quite different from what you wrote here. 



    Success
    Cor


    Monday, July 20, 2015 7:34 PM
  • For the rest I keep it and it is quite different from what you wrote here. 



    Success
    Cor


    Actually I didn't write it per se, I copied and pasted it directly from the MSDN article I linked to.
    Monday, July 20, 2015 9:17 PM