![]() |
|
|
|||||||
![]() |
ASP Net - Option Strict On disallows late binding. |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
"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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |