Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Option Strict On disallows late binding.

Reply
Thread Tools

Option Strict On disallows late binding.

 
 
Ryan McLean
Guest
Posts: n/a
 
      06-14-2004
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!
 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      06-14-2004
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.

"Ryan McLean" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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!



 
Reply With Quote
 
 
 
 
John Saunders
Guest
Posts: n/a
 
      06-14-2004
"Ryan McLean" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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?
--
John Saunders
johnwsaundersiii at hotmail


 
Reply With Quote
 
mikeb
Guest
Posts: n/a
 
      06-14-2004
Ryan McLean wrote:

> 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

--
mikeb
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Option Strict On disallows late binding Seema ASP .Net 1 01-25-2012 02:19 AM
Error: Option strict disallows late binding Adotek ASP .Net 1 10-03-2006 05:50 PM
Datagrid: Option Strict On disallows late binding -- HELP Owen Mortensen ASP .Net 7 03-13-2006 05:20 PM
Option Strict On disallows late binding Chris Davey via .NET 247 ASP .Net Datagrid Control 1 04-06-2005 05:29 PM
option strict disallows late binding reading from web.config Karl Lang ASP .Net 1 06-01-2004 10:56 AM



Advertisments