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. Dynamiclly naming variables

Dynamiclly naming variables

Scheduled Pinned Locked Moved C#
help
12 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
    saud_a_k
    wrote on last edited by
    #1

    I have to name varibles depending on a count variable. I want to append it to the variable like int "newno" + count = new int(); This gives a syntax error I also tried takin the new value in a string, but that didn't work as well .. It said : A local variable named 'var' is already defined in this scope :wtf: when I tried string var = "newno" + count; and int var = new int(); HelP!! _____________________________________________________ Yea! I could be wrong...

    E S H 3 Replies Last reply
    0
    • S saud_a_k

      I have to name varibles depending on a count variable. I want to append it to the variable like int "newno" + count = new int(); This gives a syntax error I also tried takin the new value in a string, but that didn't work as well .. It said : A local variable named 'var' is already defined in this scope :wtf: when I tried string var = "newno" + count; and int var = new int(); HelP!! _____________________________________________________ Yea! I could be wrong...

      E Offline
      E Offline
      etfintnick
      wrote on last edited by
      #2

      Hi! What exactly are you trying to do? Are you sure you shouldn't be using arrays of some sort instead? An example: string[] varArray = new string[ sizeGoesHere ]; for( int i=0; i Zerxes

      E S 2 Replies Last reply
      0
      • E etfintnick

        Hi! What exactly are you trying to do? Are you sure you shouldn't be using arrays of some sort instead? An example: string[] varArray = new string[ sizeGoesHere ]; for( int i=0; i Zerxes

        E Offline
        E Offline
        etfintnick
        wrote on last edited by
        #3

        A little error snuck in.. for( int i=0; i< Zerxes

        E 1 Reply Last reply
        0
        • E etfintnick

          A little error snuck in.. for( int i=0; i< Zerxes

          E Offline
          E Offline
          etfintnick
          wrote on last edited by
          #4

          Ack.. That's what you get for trying to post code. This should be correct for( int i=0; i

          1 Reply Last reply
          0
          • E etfintnick

            Hi! What exactly are you trying to do? Are you sure you shouldn't be using arrays of some sort instead? An example: string[] varArray = new string[ sizeGoesHere ]; for( int i=0; i Zerxes

            S Offline
            S Offline
            saud_a_k
            wrote on last edited by
            #5

            ok but that's no what I'm tryin to do.. I have a variable static int count = 0; . it is global. I am adding a text box each time I click a button say..

            private void btnAddCond_Click(object sender, System.EventArgs e)
            {
            TextBox "tb" + count = new TextBox();
            //..right here ..I need to make each textbox have a uniqe name as and when it is added..I can't do that!!:mad:
            this.Controls.Add("tb" + count); // I want this to work..!!:mad:
            }

            I hope it explains my plight!!:(( Help PLZ _____________________________________________________ Yea! I could be wrong...

            S 1 Reply Last reply
            0
            • S saud_a_k

              ok but that's no what I'm tryin to do.. I have a variable static int count = 0; . it is global. I am adding a text box each time I click a button say..

              private void btnAddCond_Click(object sender, System.EventArgs e)
              {
              TextBox "tb" + count = new TextBox();
              //..right here ..I need to make each textbox have a uniqe name as and when it is added..I can't do that!!:mad:
              this.Controls.Add("tb" + count); // I want this to work..!!:mad:
              }

              I hope it explains my plight!!:(( Help PLZ _____________________________________________________ Yea! I could be wrong...

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              Thought about using the Name property of TextBox class? Otherwise i don't see a need to give your variable a unique name, because it's gone as soon as you leave the event handler.

              private void btnAddCond_Click(object sender, System.EventArgs e)
              {
              TextBox textbox1 = new TextBox();
              this.Controls.Add(textbox1);
              }
              // identifier textbox1 is out of scope


              S 1 Reply Last reply
              0
              • S Stefan Troschuetz

                Thought about using the Name property of TextBox class? Otherwise i don't see a need to give your variable a unique name, because it's gone as soon as you leave the event handler.

                private void btnAddCond_Click(object sender, System.EventArgs e)
                {
                TextBox textbox1 = new TextBox();
                this.Controls.Add(textbox1);
                }
                // identifier textbox1 is out of scope


                S Offline
                S Offline
                saud_a_k
                wrote on last edited by
                #7

                I thank you Mr. Stefan Troschütz :rose: Yes It works!! thankx _____________________________________________________ Yea! I could be wrong...

                1 Reply Last reply
                0
                • S saud_a_k

                  I have to name varibles depending on a count variable. I want to append it to the variable like int "newno" + count = new int(); This gives a syntax error I also tried takin the new value in a string, but that didn't work as well .. It said : A local variable named 'var' is already defined in this scope :wtf: when I tried string var = "newno" + count; and int var = new int(); HelP!! _____________________________________________________ Yea! I could be wrong...

                  S Offline
                  S Offline
                  saud_a_k
                  wrote on last edited by
                  #8

                  as Mr. Stefan Troschütz has stated for TextBoxes , How do you do it for a simple Integer? _____________________________________________________ Yea! I could be wrong...

                  S 1 Reply Last reply
                  0
                  • S saud_a_k

                    as Mr. Stefan Troschütz has stated for TextBoxes , How do you do it for a simple Integer? _____________________________________________________ Yea! I could be wrong...

                    S Offline
                    S Offline
                    Stefan Troschuetz
                    wrote on last edited by
                    #9

                    What exactly do you want to do with your integers? I guess storing them in some way like the textboxes :confused: Take a look at the Collections namespace[^] which contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hashtables and dictionaries.


                    S 1 Reply Last reply
                    0
                    • S Stefan Troschuetz

                      What exactly do you want to do with your integers? I guess storing them in some way like the textboxes :confused: Take a look at the Collections namespace[^] which contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hashtables and dictionaries.


                      S Offline
                      S Offline
                      saud_a_k
                      wrote on last edited by
                      #10

                      I want to name anything (integers, long, double, char etc ) which does not have a name property Dynamically. ex.

                      static int count = 0; // global
                      //......
                      {
                      //..somewhere in the code
                      int "var" + count = new int() //you get an error on this.:eek:
                      }

                      do you get what I'm tryin' to say?? _____________________________________________________ Yea! I could be wrong...

                      S 1 Reply Last reply
                      0
                      • S saud_a_k

                        I want to name anything (integers, long, double, char etc ) which does not have a name property Dynamically. ex.

                        static int count = 0; // global
                        //......
                        {
                        //..somewhere in the code
                        int "var" + count = new int() //you get an error on this.:eek:
                        }

                        do you get what I'm tryin' to say?? _____________________________________________________ Yea! I could be wrong...

                        S Offline
                        S Offline
                        Stefan Troschuetz
                        wrote on last edited by
                        #11

                        Yeah, I get but sorry this saud_a_k wrote: int "var" + count = new int() //you get an error on this isn't possible. To accomplish a similar effect you could use a SortedList[^] to store your integers whereby the key values are created by the term "var" + count.ToString().


                        1 Reply Last reply
                        0
                        • S saud_a_k

                          I have to name varibles depending on a count variable. I want to append it to the variable like int "newno" + count = new int(); This gives a syntax error I also tried takin the new value in a string, but that didn't work as well .. It said : A local variable named 'var' is already defined in this scope :wtf: when I tried string var = "newno" + count; and int var = new int(); HelP!! _____________________________________________________ Yea! I could be wrong...

                          H Offline
                          H Offline
                          Heath Stewart
                          wrote on last edited by
                          #12

                          You could use an IDictionary implementation like so:

                          Hashtable vars = new Hashtable();
                          for (int i=0; i< 10; i++)
                          vars["newno" + i.ToString()] = i;

                          Once your code is compiled, it cannot be changed. You either need to use Reflection Emit or the CodeDom to generate new source and compile it on the fly (Reflection Emit actually emits an assembly - no compilation necessary but it's far more complex and requires that you know and understand IL). For your compiled code, you can only maintain the appearance of dynamic variables. This is done through collections, dictionaries, or the ComponentModel (probably not the best idea in your case). Using the approach I described above would probably be the easiest way. To get the int, then, just do int i = (int)vars["newno0"];.

                          Microsoft MVP, Visual C# My Articles

                          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