473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

array as struct field

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

Expand|Select|Wrap|Line Numbers
  1. struct MyStruct
  2. {
  3.   ....
  4.   ref int[] someArr; 
  5.   ....
  6. }
init:
Expand|Select|Wrap|Line Numbers
  1. new InsideStation 
  2. {
  3.   ....
  4.   ExistingArrayName; // passed to a structure object
  5.   ....
  6. }
will i be able to access this array like this?
Expand|Select|Wrap|Line Numbers
  1. MyClass.getStructField().someArr[someIndex]; 
is that correct?
Sep 22 '09 #1
12 2606
GaryTexmo
1,501 Expert 1GB
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.
Sep 22 '09 #2
tlhintoq
3,525 Expert 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. struct MyStruct
  2. {
  3.   ref int[] someArr; 
  4. }
  5.  
  6. // init
  7. new InsideStation 
  8. {
  9.   ExistingArrayName; // passed to a structure object
  10. }
  11.  
  12. //Reference
  13. MyClass.getStructField().someArr[someIndex]; 
  14.  
What is the new InsideStation part supposed to be? Is that supposed to be a construction for a class? You have a variable (ExistingArrayName) on a like by itself doing nothing? What's that about?

MyClass.getStructField().someArr[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)
Sep 22 '09 #3
@tlhintoq
Although, it didn't work, i appreciate that you actually respond fast and being helpful. Also - great humor))))
Sep 24 '09 #4
Code works fine. Only got couple of questions about style of the writing
Expand|Select|Wrap|Line Numbers
  1. namespace dugma3
  2. {
  3.     /// <summary>
  4.     /// Description of MyClass1.
  5.     /// </summary>
  6.     public class MyClass1
  7.     {
  8.         public struct Domik { 
  9.             public MenuItem[] ItemsArr; 
  10.         } 
  11.  
  12.         public struct MenuItem {
  13.             // anything 
  14.         }
  15.         // ****************************************************************
  16.         public MyClass1() // builder 
  17.         {
  18.             MenuItem[] HiTech_U = new MenuItem[] {
  19.                 // init sub objects here
  20.             };
  21.             MenuItem[] Employment_Office = new MenuItem[] {
  22.                 // init sub objects here
  23.             };
  24.             // *************************************
  25.             // manual structure array init - it works, but I'm not sure that it's "correct" syntax
  26.             // may be there's another more professional way/syntax to init struct array? 
  27.             Domik[] MyArray = new Domik[] {
  28.                 new Domik { // I just copied this somewhere around Internet
  29.                     ItemsArr = HiTech_U // what is the reason to write field name? works only that way 
  30.                 }, 
  31.                 new Domik {
  32.                     ItemsArr = Employment_Office
  33.                 }
  34.             };
  35.             // *************************************
  36.         }
  37.         // ****************************************************************
  38.     }
  39. }
  • 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
Sep 26 '09 #5
tlhintoq
3,525 Expert 2GB
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?
Sep 26 '09 #6
@tlhintoq
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
Sep 26 '09 #7
tlhintoq
3,525 Expert 2GB
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?
Sep 26 '09 #8
@tlhintoq
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?
Expand|Select|Wrap|Line Numbers
  1. Domik[] MyArray = new Domik[] {
  2.   new Domik { 
  3.     ItemsArr = HiTech_U 
  4.   },  
  5.   new Domik { 
  6.     ItemsArr = Employment_Office 
  7.   } 
  8. };
I only want to know if there's a way to srink init code by getting rid of
new Domik keywords. That's all...
Sep 27 '09 #9
GaryTexmo
1,501 Expert 1GB
I think I know what you mean... you're initializing your array with something. Like..

Expand|Select|Wrap|Line Numbers
  1. int[] intArray = new int[] {1, 2, 3, 4};
The other way you could go about it would be...

Expand|Select|Wrap|Line Numbers
  1. public const int ARR_LENGTH = 4;
  2. ...
  3. int[] intArray = new int[ARR_LENGTH];
  4.  
  5. for (int i = 0; i < intArray.Length; i++)
  6.   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...

Expand|Select|Wrap|Line Numbers
  1. Domik[] MyArray = new Domik[2];
  2. // Initialize
  3. for (int i = 0; i < MyArray.Length; i++)
  4.   MyArray[i] = new Domik();
  5.  
  6. // Insert Values
  7. MyArray[0].ItemsArr = HiTech_U;
  8. 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..

Expand|Select|Wrap|Line Numbers
  1. object[] itemsToInit = new object[] {HiTech_U, Employment_Office, <your other items here>};
  2. ...
  3. Domik[] MyArray = new Domik[itemsToInit.Length];
  4. if (MyArray.Length == itemsToInit.Length)
  5. {
  6.   for (int i = 0; i < MyArray.Length; i++)
  7.   {
  8.     MyArray[i] = new Domik();
  9.     MyArray[i].ItemsArr = objectsToInit[i];
  10.   }
  11. }
NOTE: I don't know what type HiTech_U and Employment_Office 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!
Sep 27 '09 #10
@GaryTexmo
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.
Sep 27 '09 #11
2GaryTexmo

Expand|Select|Wrap|Line Numbers
  1. int andComputed = // trying to make if shorter
  2.   domikiNames.Length & OpsAmount.Length & 
  3.   OpsIndexes.Length & WorkPrs.Length & 
  4.   ShowCst.Length & ChildOps.Length; 
  5.  
  6.   if (domiki.Length == andComputed) {
  7.     for (int i = 0; i < domiki.Length; i++) {
  8.       domiki[i] = new Domik();
  9.       domiki[i].Id = i; 
  10.       domiki[i].Name = domikiNames[i]; 
  11.       domiki[i].OptionsAmount = OpsAmount[i]; 
  12.       domiki[i].OptionsIndexes = new [] OpsIndexes[i,0]; // 2 compile errors
  13.         // } expected (CS1513); { expected (CS1514)
  14.       domiki[i].WorkPresents = WorkPrs[i]; 
  15.       domiki[i].ShowCost = ShowCst[i]; 
  16.       domiki[i].ItemsArr = ChildOps[i];
  17.     }
Expand|Select|Wrap|Line Numbers
  1. public struct Domik { 
  2.   public int Id; 
  3.   public string Name; 
  4.   public int OptionsAmount; 
  5.   public int[] OptionsIndexes {get; set;} // problematic field
  6.  
  7.   public bool WorkPresents; 
  8.   public bool ShowCost; 
  9.   public MenuItem[] ItemsArr; 
Sep 28 '09 #12
GaryTexmo
1,501 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. domiki[i].OptionsIndexes = new [] OpsIndexes[i,0];
Expand|Select|Wrap|Line Numbers
  1. 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...
Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 28 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as...
1
by: mrhicks | last post by:
Hello all, I am trying to keep my coding easy for everyone to use. I have some ARINC data that need to go out which 12 Bytes long. The first byte is the command word, the next 10 bytes represent...
1
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
9
by: herobeat | last post by:
Hi all, I'm having a hell of a time with declaring a struct to hold some binary data I'm trying to read from some files on disk. What I would like to do is something like this: public struct...
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
1
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.