• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

Option Strict On disallows late binding

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
tmccrank
Asked:
tmccrank
  • 2
  • 2
  • 2
  • +1
1 Solution
 
davehunt00Commented:
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
 
Sammy AgeilCommented:
Dim strCurrentQuestion As String = e.Item.DataItem("QuestionID").ToString
should be
Dim strCurrentQuestion As String = Ctype(e.Item.DataItem("QuestionID"),String)

HTH



0
 
tmccrankAuthor Commented:
Dave,  thanks, but I would prefer to keep Strict On rules.

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

Jens
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
davehunt00Commented:
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
 
Sammy AgeilCommented:
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
 
Bob LearnedCommented:
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
 
tmccrankAuthor Commented:
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
 
Bob LearnedCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now