-2

The problem is that I can't find a way to test anything stored in AppState["variableName"] (or App.variableName or HttpContext.Current.Application["variableName"], etc.) inside an if condition. (It only ever sees it as an object, even though I can plot it on the page with Razor as the string variable I thought it was)

So, I can't compare them to say an actual string value.

I've tried ToString(), among countless other attempts, to no avail.

My question is: How can I achieve full functionality with the AppState variable in WebMatrix Web-Pages with C#?

VoidKing
  • 6,282
  • 9
  • 49
  • 81
  • You're saying: `if ((string) AppState["variableName"] == "foo")` doesn't work? – aquinas Sep 21 '12 at 20:02
  • That's correct, it does not. I have done many tests to narrow it down to the fact that even if the strings are identical (and I have tested the output) the expression is still considered false. – VoidKing Sep 21 '12 at 20:15
  • in fact, if I set two totally different string "AppStates" and compare them, it equates to true. It seems to only be comparing types or something. – VoidKing Sep 21 '12 at 20:16
  • What you're saying is impossible. :) – aquinas Sep 21 '12 at 20:26
  • and yes i have tried casting... – VoidKing Sep 21 '12 at 20:29
  • The thing is, if I (for testing purposes) plot Session["myData"] directly to the html form, it inputs the exact string text I would expect, so I can physically see that the string values are in fact exactly identical, but when used in an if statement, it simply always claims to be false – VoidKing Sep 21 '12 at 20:29
  • Here's the if statement I am using (with humorous data for now, lol) if(AppState["gPOIName"] == "Roger"){AppState["gPOIName"] = "Bilbo";}else{AppState["gPOIName"] = "boogers";} – VoidKing Sep 21 '12 at 20:30
  • Now if I bypass the if statement and just directly plot the AppState["gPOIName"] value to an html form text, I see exactly "Roger" (without quotes, of course). So... either "Roger" != "Roger" or it doesn't work the way we think it does. – VoidKing Sep 21 '12 at 20:32
  • Also, I know that the line is being executed because when I plot the result, I only always get, "boogers" Yes, I know, my choice of string test variables, is both weird and random, lol – VoidKing Sep 21 '12 at 20:33
  • Well, I just downloaded WebMatrix 2, said create new site from template. Personal Site. In _AppStart.cshtml I added: `AppState["Stuff"]="Hello";`. Then in Default.cshtml I added: `@{ if ((string)AppState["Stuff"] == "Hello"){ @Html.Raw("OK then"); } }` and it worked just like you would expect. – aquinas Sep 21 '12 at 20:36
  • well, that's not quite duplicating the scenario. I am actually storing the data in the first page and then checking it in another. – VoidKing Sep 21 '12 at 20:43
  • Show the exact code you are using to set the data on the first page and compare on the second page. – aquinas Sep 21 '12 at 20:57
  • Ok, in your first example you showed: if ((string) AppState["variableName"] == "foo") However, in your second example you showed: @{ if ((string)AppState["Stuff"] == "Hello"){ @Html.Raw("OK then"); } } Luckily you decided to leave out the space between the casting (string) and the call to AppState the second time The point is that this worked! So, in all fairness I have to accept your answer (Thank you so much btw), and as you can tell I haven't done any type casting in a while. What I don't understand is how it every changed from string to object in the first place, to force me to cast it. – VoidKing Sep 21 '12 at 20:57
  • Also why didn't .ToString work? Don't see the difference there. – VoidKing Sep 21 '12 at 21:02
  • ToString() *would* work. That's why I wanted to see your original code. :) The reason why you have to cast to string is because AppState returns an *object* not a string. Therefore the compiler does a REFERENCE comparison, rather than the string == operator which does a value comparison. – aquinas Sep 21 '12 at 21:06
  • trust me .ToString() didn't work (no compiler errors or anything, just kept evaluating to false like it was any other way [and yes I used parenthesis to group the AppState, just to be safe]), it was one of the first things I tried after checking a little syntax, but either way though, at least the casting did. – VoidKing Sep 21 '12 at 21:30
  • Uh oh, why didn't you post as an answer? Then I could accept it and give you some more reputation. – VoidKing Sep 21 '12 at 21:32
  • I think that this thread can resolve your problem: http://stackoverflow.com/questions/12263892/webmatrix-2-storing-static-values. – GmG Sep 22 '12 at 09:13
  • @VoidKing: you need to post the actual code you are using that doesn't work so that people can duplicate your scenario. You are clearly doing something wrong. If used correctly, the AppState dictionary works in exactly the way you want it to. – Mike Brind Sep 22 '12 at 10:04
  • Well we already got it working, so it's over... Thanks for knocking my reputation down just to be mean :) I will remember you – VoidKing Sep 24 '12 at 13:13

1 Answers1

1

The problem here is that casting is needed, without a space between the cast and the AppState variable. At the time that I posted this question, I was still so new (well, still am really) to C# server side programming. An example of what works is:

if ((string)AppState["myVariable"] == "someString")
{
    //do some stuff
}

Also, whether many people like the term "global variable" or not, the AppState variable is, in fact, considered a global variable. This is clearly stated in Mike Brind's Mikesdotnetting article "Tranferring Data Between ASP.NET Web Pages" in the very first line under Application Variables:

"Application variables are also known as global variables." --(Mikesdotnetting)

Also, if you (whoever you are) have not read this article and are either new to WebMatrix or want to see all of the options for transferring data between pages in WebMatrix, please do yourself a tremendous favor and read this easy-to-read, well-written, and highly educational article found here:

http://www.mikesdotnetting.com/Article/192/Transferring-Data-Between-ASP.NET-Web-Pages

VoidKing
  • 6,282
  • 9
  • 49
  • 81