array as struct field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EntryTeam
    New Member
    • Aug 2009
    • 55

    array as struct field

    i need to write structure which has a field of poiner to array or something like this:

    Code:
    struct MyStruct
    {
      ....
      ref int[] someArr; 
      ....
    }
    init:
    Code:
    new InsideStation 
    {
      ....
      ExistingArrayName; // passed to a structure object
      ....
    }
    will i be able to access this array like this?
    Code:
    MyClass.getStructField().someArr[someIndex];
    is that correct?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Err, that's a little confusing but yes, I think so... assuming you create the appropriate methods in MyClass to return that struct. Still... When in doubt, try it out! :)

    I will say thought that you'll need to initialize someArr when you create your struct, otherwise I believe it will be a null reference.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      You need to mak items public if you want to be able to access them from other classes.

      None of your example code references anything in any other example code as far as I can see.
      Code:
      struct MyStruct
      {
        ref int[] someArr; 
      }
      
      // init
      new InsideStation 
      {
        ExistingArrayName; // passed to a structure object
      }
      
      //Reference
      MyClass.getStructField().someArr[someIndex];
      What is the new InsideStation part supposed to be? Is that supposed to be a construction for a class? You have a variable (ExistingArrayN ame) on a like by itself doing nothing? What's that about?

      MyClass.getStru ctField().someA rr[someIndex]
      Nobody can tell you if this will work since you didn't include code for your class or your getStructField( ) method

      will i be able to access this array like this?
      Finish building what you have in mind and try it. It costs you nothing. It would take a only a little longer than asking someone else if it will work. Become the mad scientist. Build it... make it... try it... experiment... breathe life into your monster... (don't forget to lock the door against the angry villagers)

      Comment

      • EntryTeam
        New Member
        • Aug 2009
        • 55

        #4
        Originally posted by tlhintoq

        Finish building what you have in mind and try it. It costs you nothing. It would take a only a little longer than asking someone else if it will work. Become the mad scientist. Build it... make it... try it... experiment... breathe life into your monster... (don't forget to lock the door against the angry villagers)
        Although, it didn't work, i appreciate that you actually respond fast and being helpful. Also - great humor))))

        Comment

        • EntryTeam
          New Member
          • Aug 2009
          • 55

          #5
          Code works fine. Only got couple of questions about style of the writing
          Code:
          namespace dugma3
          {
          	/// <summary>
          	/// Description of MyClass1.
          	/// </summary>
          	public class MyClass1
          	{
          		public struct Domik { 
          			public MenuItem[] ItemsArr; 
          		} 
          		
          		public struct MenuItem {
          			// anything 
          		}
          		// ****************************************************************
          		public MyClass1() // builder 
          		{
          			MenuItem[] HiTech_U = new MenuItem[] {
          				// init sub objects here
          			};
          			MenuItem[] Employment_Office = new MenuItem[] {
          				// init sub objects here
          			};
          			// *************************************
          			// manual structure array init - it works, but I'm not sure that it's "correct" syntax
          			// may be there's another more professional way/syntax to init struct array? 
          			Domik[] MyArray = new Domik[] {
          				new Domik { // I just copied this somewhere around Internet
          					ItemsArr = HiTech_U // what is the reason to write field name? works only that way 
          				}, 
          				new Domik {
          					ItemsArr = Employment_Office
          				}
          			};
          			// *************************************
          		}
          		// ****************************************************************
          	}
          }
          • Line26: May be there's another more professional way/syntax to init struct array?
          • Line28: Is that OK? I just copied this somewhere around Internet
          • Line29: What is the reason to write field name? Works only that way

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            You have a struct named "Domik" whose only content is a MenuItemArray.
            What is the point?

            The rest is just a bit... convoluted.

            What is it that you are actually trying to do/make here?

            Comment

            • EntryTeam
              New Member
              • Aug 2009
              • 55

              #7
              Originally posted by tlhintoq
              You have a struct named "Domik" whose only content is a MenuItemArray.
              What is the point?
              The point is that I only posted potentially problematic code, which is pointer to array.

              The rest is just a bit... convoluted.
              What is it that you are actually trying to do/make here?
              Generally, the aim is to describe two structures, and initialize them.
              Important points:
              • main structure has a field which points to array of second type structure;
              • initializing main structure array

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Originally posted by EntryTeam
                The point is that I only posted potentially problematic code, which is pointer to array.
                A) There are no pointers in the code snippet you provided.
                B) So in other words this isn't an actual issue you're having? This was just an exercise to find *potential* issues? Hypothetical exercise?

                Comment

                • EntryTeam
                  New Member
                  • Aug 2009
                  • 55

                  #9
                  Originally posted by tlhintoq
                  A) There are no pointers in the code snippet you provided.
                  B) So in other words this isn't an actual issue you're having? This was just an exercise to find *potential* issues? Hypothetical exercise?
                  A) This is not pointer, right. But, it works like one.
                  B) The code is fully functional, but I'm trying to figure out am I initializing it well. Next code block looks suspicious, because, quoting your previous reply:
                  What is the new InsideStation part supposed to be? Is that supposed to be a construction for a class?
                  Code:
                  Domik[] MyArray = new Domik[] {
                    new Domik { 
                      ItemsArr = HiTech_U 
                    },  
                    new Domik { 
                      ItemsArr = Employment_Office 
                    } 
                  };
                  I only want to know if there's a way to srink init code by getting rid of
                  new Domik keywords. That's all...

                  Comment

                  • GaryTexmo
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1501

                    #10
                    I think I know what you mean... you're initializing your array with something. Like..

                    Code:
                    int[] intArray = new int[] {1, 2, 3, 4};
                    The other way you could go about it would be...

                    Code:
                    public const int ARR_LENGTH = 4;
                    ...
                    int[] intArray = new int[ARR_LENGTH];
                    
                    for (int i = 0; i < intArray.Length; i++)
                      intArray[i] = i;
                    I use that method if I'm initializing my array to something I generate. In your case, you're hard coding the array indexes to specific things. Your alternative might be...

                    Code:
                    Domik[] MyArray = new Domik[2];
                    // Initialize
                    for (int i = 0; i < MyArray.Length; i++)
                      MyArray[i] = new Domik();
                    
                    // Insert Values
                    MyArray[0].ItemsArr = HiTech_U;
                    MyArray[1].ItemsArr = Employment_Office;
                    It's not really much better... and it all really depends on what MyArray will look like in the future. Will it always have those two items, or do you plan on initializing it with more later? Another way might be..

                    Code:
                    object[] itemsToInit = new object[] {HiTech_U, Employment_Office, <your other items here>};
                    ...
                    Domik[] MyArray = new Domik[itemsToInit.Length];
                    if (MyArray.Length == itemsToInit.Length)
                    {
                      for (int i = 0; i < MyArray.Length; i++)
                      {
                        MyArray[i] = new Domik();
                        MyArray[i].ItemsArr = objectsToInit[i];
                      }
                    }
                    NOTE: I don't know what type HiTech_U and Employment_Offi ce so I just used a generic object array here. You'd use the appropriate type, of course.

                    You could also load those objects from a file to get a list of them, or read from a directory, or whatever... I don't know enough about your program to suggest what you should do, only give you a bunch of options that I think might help you :)

                    Good luck!

                    Comment

                    • EntryTeam
                      New Member
                      • Aug 2009
                      • 55

                      #11
                      Originally posted by GaryTexmo
                      Code:
                      object[] itemsToInit = new object[] {HiTech_U, Employment_Office, <your other items here>};
                      ...
                      Domik[] MyArray = new Domik[itemsToInit.Length];
                      if (MyArray.Length == itemsToInit.Length)
                      {
                        for (int i = 0; i < MyArray.Length; i++)
                        {
                          MyArray[i] = new Domik();
                          MyArray[i].ItemsArr = objectsToInit[i];
                        }
                      }
                      NOTE: I don't know what type HiTech_U and Employment_Offi ce so I just used a generic object array here. You'd use the appropriate type, of course.

                      You could also load those objects from a file to get a list of them, or read from a directory, or whatever... I don't know enough about your program to suggest what you should do, only give you a bunch of options that I think might help you :)

                      Good luck!
                      The last code block is great! I'll do it tomorrow.
                      I have about 4-6 fields in each structure and manual inititalization looks like sh!t. I think this style will make my init-code much more readable.
                      Thanks, GaryTexmo.

                      Comment

                      • EntryTeam
                        New Member
                        • Aug 2009
                        • 55

                        #12
                        2GaryTexmo

                        Code:
                        int andComputed = // trying to make [B]if[/B] shorter
                          domikiNames.Length & OpsAmount.Length & 
                          OpsIndexes.Length & WorkPrs.Length & 
                          ShowCst.Length & ChildOps.Length; 
                        
                          if (domiki.Length == andComputed) {
                            for (int i = 0; i < domiki.Length; i++) {
                              domiki[i] = new Domik();
                              domiki[i].Id = i; 
                              domiki[i].Name = domikiNames[i]; 
                              domiki[i].OptionsAmount = OpsAmount[i]; 
                              domiki[i].OptionsIndexes = new [] OpsIndexes[i,0]; // 2 compile errors
                                // } expected (CS1513); { expected (CS1514)
                              domiki[i].WorkPresents = WorkPrs[i]; 
                              domiki[i].ShowCost = ShowCst[i]; 
                              domiki[i].ItemsArr = ChildOps[i];
                            }
                        }
                        Code:
                        public struct Domik { 
                          public int Id; 
                          public string Name; 
                          public int OptionsAmount; 
                          public int[] OptionsIndexes {get; set;} // problematic field
                         
                          public bool WorkPresents; 
                          public bool ShowCost; 
                          public MenuItem[] ItemsArr; 
                        }

                        Comment

                        • GaryTexmo
                          Recognized Expert Top Contributor
                          • Jul 2009
                          • 1501

                          #13
                          Code:
                          domiki[i].OptionsIndexes = new [] OpsIndexes[i,0];
                          Code:
                          public int[] OptionsIndexes {get; set;} // problematic field
                          That's very confusing to me... why do you have the get and set (with no associated code no less) and why do you have a multi-dimensional array initializer for a single dimensional array?

                          Still, I think your initialization line should read...
                          Code:
                          domik[i].OptionIndexes = new int[<your desired length here>];
                          By the way, if you're going to have get/set methods, it might be worthwhile to change Domik from a struct to a class.

                          Comment

                          Working...