VS2003 to VS2005 Conversion

S

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
 
A

Armin Zingler

Samuel said:
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
 
H

Herfried K. Wagner [MVP]

Samuel said:
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'.
 
G

Guest

Herfried,

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

Kerry Moorman
 
H

Herfried K. Wagner [MVP]

Samuel said:
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?
 
H

Herfried K. Wagner [MVP]

Kerry Moorman said:
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.
 
A

Armin Zingler

Samuel said:
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
 
C

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
 
A

Armin Zingler

Cor Ligthert said:
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
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
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.
 
C

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
 
A

Armin Zingler

Cor Ligthert said:
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
 
H

Herfried K. Wagner [MVP]

Samuel said:
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.
 
J

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


| But it was working in VS2003. Is it possible to make the code to work in
| VS2005 ?
|
| | >> 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
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top