Option Strict On disallows late binding.

R

Ryan McLean

Hi everyone! I am trying to be good and use option strict. When I
do, the following code bombs out. A little background:

This is a portion of a class that is my template. This template is
inherited into child classes. This bit of code takes the aspx
contents of the child and adds it into a placeholder the parent page.
Please let me know if there is a better way of accomplishing this of
how I can get rid of this error.

Thanks in advance, have a great week!
Ryan McLean

Protected Sub IterateThroughChildren(ByVal obj As Object)
Dim ctrlcol As Array
Dim i As Integer

ctrlcol = Array.CreateInstance(GetType(Control),
parent.Controls.Count)

Me.Controls.CopyTo(ctrlcol, 0)
Me.Controls.Clear()
Array.Reverse(ctrlcol)

For i = ctrlcol.Length - 1 To 0 Step -1
 obj.Controls.Add(DirectCast(ctrlcol(i), Control))
Next
End Sub

The error is in the obj.Controls and ctrlcol(i)

Thanks again!
 
M

Marina

I believe DirectCast requires the type of the object being cast to be
exactly the same as the type being cast to.
This isn't the case here, as all your controls inherit from Control, but are
not actually of type Control.
Try using CType.
 
J

John Saunders

Ryan McLean said:
Hi everyone! I am trying to be good and use option strict. When I
do, the following code bombs out. A little background:

This is a portion of a class that is my template. This template is
inherited into child classes. This bit of code takes the aspx
contents of the child and adds it into a placeholder the parent page.
Please let me know if there is a better way of accomplishing this of
how I can get rid of this error.

Thanks in advance, have a great week!
Ryan McLean

Protected Sub IterateThroughChildren(ByVal obj As Object)
Dim ctrlcol As Array
Dim i As Integer

ctrlcol = Array.CreateInstance(GetType(Control),
parent.Controls.Count)

Me.Controls.CopyTo(ctrlcol, 0)
Me.Controls.Clear()
Array.Reverse(ctrlcol)

For i = ctrlcol.Length - 1 To 0 Step -1
 obj.Controls.Add(DirectCast(ctrlcol(i), Control))
Next
End Sub

The error is in the obj.Controls and ctrlcol(i)

Try:

Protected Sub IterateThroughChildren(ByVal ctl As Control)
Dim ctrlcol As Control()
Dim i As Integer

ctrlcol = New Control(Parent.Controls.Count) {}
Me.Controls.CopyTo(ctrlcol, 0)
Me.Controls.Clear()

For i = 0 To ctrlcol.Length - 1
ctl.Controls.Add(ctrlcol(i))
Next
End Sub

I'm not sure why you reversed the array and then added it in reverse. Also,
was there a reason to declare the parameter to this method to be of type
Object?
 
M

mikeb

Ryan said:
Hi everyone! I am trying to be good and use option strict. When I
do, the following code bombs out. A little background:

This is a portion of a class that is my template. This template is
inherited into child classes. This bit of code takes the aspx
contents of the child and adds it into a placeholder the parent page.
Please let me know if there is a better way of accomplishing this of
how I can get rid of this error.

Thanks in advance, have a great week!
Ryan McLean

Protected Sub IterateThroughChildren(ByVal obj As Object)
Dim ctrlcol As Array
Dim i As Integer

ctrlcol = Array.CreateInstance(GetType(Control),
parent.Controls.Count)

Me.Controls.CopyTo(ctrlcol, 0)
Me.Controls.Clear()
Array.Reverse(ctrlcol)

For i = ctrlcol.Length - 1 To 0 Step -1
 obj.Controls.Add(DirectCast(ctrlcol(i), Control))
Next
End Sub

The error is in the obj.Controls and ctrlcol(i)

Thanks again!

Strict type checking is all about declaring your variables to their
actual type (to the extent possible), so if your ctrlcol array always
contains only Control objects, then declare it as such, and you remove
the need for a DirectCast:

Dim ctrlcol( parent.Controls.Count) as Control

And if the object being passed in is always an aspx page, then declare
it as such:

Protected Sub IterateThroughChildren( ByVal page as Page)

Putting it all together:

Protected Sub IterateThroughChildren( ByVal page as Page)
Dim ctrlcol( parent.Controls.Count) as Control
Dim i As Integer

Me.Controls.CopyTo(ctrlcol, 0)
Me.Controls.Clear()
Array.Reverse(ctrlcol)

For i = ctrlcol.Length - 1 To 0 Step -1
page.Controls.Add( ctrlcol(i))
Next
End Sub
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top