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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Unkown array length of arrays; How?

Unkown array length of arrays; How?

Scheduled Pinned Locked Moved C#
csharpdata-structureshelptutorialquestion
12 Posts 3 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.
  • T T Willey

    So I'm reading infomation from a text file. It will have two values, and I will supply a third. I don't know how to define the variable since I don't know how many lines will be in the text file, but I do know that the arrays within the main array will have three values. So I was thinking along the lines of. string[,3] StrAr; or string[3,] StrAr; Just to get it defined, but that didn't work. I'm new still to C#, since it is not my primary language, and the one I have a background on only seems to work in the program I learned it in (Lisp through AutoCAD). Any help/comments is/are appreciated. Thanks in advance.

    Tim

    A Offline
    A Offline
    Aaron VanWieren
    wrote on last edited by
    #3

    Not quite sure what you are trying but here goes the basics.... Single Array str[] strArry = new string[3]; //Number of items Double Array str[,] strArry=new string[4,3]; //4 Rows 3 Columns Not too sure about anything past this, but it would be an additional ','. The problem you are having is the 3 in the brackets. The trick here is that an array size has to be declared when initialized, as it cannot be set dynamically, so that is where you are going to have your largest problem. Hope this helps to get you going in the right direction.

    T 1 Reply Last reply
    0
    • A Aaron VanWieren

      Not quite sure what you are trying but here goes the basics.... Single Array str[] strArry = new string[3]; //Number of items Double Array str[,] strArry=new string[4,3]; //4 Rows 3 Columns Not too sure about anything past this, but it would be an additional ','. The problem you are having is the 3 in the brackets. The trick here is that an array size has to be declared when initialized, as it cannot be set dynamically, so that is where you are going to have your largest problem. Hope this helps to get you going in the right direction.

      T Offline
      T Offline
      T Willey
      wrote on last edited by
      #4

      Yea, I think I have a problem. The way I want to do it doesn't seem to want to work. I see that there is a method of the file class that will read all the lines to a string array. If I use that, then I will know the length, but then I'm reading the information twice, which will be slower. I will still look for other ways. Thanks for the hints.

      Tim

      A 1 Reply Last reply
      0
      • T T Willey

        So I'm reading infomation from a text file. It will have two values, and I will supply a third. I don't know how to define the variable since I don't know how many lines will be in the text file, but I do know that the arrays within the main array will have three values. So I was thinking along the lines of. string[,3] StrAr; or string[3,] StrAr; Just to get it defined, but that didn't work. I'm new still to C#, since it is not my primary language, and the one I have a background on only seems to work in the program I learned it in (Lisp through AutoCAD). Any help/comments is/are appreciated. Thanks in advance.

        Tim

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

        You could declare a simple class or struct that encapsulates the three values and afterwards use a generic List of this class or struct. The folowing examples uses no useful names and of course the instance fields should be made accessible by using properties.

        class Values
        {
        public string one;
        public string two;
        public string three;

        public Values(string one, string two, string three)
        {
            this.one = one;
            this.two = two;
            this.three = three;
        }
        

        }

        List list = new List();

        By the way, the encapsulation of the values furthermore allows them by name which increases readability of the code.


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        T 1 Reply Last reply
        0
        • T T Willey

          Yea, I think I have a problem. The way I want to do it doesn't seem to want to work. I see that there is a method of the file class that will read all the lines to a string array. If I use that, then I will know the length, but then I'm reading the information twice, which will be slower. I will still look for other ways. Thanks for the hints.

          Tim

          A Offline
          A Offline
          Aaron VanWieren
          wrote on last edited by
          #6

          Could you explain more of what you are trying to do. There are other options for collections besides arrays,maybe a combination of them would work.

          T 1 Reply Last reply
          0
          • A Aaron VanWieren

            Could you explain more of what you are trying to do. There are other options for collections besides arrays,maybe a combination of them would work.

            T Offline
            T Offline
            T Willey
            wrote on last edited by
            #7

            What I have is a csv file that has the names of my drawings, and the revision level they are at in the repository. I want to take that file and fill in a dialog box with the information for it, and the drawing. I can get the current level from the drawing, I have code for that already. Then if the revision level of the drawing needs to be updated (long story, happens alot) then I can pick it in the dialog box, type what the new revision should be, and have it update the drawing. I'm thinking of having it as a list view, with details shown in the dialog, so all the items will be on one line, and have the options of headers. Where I'm stuck at is, trying to create an way to have a list/array nested in another list/array that will fill out my dialog box. I don't know how long the main lis/array will be, but the sub list/array will only be three items. Hope that explains it a little more clearer. Thanks for helping.

            Tim

            1 Reply Last reply
            0
            • S Stefan Troschuetz

              You could declare a simple class or struct that encapsulates the three values and afterwards use a generic List of this class or struct. The folowing examples uses no useful names and of course the instance fields should be made accessible by using properties.

              class Values
              {
              public string one;
              public string two;
              public string three;

              public Values(string one, string two, string three)
              {
                  this.one = one;
                  this.two = two;
                  this.three = three;
              }
              

              }

              List list = new List();

              By the way, the encapsulation of the values furthermore allows them by name which increases readability of the code.


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              T Offline
              T Offline
              T Willey
              wrote on last edited by
              #8

              Thank you Stefan. I'm not sure how to use this yet, but it looks like it might work. Let me experiment with it, and if I can't figure it out, I will post back.

              Tim

              S 1 Reply Last reply
              0
              • T T Willey

                Thank you Stefan. I'm not sure how to use this yet, but it looks like it might work. Let me experiment with it, and if I can't figure it out, I will post back.

                Tim

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

                Learning by doing; I like that attitude :-D I have one more small example though, because I saw that something was lost in my first posting (the generic part of the list declaration). I've also included two more lines that simply add a value and afterwards access it. Have fun with experimenting :)

                List<Values> list = new List<Values>();
                list.Add(new Values("a", "b", "c"));
                string three = list[0].three;


                "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                www.troschuetz.de

                T 1 Reply Last reply
                0
                • S Stefan Troschuetz

                  Learning by doing; I like that attitude :-D I have one more small example though, because I saw that something was lost in my first posting (the generic part of the list declaration). I've also included two more lines that simply add a value and afterwards access it. Have fun with experimenting :)

                  List<Values> list = new List<Values>();
                  list.Add(new Values("a", "b", "c"));
                  string three = list[0].three;


                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                  www.troschuetz.de

                  T Offline
                  T Offline
                  T Willey
                  wrote on last edited by
                  #10

                  Thanks again Stefan. I think I can make this work. I hate the idea of just asking for people to do things for me. I only ask when I can't find how to do something, either with the help files, or a search on the net. I have not used lists in C# yet, in (Auto)Lisp I use them all the time, and love them. I hope they are kind of the same way in C#.

                  Tim

                  S 1 Reply Last reply
                  0
                  • T T Willey

                    Thanks again Stefan. I think I can make this work. I hate the idea of just asking for people to do things for me. I only ask when I can't find how to do something, either with the help files, or a search on the net. I have not used lists in C# yet, in (Auto)Lisp I use them all the time, and love them. I hope they are kind of the same way in C#.

                    Tim

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

                    T.Willey wrote:

                    I hate the idea of just asking for people to do things for me.

                    Unfortunately not everybody thinks this way.

                    T.Willey wrote:

                    I have not used lists in C# yet, in (Auto)Lisp I use them all the time, and love them. I hope they are kind of the same way in C#.

                    I don't know (Auto)Lisp, so I can not promise anything, but personally I like list and all the other generic stuff they introduced in .NET 2.0. Gives alot new possibilities and makes programming much easier sometimes.


                    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                    www.troschuetz.de

                    1 Reply Last reply
                    0
                    • T T Willey

                      So I'm reading infomation from a text file. It will have two values, and I will supply a third. I don't know how to define the variable since I don't know how many lines will be in the text file, but I do know that the arrays within the main array will have three values. So I was thinking along the lines of. string[,3] StrAr; or string[3,] StrAr; Just to get it defined, but that didn't work. I'm new still to C#, since it is not my primary language, and the one I have a background on only seems to work in the program I learned it in (Lisp through AutoCAD). Any help/comments is/are appreciated. Thanks in advance.

                      Tim

                      T Offline
                      T Offline
                      T Willey
                      wrote on last edited by
                      #12

                      It appears once again I have gone the wrong way. I get caught up in arrays when I try to use C# in place where I don't have to. Instead of putting the items into an array, and then into the dialog box, I just put them into the dialog box when I get them. I thank the people who responded and helped me, I hope you don't think I wasted your time, as I did learn some new things that I think will be helpful in the future. One thing I did look into was the list class, but since the program I'm programming for only use .Net1.1, that doesn't work for this issue at this time. Once again Thanks.

                      Tim

                      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