Option Strict On - Complain About Casting Image To Bitmap?
May 18, 2010
Why does "OPTION STRICT ON" complain about casting an Image to Bitmap. When CTYPE is not used but not the other way around?
Dim img As Image = Image.FromFile(My.Computer.FileSystem.SpecialDirectories.MyPictures & "ExampleFile.jpg")
'This is okay with OPTION STRICT ON.>>
Dim bmp As Bitmap = CType(img, Bitmap)
'OPTION STRICT ON allows this but why? >>
PictureBox1.BackgroundImage = bmp
'Why not? >>
PictureBox1.BackgroundImage = CType(bmp, Image)
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 there's thousands of lines of code raises compilation error, mostly are about implicit type casts. Is there any tool would help to make it compile under option strict on mode if I don't want to correct every single line manually? Because it's really painful to add CStr/CInt invocation into every line of Code myself.
I have inherited a VB.NET application that I need to compile so I can run dorkumentation on it. I first received "Option Strict On disallows implicit conversion from x to y" errors, so I turned off the Option Strict option in the Project file.
So why do I still fail with the same error message?
I really like the coding speed that vb.net provides, but I don't like the possibility to forget to declare variable types, return types of functions, etc. and that is why in each class I use[code]..
Is there a way to define those two options on the project/solution level?
what does these two code means in vb.net: Option Explicit on Option strict on i think option explicit means the compiler is not going to do any kind of conversion and i need to do all of them by the code;also it becomes case sensitive i.e;
1. I have read that keeping Option strict on and Option infer off is good practice and will insure your code is tight and properly written. I would like input regarding this, pro's and con's from those who are in the know.
2. Having said that, I have set Option strict on and Option infer off in an existing program I wrote (that was working perfectly mind you) with them off and on respectively. I went through and cleaned up all the errors on the 'need AS' and casting of variables, but one is leaving me a bit put out.[code]
I have also read where using My.Computer.System.Directory.GetFiles() instead of System.IO.Directory.GetFiles() is probably not a good thing, but when I change to this I get no error, but also no dataI am thinking I should stay with the strict on and infer off but am really slogging through getting this code correct.Running Win7 on Dual Quad Core XEON Intel Extreme with 8Gb RAM.
what do you thinkg about comment below? original article url is also suggested to read.I didnt get why my code will be faster if i use it on. anyone has good knowledge about it?
Although Visual Basic .NET allows you to perform implicit type conversions and late binding, you should avoid these practices. Implicit type conversions and late binding may lead to severe performance problems, runtime errors, code that is difficult to read and maintain,and sub-standard programming practices.Many VB.NET professional programmers believe that leaving OPTION STRICT OFF by default was perhaps Microsoft's worst decision in the VB.NET implementation. Use OPTION STRICT ON.
I have an old programme that was written with 'option strict off' I have been steadily working through it mainly sorting out casting issues but have come up against some that I dont understand.for instance I have a checkbox called tickbox
dim Tickbox as new checkbox
later in the code
dim blah as boolean = Me.Tickbox.CheckState
but option strict insists i cast it as a boolean
dim blah as boolean = CBool(Me.Tickbox.CheckState)
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'?
I'm facing a trick issue with LINQ. I generate the above code:[code..]
Everything is running well if I turn off Option Explicit. When I turn it on, compiler is showing me a meessage: Expression is of type 'Object', which is not a collection type. Referencing to lResult variable.
If a field called X in a datarow called R is an int32 and Y is an int32 how can I make this statement work: r!x += y with option strict on. Error 8 Option Strict On prohibits operands of type Object for operator '+'.
Whenever I created a new VB.NET program I must go into project's proerties and set 'option strict' on. Can I do that once so it is a default for every time I create new project?
i'm encountering the following problem. I recently activated 'Option Strict On' and now I get an error in my LINQ query. From e As TEnum returns the following error: Option Strict On disallows implicit conversions from 'Object' to 'TEnum'. [code]
I have a DataGridview in which one column is a checkbox. I use the following code to check/uncheck the box whenever the cell is clicked.
Private Sub dgUnscheduled_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgUnscheduled.CellClick If dgUnscheduled.CurrentRow.Cells(0).Value = True Then
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim num1 As Integer = Integer.Parse(num1TextBox.Text)
i have a Problem with UNION for LINQ to SQL I have my LINQ query
Dim dc As New ContainerDB.DataClassesDataContext() Dim test = (From container In dc.GetTable(Of tbl_container)() Where container.pkContainerID = iPkContainerID).Union( _ From containerHist In dc.GetTable(Of tbl_containerHIST)() Where containerHist.pkContainerID = iPkContainerID)
I have a section that compiles basic boolean expressions at runtime, such as:CustomerBirthDate > "1/1/1940"At runtime it works perfectly. The generated code file contains "Option Explicit Off" at the top and when I call compiler services, I pass the option /optionexplicit-
I have my code working well but with OPTION STRICT OFF. I have decided to fixed all my errors in the code.However after setting the OPTION STRICT to "On", my code have lots of errors. I was able to fixed most of the errors with the except of this DIM statement where I think I have to insert CDate to (Mid$(row("TIME"), 12, 8) >= minTime) and (Mid$(row("TIME"), 12, 8) >= maxTime):
I have strongly typed code which throws a Late binding Disallowed due to Strict On exception. Is there a way to allow late binding, without turning my stict option to off?
I can run the code fine without the Option Strict on, but once it's turned on, I run in to a "Option Strict On Disallows Late Binding"...why!?! I can't figure out how to fix it....
'Option Strict On' Public Class StateFinderForm Private Sub FullStateNameRadio_CheckedChanged(ByVal sender As System.Object, ByVal e As
I just turned Option Strict On in one of my existing projects that I'm enhancing. That caused a sea of errors to showup in the project. Now I have much to fix or I can put it back to OFF. Before I started to fix all these errors I need to catch my breath. Do you guys guys work with Option Strict On?