Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. if/then/else variable frustration

if/then/else variable frustration

Scheduled Pinned Locked Moved C#
databasecsharptestingbeta-testinghelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Embry
    wrote on last edited by
    #1

    Hello- I'm fairly new to C# (classic ASP guy), and I'm having a heckuva time with this. I am hoping someone can point out my flawed logic and/or syntax. I first need to ascertain if the current article group is one of several that need to be treated differently. If it is, it defines the var "strbody" using a complete value as retrieved from the database. If it is not one of those special article groups, then I have to do some string manipulation to format the retrieved value before defining and displaying it. My string manipulation code is flawless, but neither of the strbody vars I define in my if/then/else block is recognized when I call it below the code block???

    @{
    string group = Model.ArticleGroupName;

    if (group.Contains("Spacial Orientation")||group.Contains("Topography")||group.Contains("Osteology")||group.Contains("Angiology")||group.Contains("Neurology")||group.Contains("Myology")||group.Contains("Radiology")||group.Contains("Misc. Drawings")||group.Contains("Clinical Testing"))
    {
    var strbody = item.ShortBody;
    }
    else
    {

    string s = item.ShortBody;
    string sLess = s.Remove(0, 12);
    int index = sLess.IndexOf("Summary");
    var strbody = (sLess.Substring(index + 8));
    }
    

    }
    @strbody

    That code results in the following error: \Plugins\FoxNetSoft.Articles\Views\ArticleRead\List.cshtml(76): error CS0103: The name 'strbody' does not exist in the current context I am new to this, so please don't hesitate to chastise me for doing dumb stuff...I need to learn! Thanks, Steve

    P M 2 Replies Last reply
    0
    • S Steve Embry

      Hello- I'm fairly new to C# (classic ASP guy), and I'm having a heckuva time with this. I am hoping someone can point out my flawed logic and/or syntax. I first need to ascertain if the current article group is one of several that need to be treated differently. If it is, it defines the var "strbody" using a complete value as retrieved from the database. If it is not one of those special article groups, then I have to do some string manipulation to format the retrieved value before defining and displaying it. My string manipulation code is flawless, but neither of the strbody vars I define in my if/then/else block is recognized when I call it below the code block???

      @{
      string group = Model.ArticleGroupName;

      if (group.Contains("Spacial Orientation")||group.Contains("Topography")||group.Contains("Osteology")||group.Contains("Angiology")||group.Contains("Neurology")||group.Contains("Myology")||group.Contains("Radiology")||group.Contains("Misc. Drawings")||group.Contains("Clinical Testing"))
      {
      var strbody = item.ShortBody;
      }
      else
      {

      string s = item.ShortBody;
      string sLess = s.Remove(0, 12);
      int index = sLess.IndexOf("Summary");
      var strbody = (sLess.Substring(index + 8));
      }
      

      }
      @strbody

      That code results in the following error: \Plugins\FoxNetSoft.Articles\Views\ArticleRead\List.cshtml(76): error CS0103: The name 'strbody' does not exist in the current context I am new to this, so please don't hesitate to chastise me for doing dumb stuff...I need to learn! Thanks, Steve

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Declare strbody outside of the if/else block, then assign it inside the block. As your code stands, the scope of the variable belongs inside the relevant if/else part. This is just standard behaviour for C#; it's the reason you have to define it twice in the if/else block right now - they are actually different variables. The fact that you can use the same name here is irrelevant.

      S 1 Reply Last reply
      0
      • S Steve Embry

        Hello- I'm fairly new to C# (classic ASP guy), and I'm having a heckuva time with this. I am hoping someone can point out my flawed logic and/or syntax. I first need to ascertain if the current article group is one of several that need to be treated differently. If it is, it defines the var "strbody" using a complete value as retrieved from the database. If it is not one of those special article groups, then I have to do some string manipulation to format the retrieved value before defining and displaying it. My string manipulation code is flawless, but neither of the strbody vars I define in my if/then/else block is recognized when I call it below the code block???

        @{
        string group = Model.ArticleGroupName;

        if (group.Contains("Spacial Orientation")||group.Contains("Topography")||group.Contains("Osteology")||group.Contains("Angiology")||group.Contains("Neurology")||group.Contains("Myology")||group.Contains("Radiology")||group.Contains("Misc. Drawings")||group.Contains("Clinical Testing"))
        {
        var strbody = item.ShortBody;
        }
        else
        {

        string s = item.ShortBody;
        string sLess = s.Remove(0, 12);
        int index = sLess.IndexOf("Summary");
        var strbody = (sLess.Substring(index + 8));
        }
        

        }
        @strbody

        That code results in the following error: \Plugins\FoxNetSoft.Articles\Views\ArticleRead\List.cshtml(76): error CS0103: The name 'strbody' does not exist in the current context I am new to this, so please don't hesitate to chastise me for doing dumb stuff...I need to learn! Thanks, Steve

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        While you have the answer to your question there is other points that I feel needs making. I shudder with horror whenever I see a test against string content, I automatically assume the string can be edited by a user and you are screwed. Next is the question of expanding criteria - what are you going to do when they add another term? And you know they will. Built a construct such as a key word table when flags to define the function where it applies. The you can build a function that performs the test and return a boolean. This can be used in multiple places if required.

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • P Pete OHanlon

          Declare strbody outside of the if/else block, then assign it inside the block. As your code stands, the scope of the variable belongs inside the relevant if/else part. This is just standard behaviour for C#; it's the reason you have to define it twice in the if/else block right now - they are actually different variables. The fact that you can use the same name here is irrelevant.

          S Offline
          S Offline
          Steve Embry
          wrote on last edited by
          #4

          Thanks for the response! I previously tried declaring the strbody before the if statement as you suggested, but when I did, I got this error: A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else Learning C# is really frustrating!Confused | :confused: I could have done this exact thing with VBScript in about 2 minutes..and 8 less lines of code...

          D 1 Reply Last reply
          0
          • S Steve Embry

            Thanks for the response! I previously tried declaring the strbody before the if statement as you suggested, but when I did, I got this error: A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else Learning C# is really frustrating!Confused | :confused: I could have done this exact thing with VBScript in about 2 minutes..and 8 less lines of code...

            D Offline
            D Offline
            DRAYKKO
            wrote on last edited by
            #5

            Steve Embry wrote:

            A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else

            Looks like that variable is being defined somewhere else in the code; in other words you defined a variable with that exact same spelling and then tried to define it again somewhere else in the code. Try the previous suggestion of defining "strbody" before the statement and use the Find function (if you're using VS, navigate to Edit > Find and Replace > Quick Find) to try to locate all instances of that variable name, and see if maybe you used it for a different variable. Then just change the name (but, if you do, remember to update all references to the changed variable).

            ======================= Every experience in life is a lesson to be learned A. Stevens B.S., Computer Science

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups