Answered by:
Option Strict Off

Question
-
Option
Strict OffOption
Explicit On
how thes statments useful in when creation of classes in vb.net
what does they mean
adilSunday, November 9, 2008 9:41 AM
Answers
-
Finally,
The above code corrected to avoid the LATE BINDING error with OPTION STRICT ON.
So I recommend the settings as stated earlier by forum user Mr John Wein.
Option Explicit On
Option Strict On
Option Infer Off
I will leave you to discover what OPTION COMPARE does. :-)
Regards,
John
Option Strict On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Set the FORM text to 1234567890123456890 >> Me.Text = 1234567890123456890.ToString 'Maximize the FORM.>> Me.WindowState = FormWindowState.Maximized 'Used to hold the current BUTTON.>> Dim btn As Button 'Used for the BUTTON number.>> Dim ButtonNumber As Integer = 0 For y As Integer = 1 To 5 For x = 1 To 10 'Add one to the BUTTON number.>> ButtonNumber = ButtonNumber + 1 'Assign a NEW Button to btn.>> btn = New Button 'Give the Button a SIZE.>> btn.Size = New Size(40, 25) 'Assign the current number to the TEXT property of the button.>> btn.Text = ButtonNumber.ToString 'Assign a NAME of Button1 ... to Button50 as appropriate.>> btn.Name = "Button" & ButtonNumber.ToString 'Give it a LOCATION on the FORM.>> btn.Location = New Point((x * 40) + 30, (y * 25) + 40) 'Add a HANDLER to HANDLE the CLICK event.>> AddHandler btn.Click, AddressOf MyButtonsClickSub 'Add the Button to the FORM.>> Me.Controls.Add(btn) Next Next End Sub Private Sub MyButtonsClickSub(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Define a button object.>> Dim ButtonClicked As Button 'Convert the sender object to TYPE Button 'assigning the result to ButtonClicked.>> ButtonClicked = CType(sender, Button) 'Show which button was clicked.>> MessageBox.Show(ButtonClicked.Name) End Sub End Class
For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835- Edited by John Anthony Oliver Monday, November 10, 2008 4:58 AM ....
- Marked as answer by adilahmed Monday, November 10, 2008 7:01 AM
Monday, November 10, 2008 4:51 AM
All replies
-
Option Strict -> http://msdn.microsoft.com/en-us/library/zcd4xwzs(VS.71).aspx
Option Explicit -> http://msdn.microsoft.com/en-us/library/y9341s4f(VS.80).aspx
Option Explicit should always be on, to catch any small typos in your code. Personally, I never have Option Strict on, and have yet to encounter a problem.
WayneSunday, November 9, 2008 9:51 AM -
they both should be on.Sunday, November 9, 2008 9:43 PM
-
Option Strict Off allows late binding which drastically slows a program. It also allows very sloppy programming. If you need assistance and you post code that compiled for you with Option Strict Off, the squiggly lines will likely turn off quite a few of the responders on these forums. For the cleanest, most versatile and maintainable code set:
Option Explicit On
Option Strict On
Option Infer Off- Proposed as answer by John Anthony Oliver Monday, November 10, 2008 2:53 AM
Sunday, November 9, 2008 10:15 PM -
webtubbs said:
Option Strict -> http://msdn.microsoft.com/en-us/library/zcd4xwzs(VS.71).aspx
Option Explicit -> http://msdn.microsoft.com/en-us/library/y9341s4f(VS.80).aspx
Option Explicit should always be on, to catch any small typos in your code. Personally, I never have Option Strict on, and have yet to encounter a problem.
Wayne
Hi ALL,
For completeness sake only.>>
OPTION INFER -> http://msdn.microsoft.com/en-us/library/bb384665.aspx
OPTION COMPARE -> http://msdn.microsoft.com/en-us/library/8t3khw5f(VS.71).aspx
Regards,
John
For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835- Edited by John Anthony Oliver Monday, November 10, 2008 3:00 AM ....
Monday, November 10, 2008 2:57 AM -
Hi ALL,
With OPTION INFER ON this is okay.>>
For x = 1 To 10 Nextas x is INFERRED to be an INTEGER
otherwise it has to be.>>
For x As Integer = 1 To 10 Next
'or
Dim
x As Integer For x = 1 To 10 Next
Regards,
John
For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835Monday, November 10, 2008 3:24 AM -
Hi ALL,
With OPTION EXPLICIT ON
you can not do this.>>
Number4 = 40
you have to state or be explicit about what you intend like this.>>
Dim Number4 As Integer = 40
which is the best thing to do,that is use the As INTEGER or whatever to remove any doubt ( ambiguity ) you may have.
'otherwise Number4 is an INTEGER with OPTION INFER ON & OPTION EXPLICIT OFF
'otherwise if you turn OPTION INFER OFF it is an OBJECT again.>>
Dim Number4 = 40
Regards,
John
For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835- Edited by John Anthony Oliver Thursday, February 25, 2010 8:04 AM
Monday, November 10, 2008 4:02 AM -
Hi ALL again,
OPTION STRICT OFF would allow you to do this.>>
TextBox1.Text = 1234
Me.Text = 456
Which is naughty really as the numbers 1234 and 456 above are not in STRING format.
OPTION STRICT ON forces you to convert to the right TYPE
for assigning one thing to another like this.>>
TextBox1.Text = 1234.ToString
'or
TextBox1.Text = (1234).ToString
'or
TextBox1.Text = Convert.ToString(1234)
'or
TextBox1.Text = CStr(1234)
'or simply
TextBox1.Text = "1234"
Regards,
John
For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835Monday, November 10, 2008 4:13 AM -
Almost there,
To demonstrate a LATE BINDING error due to OPTION STRICT ON try this code then turn
Option Strict On
You do not need any controls added to a FORM to try this code.
To overcome such an error see the next post where the blue highlighted code is changed.
Regards,
John
Option Strict Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Set the FORM text to 1234567890123456890 >> Me.Text = 1234567890123456890.ToString 'Maximize the FORM.>> Me.WindowState = FormWindowState.Maximized 'Used to hold the current BUTTON.>> Dim btn As Button 'Used for the BUTTON number.>> Dim ButtonNumber As Integer = 0 For y As Integer = 1 To 5 For x = 1 To 10 'Add one to the BUTTON number.>> ButtonNumber = ButtonNumber + 1 'Assign a NEW Button to btn.>> btn = New Button 'Give the Button a SIZE.>> btn.Size = New Size(40, 25) 'Assign the current number to the TEXT property of the button.>> btn.Text = ButtonNumber.ToString 'Assign a NAME of Button1 ... to Button50 as appropriate.>> btn.Name = "Button" & ButtonNumber.ToString 'Give it a LOCATION on the FORM.>> btn.Location = New Point((x * 40) + 30, (y * 25) + 40) 'Add a HANDLER to HANDLE the CLICK event.>> AddHandler btn.Click, AddressOf MyButtonsClickSub 'Add the Button to the FORM.>> Me.Controls.Add(btn) Next Next End Sub Private Sub MyButtonsClickSub(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Show which button was clicked on.>> MessageBox.Show(sender.name) End Sub End Class
For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835- Edited by John Anthony Oliver Monday, November 10, 2008 4:46 AM ....
Monday, November 10, 2008 4:33 AM -
Finally,
The above code corrected to avoid the LATE BINDING error with OPTION STRICT ON.
So I recommend the settings as stated earlier by forum user Mr John Wein.
Option Explicit On
Option Strict On
Option Infer Off
I will leave you to discover what OPTION COMPARE does. :-)
Regards,
John
Option Strict On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Set the FORM text to 1234567890123456890 >> Me.Text = 1234567890123456890.ToString 'Maximize the FORM.>> Me.WindowState = FormWindowState.Maximized 'Used to hold the current BUTTON.>> Dim btn As Button 'Used for the BUTTON number.>> Dim ButtonNumber As Integer = 0 For y As Integer = 1 To 5 For x = 1 To 10 'Add one to the BUTTON number.>> ButtonNumber = ButtonNumber + 1 'Assign a NEW Button to btn.>> btn = New Button 'Give the Button a SIZE.>> btn.Size = New Size(40, 25) 'Assign the current number to the TEXT property of the button.>> btn.Text = ButtonNumber.ToString 'Assign a NAME of Button1 ... to Button50 as appropriate.>> btn.Name = "Button" & ButtonNumber.ToString 'Give it a LOCATION on the FORM.>> btn.Location = New Point((x * 40) + 30, (y * 25) + 40) 'Add a HANDLER to HANDLE the CLICK event.>> AddHandler btn.Click, AddressOf MyButtonsClickSub 'Add the Button to the FORM.>> Me.Controls.Add(btn) Next Next End Sub Private Sub MyButtonsClickSub(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Define a button object.>> Dim ButtonClicked As Button 'Convert the sender object to TYPE Button 'assigning the result to ButtonClicked.>> ButtonClicked = CType(sender, Button) 'Show which button was clicked.>> MessageBox.Show(ButtonClicked.Name) End Sub End Class
For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835- Edited by John Anthony Oliver Monday, November 10, 2008 4:58 AM ....
- Marked as answer by adilahmed Monday, November 10, 2008 7:01 AM
Monday, November 10, 2008 4:51 AM -
Short note:
Code written that works with OPTION STRICT ON will usually ALWAYS work with
OPTION STRICT OFF ( unless your code generates some other error ).
Code written that works with OPTION STRICT OFF will NOT ALWAYS work with OPTION STRICT ON.- Edited by John Anthony Oliver Saturday, November 14, 2009 2:15 AM
Monday, November 10, 2008 5:08 AM -
Thanks John Anthony Oliver , i read your posts, its really very useful
Thanks for this help.
adilMonday, November 10, 2008 6:49 AM -
Hi Adil,
You're welcome.
See also.>>
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/ed44dd90-ab13-4f31-a69d-d471f7a27854/
Regards,
John
For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835Monday, November 10, 2008 7:16 AM