Option Strict - On or Off?

What are your opinions regarding Option Strict?

Personally, I see no great advantage to having it on. Yes, it may mean my code will run a few nanoseconds slower. But it also means my code will be far more versatile, and I can use Late Binding, which is very handy when coding for MS Office applications.

Please don't post links. I won't read them. I'm after *your* opinion.

Wayne
LVL 48
Wayne Taylor (webtubbs)Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BraidenGCommented:
Hi Wayne,

Personally, I don't bother with the Option Strict call, as I have found it doesnt cause me any hassle late binding form and such.

I just use a basic late binding code:

Dim A as New Frm1
Panel1.Controls.Add(A)
A.Show

Don't quote me on the exacts of that code, but that's how I would Late Bind a form to a Panel, or for that matter just Late Bind it full stop.

My Opinion, Option Strict isn't necessary, yet it doesn't cause any harm either, since the time ammount of time that it consumes is rarely noticeable other than in larger projects.

Braiden
0
Fernando SotoRetiredCommented:
Hi Wayne,

When programming in VB .Net I am one who uses Option Strict On. The reason why I do so  is to have the compiler inform me of things like narrowing conversion so that I can properly deal with the issue at design time and to prevent data lost then a client informing me that the program is not giving the correct results when the program has been released. The added benefit of improves performance not matter how small it is, besides it may mean allot more in a large program then a small one, I will always take a performance boost when given it. As far as Office Automation goes, in .Net you do not need to late bind you can use early binding to work with objects such as Excel, Access and Word.

Fernando
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi Wayne,

Now I know you said, "Please don't post links. I won't read them. I'm after *your* opinion." I gave my option in the last post but if you are interested in hoe to early bind Microsoft Office products into a VB .Net program you can go to this link.
 
http://www.xtremevbtalk.com/showthread.php?t=160433

Fernando
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Wayne Taylor (webtubbs)Author Commented:
Fernando,

I'm aware of how to use Early Binding with MS Office. The problem lies in the different versions. If you use Late Binding, you're providing the user the ability to use whatever version of Office they have installed.

Could you also explain why this segment causes problems when Option Strict is on....

        Listview1.Items.AddRange(lvis.ToArray(type:=GetType(ListViewItem)))

....where 'lvis' is an ArrayList of ListViewItems. It works perfectly, but the banality of Option Strict means it will fail.

FWIW - Even though I have Option Strict Off, I always set the compiler to warn me of such errors, and I can choose whether the warnings are meaningless or not.

Wayne
0
Fernando SotoRetiredCommented:
Hi Wayne;

Listview1.Items.AddRange(lvis.ToArray(type:=GetType(ListViewItem)))

The above statement converts the ArrayList lvis to a System.Array of ListViewItem But the ListView1 control is expecting a 1-dimensional array of ListViewItem as seen from the error message that follows.

Error message:
Public Sub AddRange(items() As System.Windows.Forms.ListViewItem)':
Option Strict On disallows implicit conversions from 'System.Array' to '1-dimensional array of System.Windows.Forms.ListViewItem'.      

So with Option Strict On you need to do the following explicitly convert the ArrayList to an 1-dimensional array of ListViewItem as follows:

ListView1.Items.AddRange(CType(lvis.ToArray(type:=GetType(ListViewItem)), ListViewItem()))

Fernando
0
Wayne Taylor (webtubbs)Author Commented:
Yes, that's how you'd avoid the problem with Option Strict. The point I'm making is that it's already an Array of ListViewItems. Explicitly converting only confuses things.

Wayne
0
Fernando SotoRetiredCommented:
Hi Wayne;

The following is with the understanding that Options Strict is set to On.

The ArrayList.ToArray(Type) method copies the elements of the array list to a System.Array with a type of Type.

Public Overridable Function ToArray ( _
    type As Type _
) As Array

The above method definition is from Microsoft documentation. The System.Array is the base class for other arrays and can access its member elements with either a For Each loop, enumerating the members, or the use of the GetValue / SetValue methods but cannot be accessed as a normal array with an index like mySystemArray(i). So if we defined an ArrayList as follows:

        ' Creates an ArrayList.
        Dim myAL As New ArrayList()
        myAL.Add("The")
        myAL.Add("quick")
        myAL.Add("brown")
        myAL.Add("fox")
        myAL.Add("jumped")
        myAL.Add("over")
        myAL.Add("the")
        myAL.Add("lazy")
        myAL.Add("dog")

        ' And now converted it to an array with the ToArray(String) which returns a System.Array
        Dim mySysArray As Array = myAL.ToArray(GetType(String))
        ' I can iterate through the elements like this
        For Each str As String In mySysArray
            Console.WriteLine(str)
        Next

        Console.WriteLine(New String("-"c, 40))

        'Or I can do this
        For idx As Integer = 0 To 8
            ' The GetValue method returns an object of type Object and not string
            Console.WriteLine(mySysArray.GetValue(idx))
        Next

        Console.WriteLine(New String("-"c, 40))

        ' But you CANNOT do this with Option Strict On but can be done with Option Strict Off
        For idx As Integer = 0 To 8
            'Using an index like in a normal array is not valid with Strict On. My understanding
            ' is that this is a late bound array and the reason why it works in Strict Off
            Console.WriteLine(mySysArray(idx))
        Next

        Console.WriteLine(New String("-"c, 40))

        ' So in oreder to make the return value of the ToArray(Type) method work with the AddRange mothod
        ' we need to convert the System.Array to an array of ListViewItem, Dim lvi() As ListViewItem.

Fernando
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.