option strict

  • Thread starter Thread starter mp
  • Start date Start date
M

mp

more beginner questions
Is option strict recommended practice in general with vbnet?
if i turn it on i get hundreds of errors
for example the first one
(this line is fine without option strict)
m_AcadApp = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication

it occurs in this function in a class:

Function Init2(ByVal bDebug As Boolean) As Boolean
m_Debug = bDebug
Try
m_AcadApp =
Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication
m_acadDoc = m_AcadApp.ActiveDocument
Return True
Catch ex As Exception
Return False
End Try
End Function

and the variable is defined thus at the top of the class

Private m_AcadApp As AcadApplication = Nothing

and i have this imports line above that

Imports Autodesk.AutoCAD.ApplicationServices.Application

but with option strict on i get the following error and don't know how to
more explicittly define the variable.

error: Option Strict On disallows implicit conversions from 'Object' to
'Autodesk.AutoCAD.Interop.AcadApplication'.

I'm not declaring it as object(late binding) but that's what the compiler
says i'm doing

how to handle this with option strict or is option strict not necessary?

thanks

mark
 
Option Strict Off is only a good when you are a real beginner coming from
VB6

One reason, you have the chance that your programs run 10 times slower with
VB6 off.
(But also that errors are detected only at runtime)

Be aware Reflection uses the same methods as are in Option Strict Off.
 
Am 11.07.2010 03:19, schrieb mp:
error: Option Strict On disallows implicit conversions from 'Object' to
'Autodesk.AutoCAD.Interop.AcadApplication'.

I'm not declaring it as object(late binding) but that's what the compiler
says i'm doing

how to handle this with option strict or is option strict not necessary?

The last piece in

Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication

is probably of type Object (hover the mouse over it to find out).

Cast the whole expression to the type of the variable before assignment.
Do this only if you're sure that the AcadApplication property is really
of that type! Are you sure it is? The property type seems to be Object
and the returned object could be of any type. That's why Option Strict On
makes you think about it.
 
Sorry,
I see now that I misunderstood your question,
Can you try this one
m_AcadApp =
DirectCast(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication,AcadApplication)


Seems that the property and the type have the same name.

Got this from Internet looking at C# where it cannot be done without the
cast.

Cor
 
Thanks that worked!
I'm now working through the hundreds of implicit casts and converting to
direct...

how do I handle this one?
this used to work before i turned option strict on
Dim Length as Double

KERF_OFFSET is a const double

Dim NumberKerfs As Integer = Fix(Length / KERF_OFFSET)

now i get the error:

Option Strict On disallows implicit conversions from 'Double' to 'Integer'.

so i tried this

Dim NumberKerfs As Integer = DirectCast(Fix(Length / KERF_OFFSET), Int16)

now i get the error

Value of type 'Double' cannot be converted to 'Short'.



I don't get it because the help on Fix says it takes a double as arg

Number Required. A number of type Double or any valid numeric expression
and it returns an integer

my variable is declared integer and set to return value of Fix so where's
the cast???



Thanks again for your help

Mark
 
OMG!
I just discovered the intellisense red dot with Exclamation point and the
suggested fix for lines that raise syntax errors (apply fix)- that's too
cool!!!
so the answer to the below question has been supplied
Thanks
mark
 
Back
Top