?
Solved

Option Strict On disallows late binding

Posted on 2006-07-14
8
Medium Priority
?
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Ageil
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 27

Expert Comment

by:Sammy Ageil
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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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