H. Pauwelyn
H. Pauwelyn

Reputation: 14320

How works casting from an object to a String variable in VB?

I've made some code to go back to an overview page of a blog section of a site. The variables OverviewLink1 and OverviewLink2 in code below are both HtmlAnchors and would set the Href properties equal to the variable state I've made.

The property HttpContext.Current.Application gets the System.Web.HttpApplicationState object for the current HTTP request.

Dim state As HttpApplicationState = HttpContext.Current.Application(component.type & "-" & webId & "-" & langId)
OverviewLink1.HRef = state ' <-- Error happens on this line
OverviewLink2.HRef = HttpContext.Current.Application(component.type & "-" & webId & "-" & langId)

This code gives an error on the second line:

Value of type HttpApplicationState cannot be converted to String.

The third line don't give an error. Try it here in this fiddle. Now I've got some questions:

  1. How is this possible that the third line works?
  2. How casting works in VB to a variable in general?
  3. How to prevent this situation? Because I'm coming from C# and this situation isn't possible.

I'm using VB with an ASP.NET webform (the CMS is Liquifi) application.

P.S.: The variables component.type, webId and langId are variables used by the CMS Liquifi to get respectively the name of the component (type of String), the ID of the website (one website could have multiple skins) (type of Integer) and the language of the site (type of Integer)

Update:

I've tried also this code

Dim state As String = HttpContext.Current.Application(component.type & "-" & webId & "-" & langId)
OverviewLink1.HRef = state
OverviewLink2.HRef = state

and this works fine however HttpContext.Current.Application returns a HttpApplicationState and not a string but there is a cast happen. Why?

Upvotes: 0

Views: 234

Answers (2)

Ondřej
Ondřej

Reputation: 1673

The main problem is that in VB.NET, flexible autoconversion of types is turned on by default, which can bring very unexpected results and runtime errors. Especially if you came from strict language like C#.

I would recommend everyone to turn this autoshit off on every project by setting:
Project Properties -> Compile -> Option Strict On

Now it will cry every time you will try to assign something to something else which is not compatible.

Upvotes: 1

Pikoh
Pikoh

Reputation: 7703

I think the problem is you think HttpContext.Current.Application returns a HttpApplicationState. That's right if you don't give it any parameter, but if you pass a parameter it returns an Object (the item that match that key).

In C# your code's third line wouldn't compile as it does a strict type checking,but in VB.net (by default) it doesn't,so it compiles and in runtime would try to convert that Object to a String. Your example is similar to this code:

Dim s As Object = "A"
'Both of the next two lines compiles
Dim str As String = s   ' it works
Dim str2 As Integer = s ' Would fail in runtime

If you cast HttpContext.Current.Application(key) to an Object, it would also compile:

Dim state As HttpApplicationState = HttpContext.Current.Application(component.type & "-" & webId & "-" & langId)
Dim state2 As Object = HttpContext.Current.Application("blog-1-1")
Dim str3 as String= state 'Compile error, you can't convert HttpApplicationState to String
Dim str4 as String = state2 'It compiles, would give a runtime error

You would solve this problems using the Option Strict flag. That way your 3rd line wouldn't compile, as VB.NET would do strict type checking as C# does.

Upvotes: 2

Related Questions