this C# code in VB?


G

G Dean Blake

I have this code snippet in C# that I have to convert to VB

object o = null;
if (mode == PageTrackingMode.ByApplication) {
o = Page.Application[HitsKey];
}
else if (mode == PageTrackingMode.BySession) {
o = Page.Session[HitsKey];
}

In most of the C# code I see that is referring to Session and Application
variables I see that quotes are used i.e. Page.Session["HitsKey"] which
would translate to Page.Session("HitsKey") in VB. But the code above is
using the brackets without the quotes. What does that mean and how would
that translate to VB?
Thanks,
G
 
Ad

Advertisements

G

Greg Burns

G said:
I have this code snippet in C# that I have to convert to VB

object o = null;
if (mode == PageTrackingMode.ByApplication) {
o = Page.Application[HitsKey];
}
else if (mode == PageTrackingMode.BySession) {
o = Page.Session[HitsKey];
}

In most of the C# code I see that is referring to Session and Application
variables I see that quotes are used i.e. Page.Session["HitsKey"] which
would translate to Page.Session("HitsKey") in VB. But the code above is
using the brackets without the quotes. What does that mean and how would
that translate to VB?
Thanks,
G
Woudln't that simply mean that HitsKey is a string variable or constant
defined someplace else?

Greg
 
R

Russell Jones

Yes, HitsKey must be defined elsewhere. Here's the conversion.

Dim o As Object = Nothing
If mode = PageTrackingMode.ByApplication Then
o = Page.Application(HitsKey)
ElseIf mode = PageTrackingMode.BySession Then
o = Page.Session(HitsKey)
End If

Greg Burns said:
G said:
I have this code snippet in C# that I have to convert to VB

object o = null;
if (mode == PageTrackingMode.ByApplication) {
o = Page.Application[HitsKey];
}
else if (mode == PageTrackingMode.BySession) {
o = Page.Session[HitsKey];
}

In most of the C# code I see that is referring to Session and Application
variables I see that quotes are used i.e. Page.Session["HitsKey"] which
would translate to Page.Session("HitsKey") in VB. But the code above is
using the brackets without the quotes. What does that mean and how would
that translate to VB?
Thanks,
G
Woudln't that simply mean that HitsKey is a string variable or constant
defined someplace else?

Greg
 
G

G Dean Blake

This code is actually inside a servercontrol where there is a property named
HitsKey but also there is code using a session and application variable
named "HitsKey". What I don't understand is why if they simply want to
refer to the property why not just say HitsKey instead of
Page.Session[HitsKey] (I don't know why they chose to name a session
variable and a property by the same name.) It's actually code from a book
on writing servercontrols.
Thanks,
G

Russell Jones said:
Yes, HitsKey must be defined elsewhere. Here's the conversion.

Dim o As Object = Nothing
If mode = PageTrackingMode.ByApplication Then
o = Page.Application(HitsKey)
ElseIf mode = PageTrackingMode.BySession Then
o = Page.Session(HitsKey)
End If

Greg Burns said:
G said:
I have this code snippet in C# that I have to convert to VB

object o = null;
if (mode == PageTrackingMode.ByApplication) {
o = Page.Application[HitsKey];
}
else if (mode == PageTrackingMode.BySession) {
o = Page.Session[HitsKey];
}

In most of the C# code I see that is referring to Session and
Application variables I see that quotes are used i.e.
Page.Session["HitsKey"] which would translate to Page.Session("HitsKey")
in VB. But the code above is using the brackets without the quotes.
What does that mean and how would that translate to VB?
Thanks,
G
Woudln't that simply mean that HitsKey is a string variable or constant
defined someplace else?

Greg
 
H

Herfried K. Wagner [MVP]

Greg Burns said:
I have this code snippet in C# that I have to convert to VB

object o = null;
if (mode == PageTrackingMode.ByApplication) {
o = Page.Application[HitsKey];
}
else if (mode == PageTrackingMode.BySession) {
o = Page.Session[HitsKey];
}

In most of the C# code I see that is referring to Session and Application
variables I see that quotes are used i.e. Page.Session["HitsKey"] which
would translate to Page.Session("HitsKey") in VB. But the code above is
using the brackets without the quotes. What does that mean and how would
that translate to VB?
Woudln't that simply mean that HitsKey is a string variable or constant
defined someplace else?
ACK, but not necessarily a string...
 
R

Richard Myers

G Dean Blake said:
This code is actually inside a servercontrol where there is a property named
HitsKey but also there is code using a session and application variable
named "HitsKey". What I don't understand is why if they simply want to
refer to the property why not just say HitsKey instead of
Page.Session[HitsKey] (I don't know why they chose to name a session
variable and a property by the same name.) It's actually code from a book
on writing servercontrols.
Thanks,
G
You've kinda answered your own question there G.
They want to refer to the session(HitsKey) not the server control property
HitsKey that contains this code.
If they just used HitsKey then they would get a reference to the Propertry
not the session var.

Richard
 
G

G Dean Blake

But... if they want to refer to the session variable they should use
Page.Session("HitsKey") with Quotes. if they want to refer to the property
they should just say HitsKey. I have always enclosed my session variables
in quotes when writing in VB. I guess I don't know the meaning when the
quotes are not used. am I making sense?

Richard Myers said:
G Dean Blake said:
This code is actually inside a servercontrol where there is a property named
HitsKey but also there is code using a session and application variable
named "HitsKey". What I don't understand is why if they simply want to
refer to the property why not just say HitsKey instead of
Page.Session[HitsKey] (I don't know why they chose to name a session
variable and a property by the same name.) It's actually code from a book
on writing servercontrols.
Thanks,
G
You've kinda answered your own question there G.
They want to refer to the session(HitsKey) not the server control property
HitsKey that contains this code.
If they just used HitsKey then they would get a reference to the Propertry
not the session var.

Richard
 
Ad

Advertisements

G

Greg Burns

Sounds like you can refer to your session variable in one of two ways:

Page.Session(HitsKey) or Page.Session("my_session_key") where
"my_session_key" is whatever string value that was assigned as the key.

For both of these to return the same session value then HitsKey must be
defined like so:

Dim HitsKey As String = "my_session_key"

Sounds like they made HitsKey a property value for the server control so
that somebody could set what that text value key should be. As opposed to
hard-coding it in the server control as (for example) "my_session_key".

They are trying to encapsulate the server control and hide all the messy
details. One of those details is the session key's name. But you can't
really fully encapsulate a session variable, because the rest of the program
still has access to your session and not just through your server control.

Am I making sense? Probably not. :^)

Greg


G Dean Blake said:
But... if they want to refer to the session variable they should use
Page.Session("HitsKey") with Quotes. if they want to refer to the
property they should just say HitsKey. I have always enclosed my session
variables in quotes when writing in VB. I guess I don't know the meaning
when the quotes are not used. am I making sense?

Richard Myers said:
G Dean Blake said:
This code is actually inside a servercontrol where there is a property named
HitsKey but also there is code using a session and application variable
named "HitsKey". What I don't understand is why if they simply want to
refer to the property why not just say HitsKey instead of
Page.Session[HitsKey] (I don't know why they chose to name a session
variable and a property by the same name.) It's actually code from a book
on writing servercontrols.
Thanks,
G
You've kinda answered your own question there G.
They want to refer to the session(HitsKey) not the server control
property
HitsKey that contains this code.
If they just used HitsKey then they would get a reference to the
Propertry
not the session var.

Richard
 

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

Top