Jason Weber Jason Weber - 5 months ago 15
Vb.net Question

How can I fix this short snippet of VB that was translated from C#?

I was given this snippet of C# that works perfectly in Visual Studio 2010:


using System;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
LinkButton senderLink = (LinkButton)sender;
Response.Redirect(Request.RawUrl.ApplyCultureToUrl(senderLink.CommandArgument));
}
}


And every online converter (and SharpDevelop) translates it to this in VB:

Imports System
Imports System.Web.UI.WebControls

Public Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub

Protected Sub RequestLanguageChange_Click(sender As Object, e As EventArgs)
Dim senderLink As LinkButton = DirectCast(sender, LinkButton)

Response.Redirect(Request.RawUrl.ApplyCultureToUrl(senderLink.CommandArgument))
End Sub
End Class


VS also is doing the same with this:

Public NotInheritable Class Localization
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Shared Function ApplyCultureToUrl(rawUrl As String, culture As String) As String
Dim modifiedUrl As String


Saying System.Runtime.CompilerServices.Extension can only be run in modules.

The Problem:

In Visual Studio 2010, Request.RawUrl.ApplyCultureToUrl is underlined in blue, and it doesn't offer a solution. It just says ApplyCultureToUrl is not a member of 'String'. This is the only thing stopping my solution from working!

I researched on MSDN, and it says that the error means it must be surrounded by a module, but not being a coder, I have no idea how to fix it. The snippet is to help my ?lang=en-GB querystring show up in my multilingual website. Any help would be sincerely appreciated!




Update: I have the ApplyCulturetoUrl thing working, just by clicking a lot of icons in Visual Studio, and it spit this out:

Partial Public Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub

Protected Sub RequestLanguageChange_Click(sender As Object, e As EventArgs)
Dim senderLink As LinkButton = DirectCast(sender, LinkButton)
Response.Redirect(ApplyCultureToUrl(Request.RawUrl, senderLink.CommandArgument))
End Sub

Private Sub body()
Throw New NotImplementedException
End Sub

Private Function ApplyCultureToUrl(p1 As String, p2 As String) As String
Throw New NotImplementedException
End Function
End Class


And that's giving me no errors, although I don't know if it's correct ... hehe .... Now, the only other part of the code that's giving me an error is in localization.vb, a class, and it's like this:

Public NotInheritable Class Localization
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Shared Function ApplyCultureToUrl(rawUrl As String, culture As String) As String
Dim modifiedUrl As String


And System.Runtime.CompilerServices.Extension() is underlined in squiggly blue and it says Extension methods can only be defined in modules. I'm using all of your suggestions! Thank you for sticking with it!

Answer

It looks like ApplyCultureToUrl is an extension method.

Solving this could be as easy as importing the namespace in which ApplyCultureToUrl is defined.

However it may be more complex than this:

In VB.Net extensions methods have to be in a module, this has to be public to reference the extension from other projects. To include them you just need to reference the containing assembly and import the right namespace.

The MSDN page for VB.Net Extension methods is here.