473,387 Members | 1,766 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,387 software developers and data experts.

C# - Selecting specific items in an array element with multiple values

Hello all,

I'm new to C# and I'm hitting a brick wall on this.

I've skimmed through the posts regarding arrays and their elements and was unable to find the information I was looking for, so hopefully I'm not blind and someone will be kind enough to help me out :)

Using a standard 2D array I need to assign it a new class instance that contains several values. My setup needs to be like below, so dictionaries etc. won't help me much and they're a bit over my head right now anyway.

object[] testArray = new object[2];

testArray[0] = new className(100, 1.5, 3);
testArray[1] = new anotherClass(200, 3.5, 4);

I need to be able to go through each array index and pull out the third item in each element of that index. So I would need to pull out the 3 or the 4 for each of these two indexes.

I've tried running through the Array methods, but haven't come up with anything so far that I could use. I'm not looking for anyone to code it for me, as that would be silly, but if you could provide a point in the right direction that would be great :)

Thanks in advance!
Oct 17 '07 #1
2 6527

object[] testArray = new object[2];
testArray[0] = new className(100, 1.5, 3);
testArray[1] = new anotherClass(200, 3.5, 4);
Hello CyOnide!

1.If both of classes contain three values of the same type(looking at their constructors it seems so) you should use one class (f.e. object).
Expand|Select|Wrap|Line Numbers
  1. class object
  2. {
  3.     public:
  4.        object::object(int v1,double v2,int v3):val1(v1),val2(v2),val3(v3){} //constructor 
  5.        int val1;
  6.        double val2;
  7.        int val3;   //class fields
  8. };
  9.  
2. If it is not so - classes className and anotherClass have to be derived from class object if you want to use them as testArray's array fields, because the array's elements type is object.
Expand|Select|Wrap|Line Numbers
  1. class className: public object 
  2. {
  3.      public:
  4.         className::className(int v1, double v2, int v3):object(v1,v2,v3); //constructor
  5.         .....//other fields or methods
  6. };
  7.  
  8. class anotherClass: public object
  9. {
  10. //------------------------//the same as in className
  11. };
  12.  
3. To reach third value of each class from array just use next code
Expand|Select|Wrap|Line Numbers
  1. testArray[i]->val3;
  2.  
Good luck!
Oct 18 '07 #2
Shashi Sadasivan
1,435 Expert 1GB
since the array is storing objects rahter than another set of arrays or any properly structured class, it will return an object type which you have to cast back to its original class.
If you make all the classes that you are entering into this array inherit an Interface which will have a method of fetching the 3rd value, then you can avoid the speggeti code that you would otherwise be entering into.

cheers and good luck
Oct 18 '07 #3

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

Similar topics

2
by: Bob Rivers | last post by:
Hi, Is it possible to verify, using js, if ANY item in a form has changed? For example: I have a <form> with multiple items (<input>, <select>, <textarea>, etc). I need to track if one (or...
3
by: james.dixon | last post by:
Hi I was wondering if anyone else had had this problem before (can't find anything on the web about it). I have three select elements (list boxes - from here on I'll refer to them as 'the...
8
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If...
4
by: bienwell | last post by:
Hi all, Data displayed on the datalist control is bound by the column name of the dataset like this : <%# DataBinder.Eval(Container.DataItem, "title")%> Could I use an element of the array...
1
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
2
by: MrL8Knight | last post by:
I am building a simple shopping cart and I am having problems trying to add the costs of the items to generate a total cost. I am new at this so forgive me if my technical verbiage isn’t the...
22
by: guitarromantic | last post by:
Hey everyone, I run a site with staff-submitted reviews, and most of them are written by one author. However, we also do "multiple" reviews. Up until now I just had a userid for a 'Multiple'...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.