Option Strict On disallows late binding.

Discussion in 'ASP .Net' started by Ryan McLean, Jun 14, 2004.

  1. Ryan McLean

    Ryan McLean Guest

    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, Jun 14, 2004
    #1
    1. Advertising

  2. Ryan McLean

    Marina Guest

    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:...
    > 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, Jun 14, 2004
    #2
    1. Advertising

  3. "Ryan McLean" <> wrote in message
    news:...
    > 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, Jun 14, 2004
    #3
  4. Ryan McLean

    mikeb Guest

    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, Jun 14, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Lang
    Replies:
    1
    Views:
    929
    Karl Lang
    Jun 1, 2004
  2. Owen Mortensen
    Replies:
    7
    Views:
    3,178
    Owen Mortensen
    Mar 13, 2006
  3. Adotek
    Replies:
    1
    Views:
    453
    Bruno Alexandre
    Oct 3, 2006
  4. Chris Davey via .NET 247

    Option Strict On disallows late binding

    Chris Davey via .NET 247, Apr 6, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    183
    Brock Allen
    Apr 6, 2005
  5. Seema
    Replies:
    1
    Views:
    875
    Arne Vajhøj
    Jan 25, 2012
Loading...

Share This Page