473,385 Members | 2,269 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,385 software developers and data experts.

Finding a particular field from ArrayList of struct

Hi, question about using ArrayList from C# newbie,

I managed to make my array list which holds a number of structs:

struct myStruct
{
public int field1;
public int field2;
}

static ArrayList myArray = new ArrayList();

How could I find a struct that holds a particular value in field1?
IndexOf()?

Bob
Jul 30 '07 #1
4 1969
I wrote a method like:

Expand|Select|Wrap|Line Numbers
  1. int findStruct(ArrayList arr)
  2. {
  3.   int idx = -1;
  4.   for (int i = 0; i < arr.Count; i++)
  5.     if (((myStruct)arr[i]).id == 0x0302)
  6.       {
  7.       idx = i;
  8.       break;
  9.       }
  10.  
  11.        return idx;
  12. }
Any other fancy way to do a job?

Bob



Hi, question about using ArrayList from C# newbie,

I managed to make my array list which holds a number of structs:

struct myStruct
{
public int field1;
public int field2;
}

static ArrayList myArray = new ArrayList();

How could I find a struct that holds a particular value in field1?
IndexOf()?

Bob
Jul 30 '07 #2
RoninZA
78
You could use the IndexOf() method of the ArrayList:
Expand|Select|Wrap|Line Numbers
  1. myStruct struct = new MyStruct();
  2. struct.Field1 = 1; //or whatever int you're searching for
  3. struct.Field2 = 2; //ditto
  4.  
  5. int foundIndex = myArrayList.IndexOf(struct);
  6.  
Jul 30 '07 #3
You could use the IndexOf() method of the ArrayList:
Expand|Select|Wrap|Line Numbers
  1. myStruct struct = new MyStruct();
  2. struct.Field1 = 1; //or whatever int you're searching for
  3. struct.Field2 = 2; //ditto
  4.  
  5. int foundIndex = myArrayList.IndexOf(struct);
  6.  
Thanks for reply RoninZA,

I guess IndexOf() can be used as you suggested when I know all field values.
What if I know just one of the field and don't care other fields?

Bob
Jul 30 '07 #4
RoninZA
78
Best way would be to loop through the structure and try and find the index you are looking for.
Jul 31 '07 #5

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

Similar topics

6
by: Michael C | last post by:
Is it possible to use an ArrayList inside a struct? I keep running into a null reference exception when I try to Add to the ArrayList in the struct, and it won't let me initialize the ArrayList in...
1
by: Peter Schmitz | last post by:
Hi, I've got the following problem: I need to create a special Dll that is based on MFC, but also includes also managed code (which works fine and isn't a problem). The problem occurrs in the...
4
by: Jeoryos | last post by:
Well here is some code that will help explain the situation: I first make a struct that actually will be used to save some server informations: struct Server { public String IP; public int...
2
by: Extremest | last post by:
Here is the code I have so far. It connects to a db and grabs headers. It then sorts them into groups and then puts all the complete ones into another table. Problem I am having is that for some...
2
by: pengbsam | last post by:
Hello All: Having a problem with arraylist.copyto function. And here's a sample of my code: In global--> public struct point { int x; string y; } static public point point;
2
by: Fred Wilson | last post by:
Hello, How does one go about finding all the tables that have a particular field name? For example a field name "EVENT_CODE" I would like to find all the places in the database this is...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
17
by: abhimanyu.v | last post by:
Hi Guys, I have one doubt. The test program is given below. It uses two way of finding out the offset of a variable in structure. I executed the program and found the same result. My question...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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: 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,...

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.