473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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!
Nov 18 '05 #1
3 1438
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" <Ry*********@NAU.EDU> wrote in message
news:6e**************************@posting.google.c 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!

Nov 18 '05 #2
"Ryan McLean" <Ry*********@NAU.EDU> wrote in message
news:6e**************************@posting.google.c 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
Nov 18 '05 #3
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
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Karl Lang | last post by:
Hi I've created a new configuration section in Web.Config to hold the connection string for my database. If I have Option Strict On I get a message "Option Strict On disallows late binding" when I...
12
by: Simon Harris | last post by:
Hi All, I've been advised to use option strict. I've tried to read up on this, all i can find is that it... "disallows implicit narrowing conversions" This kinda makes sense - Means I have...
11
by: Dieter Schwerdtfeger via DotNetMonster.com | last post by:
I have this function where i search through my database for items that were made on a specific date. The line "CType(dvClubs.Item(teller).Item(veld).ToShortDateString" gives me the error : option...
3
by: Starbuck | last post by:
Hi The following generates an error when Option Strict is On Can anytell tell me how to get round this please. Private Sub optWithTone_CheckedChanged(ByVal eventSender As System.Object, ByVal...
4
by: Heinz | last post by:
Hi all, I use VB.net 2003 and want to export data to Excel. Target PCs still have Office 2000 so I could not use Microsofts PIAs. Instead I use the included Excel 10 COM DLL from Microsoft....
7
by: Owen Mortensen | last post by:
(This code was working in asp.net 1.1 VS2003. After upgrade to VS2005, chokes): in the aspx file: <ItemTemplate> <asp:datagrid id="dg_instr" runat="server" AutoGenerateColumns="False"...
1
by: Adotek | last post by:
Hi All, I've just converted a solution from .Net v1.1 to v2.0, by allowing Visual Studio 2005 to do the conversion. Since doing so, I am getting a compilation error as follows: "Option...
6
by: Rob | last post by:
I have employed a "Singleton mode" of programming for this project. I have a class that exposes some properties of the class "Sample" to other forms.... If I set Option Strict On, I get many...
7
by: Lynn | last post by:
Hello, I have a website that is working fine. I have just turned on "option strict" and am getting an error with the parts of my code. I have fixed everything but this section, which has me...
0
by: Ronald S. Cook | last post by:
Hi, I'm wanting to iterate over a generic IList, but the code won't compile because the blue sqiggle line under: _DataRow("SecurityFunctionName") = _IList(i).SecurityFunctionName; sez...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.