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. Access different variable names on the fly

Access different variable names on the fly

Scheduled Pinned Locked Moved C#
tutorial
4 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.
  • J Offline
    J Offline
    J Cod3r
    wrote on last edited by
    #1

    Hi all, Does someone know if it is possible to access a variable and change the name of the variable based on a condition. (This does not make much sense but let me explain it using the example below. I have two foreach loops, both doing the exact same thing. But it is accessing different variables. I would prefer it if i were to have one foreach loop and access the different variable based on the string that was passed to the function. Thus having something like (string.Concat(context,"ReadOnlyProperties"))

    private void CustomizeEditor(string context)
    {
       DetailView detailViewCore = (DetailView)View;
       Object obj = detailViewCore.CurrentObject;
       if (context == "live")
       {
          foreach (string liveTargetPropertyName in liveReadOnlyProperties.Keys)
          {
             CriteriaOperator criteria = CriteriaOperator.Parse(liveReadOnlyProperties[liveTargetPropertyName]);
             UpdateProperty(detailViewCore, obj, liveTargetPropertyName, criteria);
          }
       }
       else
       {
          foreach (string saveTargetPropertyName in saveReadOnlyProperties.Keys)
          {
             CriteriaOperator criteria = CriteriaOperator.Parse(saveReadOnlyProperties[saveTargetPropertyName]);
             UpdateProperty(detailViewCore, obj, saveTargetPropertyName, criteria);
          }
       }
    }
    

    Thanks

    E N P 3 Replies Last reply
    0
    • J J Cod3r

      Hi all, Does someone know if it is possible to access a variable and change the name of the variable based on a condition. (This does not make much sense but let me explain it using the example below. I have two foreach loops, both doing the exact same thing. But it is accessing different variables. I would prefer it if i were to have one foreach loop and access the different variable based on the string that was passed to the function. Thus having something like (string.Concat(context,"ReadOnlyProperties"))

      private void CustomizeEditor(string context)
      {
         DetailView detailViewCore = (DetailView)View;
         Object obj = detailViewCore.CurrentObject;
         if (context == "live")
         {
            foreach (string liveTargetPropertyName in liveReadOnlyProperties.Keys)
            {
               CriteriaOperator criteria = CriteriaOperator.Parse(liveReadOnlyProperties[liveTargetPropertyName]);
               UpdateProperty(detailViewCore, obj, liveTargetPropertyName, criteria);
            }
         }
         else
         {
            foreach (string saveTargetPropertyName in saveReadOnlyProperties.Keys)
            {
               CriteriaOperator criteria = CriteriaOperator.Parse(saveReadOnlyProperties[saveTargetPropertyName]);
               UpdateProperty(detailViewCore, obj, saveTargetPropertyName, criteria);
            }
         }
      }
      

      Thanks

      E Offline
      E Offline
      Eduard Keilholz
      wrote on last edited by
      #2

      You can make a method which contains the loop, and call the method twice passing the variable to loop through..

      .: I love it when a plan comes together :. http://www.zonderpunt.nl

      1 Reply Last reply
      0
      • J J Cod3r

        Hi all, Does someone know if it is possible to access a variable and change the name of the variable based on a condition. (This does not make much sense but let me explain it using the example below. I have two foreach loops, both doing the exact same thing. But it is accessing different variables. I would prefer it if i were to have one foreach loop and access the different variable based on the string that was passed to the function. Thus having something like (string.Concat(context,"ReadOnlyProperties"))

        private void CustomizeEditor(string context)
        {
           DetailView detailViewCore = (DetailView)View;
           Object obj = detailViewCore.CurrentObject;
           if (context == "live")
           {
              foreach (string liveTargetPropertyName in liveReadOnlyProperties.Keys)
              {
                 CriteriaOperator criteria = CriteriaOperator.Parse(liveReadOnlyProperties[liveTargetPropertyName]);
                 UpdateProperty(detailViewCore, obj, liveTargetPropertyName, criteria);
              }
           }
           else
           {
              foreach (string saveTargetPropertyName in saveReadOnlyProperties.Keys)
              {
                 CriteriaOperator criteria = CriteriaOperator.Parse(saveReadOnlyProperties[saveTargetPropertyName]);
                 UpdateProperty(detailViewCore, obj, saveTargetPropertyName, criteria);
              }
           }
        }
        

        Thanks

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        private void CustomizeEditor(string context)
        {
        DetailView detailViewCore = (DetailView)View;
        Object obj = detailViewCore.CurrentObject;
        string parse, propertyName; // I am just assuming this is string. Change to your datatype
        if (context == "live"){
        parse = liveReadOnlyProperties[liveTargetPropertyName];
        propertyName = liveTargetPropertyName;
        keys = liveReadOnlyProperties.Keys;
        }
        else{
        parse = saveReadOnlyProperties[saveTargetPropertyName];
        propertyName = saveTargetPropertyName;
        keys = saveReadOnlyProperties.Keys;
        }

        foreach (string propertyName in keys)
        {
        CriteriaOperator criteria = CriteriaOperator.Parse(parse);
        UpdateProperty(detailViewCore, obj, propertyName , criteria);
        }
        }

        Navaneeth How to use google | Ask smart questions

        1 Reply Last reply
        0
        • J J Cod3r

          Hi all, Does someone know if it is possible to access a variable and change the name of the variable based on a condition. (This does not make much sense but let me explain it using the example below. I have two foreach loops, both doing the exact same thing. But it is accessing different variables. I would prefer it if i were to have one foreach loop and access the different variable based on the string that was passed to the function. Thus having something like (string.Concat(context,"ReadOnlyProperties"))

          private void CustomizeEditor(string context)
          {
             DetailView detailViewCore = (DetailView)View;
             Object obj = detailViewCore.CurrentObject;
             if (context == "live")
             {
                foreach (string liveTargetPropertyName in liveReadOnlyProperties.Keys)
                {
                   CriteriaOperator criteria = CriteriaOperator.Parse(liveReadOnlyProperties[liveTargetPropertyName]);
                   UpdateProperty(detailViewCore, obj, liveTargetPropertyName, criteria);
                }
             }
             else
             {
                foreach (string saveTargetPropertyName in saveReadOnlyProperties.Keys)
                {
                   CriteriaOperator criteria = CriteriaOperator.Parse(saveReadOnlyProperties[saveTargetPropertyName]);
                   UpdateProperty(detailViewCore, obj, saveTargetPropertyName, criteria);
                }
             }
          }
          

          Thanks

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Why not simply pass in the appropriate collection?

          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