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. Genericize access to variables

Genericize access to variables

Scheduled Pinned Locked Moved C#
question
25 Posts 20 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.
  • B BC3Tech

    how about lambda? :)

    foreach (Control l in Controls.Cast().Where(c => c.Name.StartsWith("sumLabel")))
    l.Text = l.Name;

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #21

    Lambda's look cool :cool: It's either searching through the collection and keeping the result around, or hardcoding them in advance. Attributes that mark a set of fields as a group might be another option, but that would add a bit more complexity. No, I think I like the lambda better. Would also be cool combined with a regex :) Nearly forgot, but that cast in my original query would be redundant, as we'd only need to store labels;

    class Form1
    {
    IEnumerable summingLabels;
    public Form1()
    {
    InitializeComponent();

    	summingLabels =
    		from c in Controls.Cast().AsQueryable()
    		where c.GetType() == typeof(Label)
    		&& c.Name.StartsWith("label")
    		select c as Label;
    
    	// later on, when you need them
    	foreach(Label l in summingLabels)
    		l.Text = l.Location.ToString();
    }
    

    }

    I are Troll :suss:

    S 1 Reply Last reply
    0
    • L Lost User

      Lambda's look cool :cool: It's either searching through the collection and keeping the result around, or hardcoding them in advance. Attributes that mark a set of fields as a group might be another option, but that would add a bit more complexity. No, I think I like the lambda better. Would also be cool combined with a regex :) Nearly forgot, but that cast in my original query would be redundant, as we'd only need to store labels;

      class Form1
      {
      IEnumerable summingLabels;
      public Form1()
      {
      InitializeComponent();

      	summingLabels =
      		from c in Controls.Cast().AsQueryable()
      		where c.GetType() == typeof(Label)
      		&& c.Name.StartsWith("label")
      		select c as Label;
      
      	// later on, when you need them
      	foreach(Label l in summingLabels)
      		l.Text = l.Location.ToString();
      }
      

      }

      I are Troll :suss:

      S Offline
      S Offline
      Shani Natav
      wrote on last edited by
      #22

      Or Maybe to make it nicer with no casting and type checking:

      summingLabels =
      from c in Controls.OfType()
      where c.Name.StartsWith("label")
      select c;

      	// later on, when you need them
      	foreach(Label l in summingLabels)
      		l.Text = l.Location.ToString();
      
      L 1 Reply Last reply
      0
      • S Shani Natav

        Or Maybe to make it nicer with no casting and type checking:

        summingLabels =
        from c in Controls.OfType()
        where c.Name.StartsWith("label")
        select c;

        	// later on, when you need them
        	foreach(Label l in summingLabels)
        		l.Text = l.Location.ToString();
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #23

        Yup, works, and more concise and readable :thumbsup:

        I are Troll :suss:

        1 Reply Last reply
        0
        • D David Knechtges

          I googled this extensively yesterday and didn't come out with an answer: Is it possible to do some thing like this, and if so how? labela1.Text = "something"; labela2.Text = "something"; labela3.Text = "something"; Now what I want is a way to write that code as say: for (int i=1;i<4;i++) labelai.Text = "something"; so that the i in labelai above is replaced by 1, 2, 3 at runtime and the net result of the for loop is the same as what happens in the block above. Is there a way to do that? Thanks!

          R Offline
          R Offline
          RMcEachern
          wrote on last edited by
          #24

          How to do this is quite simple, but not obvious since we do not normally think of dimensioning a variable as a control type. The simplest way (to me) is to dimension an array of labels, Label(1), Label(2) etc. as Label. Then set each member of the array equal to each of the labels you want to control a property for. Then you can write a loop setting a property of each label in the array as an iterated function, such as: Dim Label(1) as Label Dim Label(2) as Label ... Label(1) = LabelA Label(2) = LabelB ... For x = 1 to 5 Label(x).text = "Text " & x Next (Sorry, I am an old VB.NET programmer, but the concept is the same in all :-D ) I think this is what you want, works great for me, and works for the other common controls, too.

          1 Reply Last reply
          0
          • D David Knechtges

            I googled this extensively yesterday and didn't come out with an answer: Is it possible to do some thing like this, and if so how? labela1.Text = "something"; labela2.Text = "something"; labela3.Text = "something"; Now what I want is a way to write that code as say: for (int i=1;i<4;i++) labelai.Text = "something"; so that the i in labelai above is replaced by 1, 2, 3 at runtime and the net result of the for loop is the same as what happens in the block above. Is there a way to do that? Thanks!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #25

            take a look at the dictionary class

            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