Name/Value Properties

R

Raterus

Hello,

Does anyone have any ideas how I can accomplish this using properties. I'm
trying to create a property, that acts like a structure. Basically I'm
trying to create a property that I could do this with.

myCompanyIndex = currentCompany.Index
myCompanyName = currentCompany.Value

"currentCompany" would be the property, Index is an integer, Value is a
string.

Here is some of what I've tried...(I'm trying to set a single session
variable in asp.net, but for people unfamiliar with asp.net,
Session("company") would be an object)

Protected Structure NameValue
Public Name As String
Public Value As Integer
End Structure

Protected Property currentCompany() As NameValue
Get
currentCompany = CType(Session("company"), NameValue)
End Get
Set(ByVal Value As NameValue)
Session("company") = Value
End Set
End Property

This looks like it may work, but in code when I try this I get the following
error.

currentPEO.value = 1
currentPEO.Name = "My Company Name"

I get the following error on both lines..."Expression is a value and
therefore cannot be the target of an assignment."

Any help?
--Michael
 
W

William Ryan eMVP

Raterus:

Looks like Boxing.... instead of using a struct, change it to a class..that
should fix it.
 
R

Raterus

Sure did thanks! I also had to make the varibles in that class "shared", or
it wouldn't be instantiated. It seems every time I try to use a structure I
end up using a class, any point really now to use a structure over a class?
 
R

Raterus

Nope, using "shared" in the NameValue class isn't going to work. I need
some way to instantiate these properties so they use different
"NameValue"'s. Here's what I got

Public Class NameValue
Public Name As String
Public Value As Integer

Public Sub New()
Name = String.Empty
Value = -1
End Sub
End Class

Pubic Class Common
Protected Property currentCompany() As NameValue
Get
currentCompany = CType(Session("Company"), NameValue)
End Get
Set(ByVal Value As NameValue)
Session("Company") = Value
End Set
End Property
End Class

then from another class that inherits common:

currentCompany.Value = 6
which gives the good ole error "Object reference not set to an instance of
an object."
I don't know how I am supposed to issue a "new" when it isn't necessarily
new. I think I'm trying to do something I really should be doing...
 
W

William Ryan eMVP

Raterus:

No need to use Shared in this case. Your private member variable, has it
been initialized? I'm guessing no due to the exception. So, when you
create a new instance of Common set currentCompany = new NameValue and that
should get you around the problem.

I use Structs a fair amount when I need to pass something around but
Boxing/Unboxing can be a performance problem in addition to being a big
headache if you aren't careful.

One way or another, you'll need to instantiate an instance of NameValue and
you should be ok...let me know if not.

Bill
 
R

Raterus

I tried adding a New() Sub to my common class, and instantiating my
properties there, but then I run into an asp.net issue. My base pages
inherit this Common class which then inherits the Page Class. At the time
this happens, my session object isn't instantiated, so I get an error
"Session state can only be used when enableSessionState is set to true,
either in a configuration file or in the Page directive"

I can work around this by just adding a sub to the common class "Start()",
which instantiates my properties, and then calling it from Page_load. I
didn't really like that option though, as it required me to do that on every
page. Sure it's not that big of a deal though, i'd just much rather use
them right off the bat, than do all this instantiating :).

Any other options you can think of?
--Michael
 

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