Home All Groups Group Topic Archive Search About

VS2003 to VS2005 Conversion

Author
12 Jan 2006 12:23 PM
Samuel
Hi All,



I am in the process of converting a VS 2003 project to VS 2005 project (VB.NET Class Library).



It gives the error in TypeOf and DirectCast statements. It was working perfectly under VS2003.



Public Shared Sub Clear(ByVal ctl As HtmlControls.HtmlControl)

          If TypeOf ctl Is DropDownList Then

                    DirectCast(ctl, DropDownList).Items.Clear()

          End If

End Sub



1)     The error is Expression of type 'System.Web.UI.HtmlControls.HtmlControl' can never be of type 'System.Web.UI.WebControls.DropDownList'.

2)     Value of type 'System.Web.UI.HtmlControls.HtmlControl' cannot be converted to 'System.Web.UI.WebControls.DropDownList'



How I can avoid these errors, I tried changing the Option Strict option to Off. I cannot change the code because the TypeOf and DirectCast statements are used in more than 200 places.



Thanks  & Regards,

Sam

Author
12 Jan 2006 12:54 PM
Armin Zingler
Show quote Hide quote
"Samuel" <sam***@photoninfotech.com> schrieb
> Hi All,
>
>
>
> I am in the process of converting a VS 2003 project to VS 2005
> project (VB.NET Class Library).
>
>
>
> It gives the error in TypeOf and DirectCast statements. It was
> working perfectly under VS2003.
>
>
>
> Public Shared Sub Clear(ByVal ctl As HtmlControls.HtmlControl)
>
>           If TypeOf ctl Is DropDownList Then
>
>                     DirectCast(ctl, DropDownList).Items.Clear()
>
>           End If
>
> End Sub
>
>
>
> 1)     The error is Expression of type
> 'System.Web.UI.HtmlControls.HtmlControl' can never be of type
> 'System.Web.UI.WebControls.DropDownList'.
>
> 2)     Value of type 'System.Web.UI.HtmlControls.HtmlControl' cannot
> be converted to 'System.Web.UI.WebControls.DropDownList'
>
>
>
> How I can avoid these errors, I tried changing the Option Strict
> option to Off. I cannot change the code because the TypeOf and
> DirectCast statements are used in more than 200 places.

DropDownList is not derived from HtmlControl, thus you can not cast this
way. Why do you think you can? A HtmlControl does not have an Items
property. The classes derived from HtmlControl are

System.Web.UI.HtmlControls.HtmlContainerControl
System.Web.UI.HtmlControls.HtmlImage
System.Web.UI.HtmlControls.HtmlInputControl

ctl can point to one of these objects, but /never/ to a DropDownList object.



Armin
Author
12 Jan 2006 1:10 PM
Samuel
But it was working in VS2003. Is it possible to make the code to work in
VS2005 ?

Show quoteHide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:%23Q7a9e3FGHA.2064@TK2MSFTNGP09.phx.gbl...
> "Samuel" <sam***@photoninfotech.com> schrieb
>> Hi All,
>>
>>
>>
>> I am in the process of converting a VS 2003 project to VS 2005
>> project (VB.NET Class Library).
>>
>>
>>
>> It gives the error in TypeOf and DirectCast statements. It was
>> working perfectly under VS2003.
>>
>>
>>
>> Public Shared Sub Clear(ByVal ctl As HtmlControls.HtmlControl)
>>
>>           If TypeOf ctl Is DropDownList Then
>>
>>                     DirectCast(ctl, DropDownList).Items.Clear()
>>
>>           End If
>>
>> End Sub
>>
>>
>>
>> 1)     The error is Expression of type
>> 'System.Web.UI.HtmlControls.HtmlControl' can never be of type
>> 'System.Web.UI.WebControls.DropDownList'.
>>
>> 2)     Value of type 'System.Web.UI.HtmlControls.HtmlControl' cannot
>> be converted to 'System.Web.UI.WebControls.DropDownList'
>>
>>
>>
>> How I can avoid these errors, I tried changing the Option Strict
>> option to Off. I cannot change the code because the TypeOf and
>> DirectCast statements are used in more than 200 places.
>
> DropDownList is not derived from HtmlControl, thus you can not cast this
> way. Why do you think you can? A HtmlControl does not have an Items
> property. The classes derived from HtmlControl are
>
> System.Web.UI.HtmlControls.HtmlContainerControl
> System.Web.UI.HtmlControls.HtmlImage
> System.Web.UI.HtmlControls.HtmlInputControl
>
> ctl can point to one of these objects, but /never/ to a DropDownList
> object.
>
>
>
> Armin
Author
12 Jan 2006 1:18 PM
Herfried K. Wagner [MVP]
"Samuel" <sam***@photoninfotech.com> schrieb:
> But it was working in VS2003. Is it possible to make the code to work in
> VS2005 ?

I doubt that the code works in VS.NET 2003.  The inheritance hierarchy
didn't change.  Maybe you forgot to turn on 'Option Strict' in VS.NET 2003?

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
13 Jan 2006 5:03 AM
Samuel
Hi,

Attached herewith the 2003 project files and 2005 project files. Now I think
you will agree that the code will compile with 2003 and not with 2005.

Sam
Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:Ok1fgq3FGHA.3856@TK2MSFTNGP12.phx.gbl...
> "Samuel" <sam***@photoninfotech.com> schrieb:
>> But it was working in VS2003. Is it possible to make the code to work in
>> VS2005 ?
>
> I doubt that the code works in VS.NET 2003.  The inheritance hierarchy
> didn't change.  Maybe you forgot to turn on 'Option Strict' in VS.NET
> 2003?
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
>

[attached file: 2005ClassLibrary2.zip]
[attached file: 2003ClassLibrary2.zip]
Author
13 Jan 2006 12:06 PM
Herfried K. Wagner [MVP]
"Samuel" <sam***@photoninfotech.com> schrieb:
> Attached herewith the 2003 project files and 2005 project files. Now I
> think
> you will agree that the code will compile with 2003 and not with 2005.

I've never said the code won't compile in VS.NET 2003.  I only said it won't
work as expected.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
12 Jan 2006 2:33 PM
Armin Zingler
"Samuel" <sam***@photoninfotech.com> schrieb
> But it was working in VS2003. Is it possible to make the code to
> work in VS2005 ?


Have you tried it in VS2003? I'm pretty sure it does not work.


Armin
Author
12 Jan 2006 5:00 PM
Cor Ligthert [MVP]
Armin,

>
> Have you tried it in VS2003? I'm pretty sure it does not work.
>
Did you try it, I tried it there was no error given.

web.ui.htmlcontrols derive from web.ui.controls

And than I stopped because there is a lot possible as well with html control
however AFAIK not so well documented. Therefore I am currious if you have
checked it really deep..
..
Cor
Author
12 Jan 2006 7:20 PM
Armin Zingler
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb
> >
> > Have you tried it in VS2003? I'm pretty sure it does not work.
> >
> Did you try it,

No. To check this, looking at the object browser should be sufficient. :-) I
was not able to test it because I failed in assigning a DropDownList to a
variable declared as HtmlControl.

> I tried it there was no error given.

> web.ui.htmlcontrols derive from web.ui.controls

Right. Inheritance hierarchiy:

System.Web.UI.HtmlControls.HtmlControl
    System.Web.UI.Control


System.Web.UI.WebControls.DropDownList
    System.Web.UI.WebControls.ListControl
        System.Web.UI.WebControls.WebControl
            System.Web.UI.Control


As you see, you can not cast HtmlControls.HtmlControl to DropDownList
because DropDownList is not derived from HtmControl.


> And than I stopped because there is a lot possible as well with html
> control however AFAIK not so well documented. Therefore I am
> currious if you have checked it really deep..

Yes, I checked the object browser really deep.


Armin
Author
12 Jan 2006 7:28 PM
Armin Zingler
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb
> Did you try it, I tried it there was no error given.

I forgot: How did you try it?



Armin
Author
13 Jan 2006 7:03 AM
Cor Ligthert [MVP]
I just dragged the coded in a webproject (2003) and it did give no error.

Than I pushed F1 (not serious I have searched for it on MSDJ) and saw that
they both derive from ui.web.control however "items" is not a direct member
from that and that did confuse me. However to test this I would have to do
as I wrote and that confuses me every time even more..

Cor
Author
13 Jan 2006 11:01 AM
Armin Zingler
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb
> I just dragged the coded in a webproject (2003) and it did give no
> error.
>
> Than I pushed F1 (not serious I have searched for it on MSDJ) and
> saw that they both derive from ui.web.control however "items" is not
> a direct member from that and that did confuse me. However to test
> this I would have to do as I wrote and that confuses me every time
> even more..


What's your conclusion now?


Armin
Author
13 Jan 2006 12:03 PM
Cor Ligthert [MVP]
>
> What's your conclusion now?
>
As written by you and Herfried, what can I conclude more.

Cor
Author
12 Jan 2006 11:02 PM
Herfried K. Wagner [MVP]
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
>> Have you tried it in VS2003? I'm pretty sure it does not work.
>
> Did you try it, I tried it there was no error given.

I didn't try it, but I am sure the problem is that the warning is simply
missing in VS.NET 2003 and thus the code compiles but doesn't work as
expected because the 'TypeOf...Is' condition is never met.  VB 7.* lets you
write and compile

\\\
Dim f As Form
If TypeOf f Is String Then
    MsgBox("Bla")
End If
///

although the condition will never be true because a variable of type 'Form'
cannot reference a 'String' object.

> web.ui.htmlcontrols derive from web.ui.controls

Both are namespaces, so there is no "is-a" relation between them at all.  As
said in other messages, 'HtmlControl' is not derived from 'WebControl' and
vice versa.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
13 Jan 2006 4:34 PM
Jay B. Harlow [MVP - Outlook]
Samuel,
PMFJI.

| But it was working in VS2003.
Compiling yes, working (aka executing) we all doubt it!

| Is it possible to make the code to work in
| VS2005 ?
No, for the reasons stated.

Did the Items.Clear method in VS2003 ever get called for a DropDownList?
Have you walked through your code in VS 2003 & verified the code (inside the
If) actually executes?

I suspect in VS2003 you effectively had:

Public Shared Sub Clear(ByVal ctl As HtmlControls.HtmlControl)

End Sub

i.e. Although the code compiles in VS 2003, the statement never executed as
ctl could never be a DropDownList!

VS 2005 is much better at catching coding errors such as this, that may
appear correct when you type them, however the code won't actually execute.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Samuel" <sam***@photoninfotech.com> wrote in message
news:e6VTxm3FGHA.3200@tk2msftngp13.phx.gbl...
| But it was working in VS2003. Is it possible to make the code to work in
| VS2005 ?
|
| "Armin Zingler" <az.nospam@freenet.de> wrote in message
| news:%23Q7a9e3FGHA.2064@TK2MSFTNGP09.phx.gbl...
| > "Samuel" <sam***@photoninfotech.com> schrieb
| >> Hi All,
| >>
| >>
| >>
| >> I am in the process of converting a VS 2003 project to VS 2005
| >> project (VB.NET Class Library).
| >>
| >>
| >>
| >> It gives the error in TypeOf and DirectCast statements. It was
| >> working perfectly under VS2003.
| >>
| >>
| >>
| >> Public Shared Sub Clear(ByVal ctl As HtmlControls.HtmlControl)
| >>
| >>           If TypeOf ctl Is DropDownList Then
| >>
| >>                     DirectCast(ctl, DropDownList).Items.Clear()
| >>
| >>           End If
| >>
| >> End Sub
| >>
| >>
| >>
| >> 1)     The error is Expression of type
| >> 'System.Web.UI.HtmlControls.HtmlControl' can never be of type
| >> 'System.Web.UI.WebControls.DropDownList'.
| >>
| >> 2)     Value of type 'System.Web.UI.HtmlControls.HtmlControl' cannot
| >> be converted to 'System.Web.UI.WebControls.DropDownList'
| >>
| >>
| >>
| >> How I can avoid these errors, I tried changing the Option Strict
| >> option to Off. I cannot change the code because the TypeOf and
| >> DirectCast statements are used in more than 200 places.
| >
| > DropDownList is not derived from HtmlControl, thus you can not cast this
| > way. Why do you think you can? A HtmlControl does not have an Items
| > property. The classes derived from HtmlControl are
| >
| > System.Web.UI.HtmlControls.HtmlContainerControl
| > System.Web.UI.HtmlControls.HtmlImage
| > System.Web.UI.HtmlControls.HtmlInputControl
| >
| > ctl can point to one of these objects, but /never/ to a DropDownList
| > object.
| >
| >
| >
| > Armin
|
|
Author
12 Jan 2006 12:55 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Samuel" <sam***@photoninfotech.com> schrieb:
>I am in the process of converting a VS 2003 project to VS 2005 project
>(VB.NET Class Library).
>
>It gives the error in TypeOf and DirectCast statements. It was working
>perfectly under VS2003.
>
>Public Shared Sub Clear(ByVal ctl As HtmlControls.HtmlControl)
>          If TypeOf ctl Is DropDownList Then
>                    DirectCast(ctl, DropDownList).Items.Clear()
>          End If
>End Sub
>
>1)     The error is Expression of type
>'System.Web.UI.HtmlControls.HtmlControl'
> can never be of type 'System.Web.UI.WebControls.DropDownList'.

Which is true.  'DropDownList' does not inherit from 'HtmlControl', not even
in .NET 1.1.  You may want to use 'WebControls.WebControl' instead of
'HtmlControls.HtmlControl'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
12 Jan 2006 1:17 PM
Kerry Moorman
Herfried,

I'm interested in how you think this could have been working perfecting for
the OP under VS2003.

Kerry Moorman


Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "Samuel" <sam***@photoninfotech.com> schrieb:
> >I am in the process of converting a VS 2003 project to VS 2005 project
> >(VB.NET Class Library).
> >
> >It gives the error in TypeOf and DirectCast statements. It was working
> >perfectly under VS2003.
> >
> >Public Shared Sub Clear(ByVal ctl As HtmlControls.HtmlControl)
> >          If TypeOf ctl Is DropDownList Then
> >                    DirectCast(ctl, DropDownList).Items.Clear()
> >          End If
> >End Sub
> >
> >1)     The error is Expression of type
> >'System.Web.UI.HtmlControls.HtmlControl'
> > can never be of type 'System.Web.UI.WebControls.DropDownList'.
>
> Which is true.  'DropDownList' does not inherit from 'HtmlControl', not even
> in .NET 1.1.  You may want to use 'WebControls.WebControl' instead of
> 'HtmlControls.HtmlControl'.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
>
Author
12 Jan 2006 1:19 PM
Herfried K. Wagner [MVP]
"Kerry Moorman" <KerryMoor***@discussions.microsoft.com> schrieb:
> I'm interested in how you think this could have been working perfecting
> for
> the OP under VS2003.

That's what I am wondering about too.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
2 May 2006 10:28 AM
Binoy Benedict
CCSBBT
CCS***@bbt.org

*** Sent via Developersdex http://www.developersdex.com ***