Solved

Option Strict On disallows late binding

Posted on 2006-07-14
8
274 Views
Last Modified: 2011-09-20
Hi,

I'm getting the above error on the following line of code:

Dim strCurrentQuestion As String = e.Item.DataItem("QuestionID").ToString

How can I get around the error? (other than going to OptStr Off of course)

Thanks
0
Comment
Question by:tmccrank
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 4

Expert Comment

by:davehunt00
ID: 17111786
You can turn off Strict On rules. You lose some notification, but might be best for what you're doing.

http://msdn2.microsoft.com/en-us/library/efwbatax.aspx

Dave
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17111903
Dim strCurrentQuestion As String = e.Item.DataItem("QuestionID").ToString
should be
Dim strCurrentQuestion As String = Ctype(e.Item.DataItem("QuestionID"),String)

HTH



0
 

Author Comment

by:tmccrank
ID: 17111969
Dave,  thanks, but I would prefer to keep Strict On rules.

Sammy, thanks, but I'm still getting the same error.

Jens
0
 
LVL 4

Expert Comment

by:davehunt00
ID: 17112057
Lots of better experts than I, but I think you can't avoid the error with this compiler setting wtih the "e.Item.DataItem("QuestionID").ToString".  Because this is a runtime binding (vs one the compiler controls) the warning is basically telling you that it can't be sure you'll stuff the right data in and that it will implicitly convert. It's not saying it can't work, just that it might not.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 27

Expert Comment

by:Sammy
ID: 17112343
try it in the databound sub
 Protected Sub MyGird_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
Dim strCurrentQuestion As String = CType(e.Item.DataItem("QuestionID"),String)
    End Sub
or you can try
Dim strCurrentQuestion As String = DataBinder.Eval(e.Item.DataItem, "QuestionID")


Good luck
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 17115769
The best way to handle this type of exception is to break down the constituent parts and find out what the types are.

CType(CType(e.Item, DataGridItem).DataItem, DataRowView)("QuestionID").ToString()

You can see where Option Strict On can cause a lot more work.  I also made some assumptions about DataRowView, so you need to get the type with something like e.Item.DataItem.GetType().Name.

Bob
0
 

Author Comment

by:tmccrank
ID: 17130729
Thanks Bob,

I'm new at  .NET, could you tell me how to use e.Item.DataItem.GetType().Name to get the type that should replace DataRowView?

I just did this:  Dim strType As String = e.Item.DataItem.GetType().Name

And tried to use the variable strType:
Dim strCurrentQuestion As String = CType(CType(e.Item, RepeaterItem).DataItem, strType("QuestionID").ToString()

I'm getting design-time error with QuestionID: Array bounds cannot appear in type specifiers.

I'm likely way off in how I'm using the strType variable, your help is appreciated.
Jens
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17130926
That was only a temporary test to see what the type is, not to be used in the end.

Try this:
  Dim strCurrentQuestion As String = CType(CType(e.Item, RepeaterItem).DataItem)("QuestionID").ToString()

You can quickly see how cumbersome Option Strict can be.

Bob


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now