option strict on

  • Thread starter Thread starter Daylor
  • Start date Start date
D

Daylor

hi.
im using option strict on.
im doing in ,from the simple reason ,to be warn when there are implict
conversion like string to int ,int to string.

BUT.
the price ,(now i see ), is very bad.
if i have class CCar and interface ICar
when im doing this :
ICar = new CCar
i get compiler error,because the option strict on.


i thought ,the compiler can "see", the ccar is implementing ICar, just for
this reason to do : ICar = new CCar.


the question :
is there a middle way (im not talking about explicit converstion of class to
interface ) ,so the option strict will be ON, but implict coversion between
class and interface will not generate compile error ?
 
Daylor said:
hi.
im using option strict on.
im doing in ,from the simple reason ,to be warn when there are
implict conversion like string to int ,int to string.

BUT.
the price ,(now i see ), is very bad.
if i have class CCar and interface ICar
when im doing this :
ICar = new CCar
i get compiler error,because the option strict on.


i thought ,the compiler can "see", the ccar is implementing ICar,
just for this reason to do : ICar = new CCar.


the question :
is there a middle way (im not talking about explicit converstion of
class to interface ) ,so the option strict will be ON, but implict
coversion between class and interface will not generate compile error
?


I don't see the problem. The following code works:

Public Interface icar
Sub test()
End Interface

Public Class ccar
Implements icar

Public Sub test() Implements icar.test
End Sub
End Class


'...
Dim o As icar
o = New ccar 'no problem with option strict on
 
* "Daylor said:
im using option strict on.
im doing in ,from the simple reason ,to be warn when there are implict
conversion like string to int ,int to string.

BUT.
the price ,(now i see ), is very bad.
if i have class CCar and interface ICar
when im doing this :
ICar = new CCar
i get compiler error,because the option strict on.


i thought ,the compiler can "see", the ccar is implementing ICar, just for
this reason to do : ICar = new CCar.

I am not able to repro that in VB.NET 2003.
 
Daylor,
Can you post your code for CCar & ICar or preferable a small sample such as
Armin's

As Armin demonstrated & Herfried stated, This functions as you expect in
both VB.NET 2002 & VB.NET 2003!

When CCar Implements ICar:

If I have:
Dim ic As ICar
Dim car As CCar

The following will work with Option Strict On
ic = car

The following may not succeed as other classes may implement ICar
car = ic

So you need to check the type of the object & DirectCast to succeed.
If TypeOf ic Is CCar Then
car = DirectCast(ic, CCar)
End If

Hope this helps
Jay
 
hi again.
im sure of what im asking here.

i try it again, and compare what the first sender post.

ill explain with more details :
the classes names,is just to be more clear :

i have the ICar interface in MyInterfaces.dll assemly. (vb.net)
i have the CCar class the implements ICar in the MyApp.dll assembly (vb.net)
and i have Garage class in the MyGarage.dll assembly (Managed c++ )

now , i have TestApp.exe (vb.net ) that create this CCar :

private m_ccar as CCar

public sub MySub ()
m_ccar = new CCar
m_garage.SendToRepair (m_ccar) // m_garage expect ICar !! ,and here i
get an error, the option strict is on ,and not allow this converstion.

end sub

now, one more thing,i just want to say,is that the ICar is in namespace,and
CCar is not.just saying this for the info.
so the error i get is like this :
"Option Strict On disallows implicit conversions from CCar to
MyInterfaces.ICar"

================================================
hope u can help me with that, cause from the start i didnt belive that
option strict on , will not let me send type to paramater that is interface
he is implement
================================================
 
HI AGAIN.
one more thing, when im doing it in the same assembly.
i dont have an error.


meaning , when sending a CCar class ,to sub the recive ICar ,in the assembly
that have the CCar class.
(the ICar is in other assembly ) , its works fine. no errors.


for some reason, if i do it , like i want to do,(describe way i have the
solution in the last post ) , i recive an error.

----------------------------------------------------
now, i thought maybe i have problem with referencing the same interface.dll
assembly.
i try to point from each project to the interface.dll , in 1 project i get
an error, that the project can't copy the dll,
cause other process use it.
 
Daylor said:
hi again.
im sure of what im asking here.

i try it again, and compare what the first sender post.

ill explain with more details :
the classes names,is just to be more clear :

i have the ICar interface in MyInterfaces.dll assemly. (vb.net)
i have the CCar class the implements ICar in the MyApp.dll assembly (vb.net)
and i have Garage class in the MyGarage.dll assembly (Managed c++ )

now , i have TestApp.exe (vb.net ) that create this CCar :

private m_ccar as CCar

public sub MySub ()
m_ccar = new CCar
m_garage.SendToRepair (m_ccar) // m_garage expect ICar !! ,and here i
get an error, the option strict is on ,and not allow this converstion.

end sub

now, one more thing,i just want to say,is that the ICar is in namespace,and
CCar is not.just saying this for the info.
so the error i get is like this :
"Option Strict On disallows implicit conversions from CCar to
MyInterfaces.ICar"

================================================
hope u can help me with that, cause from the start i didnt belive that
option strict on , will not let me send type to paramater that is interface
he is implement
================================================


I do understand you but I can not reproduce the problem. The following code
works:


Public Interface ICar
Sub test()
End Interface

Public Class CCar
Implements ICar

Public Sub test() Implements ICar.test
End Sub
End Class

Public Class Garage
Public Sub SendToRepair(ByVal Car As ICar)

End Sub
End Class
'...

Dim o As ICar
Dim g As New Garage

o = New CCar
g.SendToRepair(o)



Maybe you define the interface ICar or the CClas twice, i.e. in different
assemblies?
 
Ok, now its ok, no more error , after i did that :
remove the Interface.Dll project from the solution.

==========================================================
 
Now i got it :)
i return the interface.dll project to the solution.
go to the config manager,and remove the build checkbox!!!!!!!! from the
interface project. .

:)
this is REAL SOLUTION.

the compiler errors are not so good :)

=============================================================
 
Daylor,
Does the MyGarage.dll, MyApp.dll or TestApp.exe define its own ICar
interface or does it reference the MyInterfaces.dll and get ICar from there?
(From what you said it is suppose to get it from MyInterfaces.dll, sometimes
developers cut & past the interface to their project by mistake.
[MyGarage.dll]MyInterfaces.ICar is different then
[MyApp.dll]MyInterfaces.ICar.

Does TestApp.exe reference all three dlls? Does MyGarage.dll & MyApp.dll
reference MyInterfaces.dll?

Are you certain that the assemblies are being built in the proper order?
1. MyInterfaces.dll
2. MyApp.dll
3. MyGarage.dll
4. TestApp.exe

Note 2 & 3 can be reversed, however MyInterfaces.dll must be first & TestApp
must be last.

Are you certain that you are referencing the correct version of the
assemblies? In other words you do not have an old copy of an assembly that
you are referencing.

Is this four solutions each with a project, or one solution with four
projects?

If this is one solution are you referencing the project or the dll in the
other projects?

Is this VS.NET 2002 or VS.NET 2003?
hope u can help me with that, cause from the start i didnt belive that
option strict on , will not let me send type to paramater that is interface
he is implement
If you use Option Strict Off, will it run? I would expect a runtime error on
that line as it appears that the ICar in TextApp is different then the ICar
in MyGarage.dll, hence the compile error with Option Strict On. In other
words the error is because you have a second definition of ICar someplace
which is not the ICar that CCar is using!

Hope this helps
Jay
 
Daylor,
It sounds like you are simply avoiding the problem with out finding the
"solution", which my series of questions were intended to help find a
solution for you.

I normally reference the Project and not the DLL when I have multiple
projects in a solution, this will cause VS.NET to build things in the proper
order.

Alternatively (in addition to?) you can use Project - Dependencies to set
which projects in your solution are dependent on which other projects. I
find referencing the project in a solution sets the dependencies correctly.

I've seen others mention that a dll in use, however I have not experienced
it personally. Most of the time I have heard about it, it has been projects
referencing the DLL itself as opposed to the project. Try referencing the
project to see if that makes a difference.

Hope this helps
Jay
 
It sounds, as someone suggested, that you have the interface iCar defined
in both the MyInterfaces Dll and the MyGarage Dll - you should have only
one definition, probably in MyInterfaces.dll and reference that project
from both the MyGarage.Dll and the MyApp.Dll
if you have two definitions, they will act as different types, even though
they have the same name - not referencing the MyInterfaces.Dll or setting
the project to not build works but isn't a solution - it just means that
the MyInterfaces.Dll isn't being used at all.
 
Back
Top