0

Possible Duplicate:
Is there a global variable (across web pages) I can use in C# WebMatrix, that actually works?

I have a rather odd (to me) problem. I have tested and tried many things and traced the problem down to one simple fact.

When I try to compare an AppState["uniqueName"] to a string value, it always evaluates to false. Here are a couple of snippets so you can see what I have:

On the first page:

AppState["gAdmitsMembership"] = AdmitsMembership;

On the second page:

if(AppState["gAdmitsMembership"]=="true"){checkBoxes[0]="checked='checked'";}else{checkBoxes[0]="";}

As you can probably tell the point is to keep a checkbox checked after submitting a form (and subsequently bringing the data back up in another form for possible editing)

Now, as I stated, I have tested many things and this is what I have done.

I plotted the value of AppState["gAdmitsMembership"] directly to a text input field (in the second page), in order to see what the actual value being tested was. It was indeed "true", however it still always equates to false (which I determined with another test by manipulating what happens with the "else".

Why is this failing the if condition?

It is important to note that nowhere in the code is this value "true" a boolean value. It is always the string "true". I have to do it this way because when I try:

if(AppState["gAdmitsMembership"]==true){checkBoxes[0]="checked='checked'";}else{checkBoxes[0]="";}

(same thing only with boolean true)

I get an error that says '== cannot be compared to objects or boolean' or something to that effect.

For this reason the compared string value must remain as "true" so that when it is added to the database it will be converted to the database as the necessary boolean value, true.

Anyway, any help that gets me through this will be quickly accepted and definitely appreciated. Thanks!

Community
  • 1
  • 1
VoidKing
  • 6,282
  • 9
  • 49
  • 81

1 Answers1

-1

The syntax you use to assign a value to the AppState variable on the first page is not the right one.

Try with

App.gAdmitsMembership = AdmitsMembership;

as stated in this previous thread: Webmatrix 2: Storing static values.

Community
  • 1
  • 1
GmG
  • 1,372
  • 1
  • 9
  • 10
  • You can use either syntax to set and retrieve a global variable. – Mike Brind Sep 22 '12 at 12:13
  • @GmG Exactly, Mike is right. The problem is that the AppState variables are referenced as objects, and in order to compare them casting is needed. That is: (string)AppState["gAdmitsMembership"]. Note, however that if I needed the value as pretty much anything else (other than string) I would have to go through (what I consider) the clunky process of "unboxing" the AppState variable by assigning it to a new object variable, then using it to compare then reassign the AppState variable. – VoidKing Oct 29 '12 at 13:42