Solved

Option Strict On disallows late binding

Posted on 2006-07-14
8
300 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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 …

856 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