473,396 Members | 2,039 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.

Arrays in Structs?

Hi, I know this is a simple question and I've googled for the answers too many times now. I've tried every possible answers in my coding however, I still cannot seem to make it work. :'(

What I wanted to do in my coding is to be able to parse and store the value of <selects> and their <options> so that I would be able to use them later in my other functions. I decided to use structs (declared as array) as there can be many <select> in a webpage and use arraylist to save their options. So that when calling them, it would look like select[1].options[1] or select[1].options[2]... etc.

My coding goes something like this:

Expand|Select|Wrap|Line Numbers
  1. class Parser {
  2.  
  3. static Select[] selects = new Select[100]; 
  4. //declared so it can be called in other fuctions however, is there a way so that even if selects is not global and declared in main, it can still be called on loop function?
  5.  
  6. public struct Select
  7.     {
  8.         public string selectname;
  9.         public int selectnumber;
  10.         public String[] options;
  11.  
  12. public Select()
  13. {
  14.  
  15. options = new String[20]; 
  16. //the error I get here is: structs cannot contain explicit parameterless constructors - I have absolutely no idea what parameter to put in there since in main, I also have selects[selectcount].selectname = selectname; and selects[selectcount].selectnumber = selectnumber; and a for(;;) selects[selectcount].options[i]=value;
  17.  
  18. }
  19.     } 
  20.  
  21. static int Main(string[] args)
  22. {
  23. //my code is long.. XD this is a shortcut :D
  24.  
  25. selects[selectcount].selectname = selectname;  selects[selectcount].selectnumber = selectnumber; 
  26. for(;;) 
  27. selects[selectcount].options[i]=value;
  28.  
  29. recursiveloop(selectcount, selects[selectcount].selectnumber);
  30. }
  31.  
  32. public void recursiveloop(int limit, int num)
  33. {
  34. //the fuction i've been talking about where...
  35. String valuehere;
  36.  
  37. for(;;)
  38.  
  39. if (condition here)
  40.      valuehere += selects[fixedvaluehere].option[i]; //it does not recognize selects if I don't declare it global.
  41.  
  42. else
  43.      recursiveloop();
  44.  
  45. display(valuehere);
  46. }
  47.  
  48. }
Err... I have no idea if I have explained my problem clearly but.. any advice or suggestions would be greatly greatly appreciated. Have been bouncing here and there and tweak codes and still not get the solution. T.T
Sep 15 '09 #1
7 2137
GaryTexmo
1,501 Expert 1GB
You're making a constructor for a struct? I didn't know you could do that...

I think you need to change your struct definition to a class. I may be mistaken though...

*Edit: Scratch that... apparently you can make a constructor for a struct, but it doesn't get called when you define an array of them. This is the same for a class as well, since you are just defining references. I'd recommend using a class, but you can use a struct and do the following...

Expand|Select|Wrap|Line Numbers
  1. public Select(int length)
  2. {
  3. options = new String[length];
  4. }
inside main...
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < selects.Length; i++)
  2.   selects[i] = new Select(20);
  3. ...
You've gotta initialize your array anyways. If you change it to a class you can use a parameter-less constructor if you want.
Sep 15 '09 #2
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Sep 15 '09 #3
tlhintoq
3,525 Expert 2GB
Hi, I know this is a simple question and I've googled for the answers too many times now. I've tried every possible answers in my coding however, I still cannot seem to make it work. :'(

What I wanted to do in my coding is to be able to parse and store the value of <selects> and their <options> so that I would be able to use them later in my other functions. I decided to use structs (declared as array) as there can be many <select> in a webpage and use arraylist to save their options. So that when calling them, it would look like select[1].options[1] or select[1].options[2]... etc.
Great description of what you are planning. Less clear on the actual failure. "It doesn't work" is a bit vague. Are you unable to retrieve values? Are you getting exception errors?
Sep 15 '09 #4
GaryTexmo
1,501 Expert 1GB
@tlhintoq
It's kinda nested in the code, but there is a description of the failure...

//the error I get here is: structs cannot contain explicit parameterless constructors - I have absolutely no idea what parameter to put in there since in main, I also have selects[selectcount].selectname = selectname; and selects[selectcount].selectnumber = selectnumber; and a for(;;) selects[selectcount].options[i]=value;
It's a build error.
Sep 15 '09 #5
tlhintoq
3,525 Expert 2GB
I'm with Gary. Shift out of structs and into classes
Sep 15 '09 #6
Plater
7,872 Expert 4TB
Very rarely do I find a use for structs (except for dealing with pinvoke things, and certain byte[] <-> struct things).
I usually decide that a class would be much better. And then I usually end up deciding a DataTable is better then my own custom class.
Have you investigated the DataTable object?
Sep 15 '09 #7
i just would like to say thank you for all of you who replied. :D:D

I have converted the struct into class and somehow it's working now. :D i hope it'll always work after all the codes I am going to put in. XD

I will remember to use the [code] tag the next time I post in the forum. :) And thanks again for responding fast.

And Mr. Plater, im sorry but I'm a newbie to C# and haven't studied DataTable objects yet. :( But I will explore the wonders of C# soon enough. =)
Sep 16 '09 #8

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

Similar topics

1
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct...
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
33
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
3
by: Zenon | last post by:
I have a function which returns array of structs. I need to create a collection of those arrays and thought that an ArrayList would be a good way to do this since the count is variable. The...
3
by: Nyx18 | last post by:
what im trying to do is read in a data from a file into 4 different arrays then also read in the same data using array of structs. the assignment is to show that we know how to use both methods of...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.