Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Option Strict On disallows late binding.

 
Thread Tools Search this Thread
Old 06-14-2004, 06:22 PM   #1
Default Option Strict On disallows late binding.


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!


Ryan McLean
  Reply With Quote
Old 06-14-2004, 06:31 PM   #2
Marina
 
Posts: n/a
Default Re: Option Strict On disallows late binding.
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" <> wrote in message
news: 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!





Marina
  Reply With Quote
Old 06-14-2004, 06:40 PM   #3
John Saunders
 
Posts: n/a
Default Re: Option Strict On disallows late binding.
"Ryan McLean" <> wrote in message
news: 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




John Saunders
  Reply With Quote
Old 06-14-2004, 06:49 PM   #4
mikeb
 
Posts: n/a
Default Re: Option Strict On disallows late binding.
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


mikeb
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Blockbuster's Latest Concession. One-Shot Scot DVD Video 128 01-24-2005 04:08 AM
Wal-mart's DVD Rental Service - a customer review FYI r7di697 DVD Video 28 11-05-2003 01:00 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46