C# to VB help required

J

Jon Paal

Trying to convert this to VB and need some help. Standard converters not accepting the code.
major problem is on the last line of code text
thanks for any help -

------------ C# code ------------------
public Global()
{
this.PreRequestHandlerExecute += new EventHandler(Application_PreRequestHandlerExecute);
}

private void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
HttpContext CurrentContext = HttpContext.Current;
Page myPage = CurrentContext.Handler as Page;
if (myPage != null)
{
myPage.Theme = (CurrentContext.Profile as ProfileCommon).SiteTheme;
}
}
 
K

Karl Seguin

The last line isn't particularly well written, which is why I'd expect
converts to throw up some errors.

the corrent vb.net line would be:

myPage.Theme = ctype(CurrentContext.Profile, ProfileCommon).SiteTheme


I say it's badly programmed because "as" tries and casts a value to a type,
and returns null if it fails. It's used as a safe-conversion. However,
since you aren't checking for null and are simply accessing a property, you
might as well use a direct cast.

myPage.Theme = ((ProfileCommon)CurrentContext.Profile).SiteTheme

or check for null:

ProfileCommon profile = CurrentContext.Profile as ProfileCommon;
if (profile != null)
{
myPage.Theme = profile.SiteTheme;
}
else
{
//default theme perhaps?? what to do?
}


karl
 
G

grey_christ

You mean that myPage.Theme = (CurrentContext.Profile as
ProfileCommon).SiteTheme; line?

VB.Net version would be:
myPage.Theme = CType(CurrentContext.Profile, ProfileCommon).SiteTheme;
 
J

Jon Paal

this is my resultant VB code (located in my Global.asax file), but compiler is throwing this error:

Compiler Error Message: BC30269: 'Public Sub New()' has multiple definitions with identical signatures.

'Public Sub New()' only occurs once in the Global.asax but does get used in the App_Code files.

Is there a way to have 'Public Sub New()' also in the Global .asax ?
or is there another way to create an instance of the RequisiteHandler ?



------- VB code in Global.asax-------------
Public Sub New()
AddHandler Me.PreRequestHandlerExecute, AddressOf Application_PreRequestHandlerExecute
End Sub

Private Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
Dim CurrentContext As HttpContext = HttpContext.Current
Dim myPage As Page = CurrentContext.Handler
If Not myPage Is Nothing Then
myPage.Theme = myPage.Theme = CType(CurrentContext.Profile, ProfileCommon).SiteTheme
End If
End Sub
 
K

Karl Seguin

Not sure why it would complain inVB.net and not in C#. But I can tell you
that you shouldn't be hooking these events..they are already hooked up by
the framework.

Ty removing the constructor and changing the method from Private to
Protected.

Karl
 
J

Jon Paal

thanks for helping.
I'll have to try and find a working solution for dynamic themes, because I'm getting in over my head with this one.
 
G

Guest

Ahem... (only some converters have a problem with this code)
The following was produced with our Instant VB C# to VB converter:

Public Sub New()
AddHandler PreRequestHandlerExecute, AddressOf
Application_PreRequestHandlerExecute
End Sub

Private Sub Application_PreRequestHandlerExecute(ByVal sender As Object,
ByVal e As EventArgs)
Dim CurrentContext As HttpContext = HttpContext.Current
Dim myPage As Page = TryCast(CurrentContext.Handler, Page)
If Not myPage Is Nothing Then
myPage.Theme = (TryCast(CurrentContext.Profile, ProfileCommon)).SiteTheme
End If
End Sub

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top