Yakyb

I have been working in C# for a couple of weeks now so do tell me if im doing something incorrectly.

i have setup a small program that uses three arraylists Build1,Build2,Build3

they all contain a objects i have named Part

with data (string Name ,string type, double Cost, int Amount, double RelPerf)

i can fill the arrays with parts using my GUI and remove parts w/o an issue

what i need to do now is to create a new build.

i need to filter each Build(1,2,3) for the lowest double value for Cost

i.e

search each build(1,2,3) for Part.type = a

find which build contains the Part.type = a with the lowest value for Part.cost

and add that part to a new ArrayList

i then need to repeat this for Part.type b,c,d,e, etc

i was considering using a foreach statement but im not entirely sure how to implement this

any help would be greatly appreciated

thanks

Yakyb



Re: Visual C# General couple of questions arraylist

Jim de la Bastide

Firstly, are you using .NET 2.0 If you are you may want to think about using a generic list instead of an arraylist:

List build1 = new List();

It function similarly to the arraylist where T is any object (string, int, your own custom object). The nice this about using this generic is that it is type safe (ie if you have a List of strings you cannot add an int to the list and will get a build error). Anyway a bit off topic.

If you cannot filter your data at the database level and must do it in code, you can use a for loop to go through all of the lists and get what you are looking for. I would definitely do some performance testing on it since it sounds like teh lists can be fairly large and while one implementation may work it may be slow and there may be a more efficient way of reprenting the data. Here is a very brief snippet of a foreach loop. Assume Build is an object and Parts is a custom object that is in a collection within the Build object:

Code Snippet

double lowestPrice = 0;

foreach (Part currentPart in build1.Parts)

{

double partPrice = currentPart.Price;

foreach (Part build2Part in build2.Parts)

{

double build2PartPrice = build2Part.Price;

if (build2PartPrice < partPrice)

{

lowestPrice = build2PartPrice;

break;

}

}

}

Again this is just a sample of the foreach loop. I highly doubt your actual algorithm will look like this. The break is there if you know that the the price is the lowest and you can stop looping. Of course you can order all of the objects by using sort on the list and implementing IComparable. Then you can look at the first price in each list and check that way. Additional IComparable information: http://msdn2.microsoft.com/en-us/library/system.icomparable(VS.71).aspx

Example:

Code Snippet

public class RepositoryLogEntry : IComparable<RepositoryLogEntry>

{

///

/// Sort the item in reverse order based on the revision number

/// (ie the highest revision number first).

///

///

///

public int CompareTo(RepositoryLogEntry other)

{

// The - negates the compareTo to ensure that

// the highest number is first in the list

return -_revision.CompareTo(other.Revision);

}

}





Re: Visual C# General couple of questions arraylist

Yakyb

thanks for your quick response

yeah im using 2.0 on VS2005 i will probably change to a List but the course i went on used arraylists so i stuck with them

whilst i see what your doing it doesnt quite answer the question

heres a bit more info

each list will only have maximum 10 items in it

each type of part will be different in the list

(essentially what im doing is creating a shopping list for computer parts in a single PC and making a comparison of them)

what i need to do is search each list for the part that is cheapest and return a mixture of parts from each build that is the cheapest. i.e CPU from build1 is cheapest whilst GPU from build2 is cheapest and HDD from build 3 is cheapest

then enter these cheapest parts and return them in build 4

there is no database behind this ATM

thanks again for any help on this subject as i said im pretty new to it





Re: Visual C# General couple of questions arraylist

Gregor Berginc

You start with three lists of Parts and would like to prepare another list with cheapest parts of each type you need. What if you prepare an enumeration of all parts from all three lists, e.g,

Code Snippet

public IEnumerable<Part> Parts()

{

foreach (Part part in this.build1)

{

yield return part;

} // foreach


foreach (Part part in this.build2)

{

yield return part;

} // foreach


foreach (Part part in this.build3)

{

yield return part;

} // foreach

} // Parts



Then, find the cheapest part for a type A:

Code Snippet

string partType = "A";

double cheapestCost = Double.Max;

Part cheapestPart = null;


foreach (Part part in this.Parts())

{

if ((part.Type == partType) && (part.Cost < cheapestCost))

{

cheapestCost = part.Cost;

cheapestPart = part;

} // if

} // foreach


// Use cheapestPart to build new configuration.


HTH,







Re: Visual C# General couple of questions arraylist

Mario Cossi

Yakyb,

as already said in this thread, ArrayLists are more or less obsolete in .NET 2.0. Another interesting feature is the list of generic methods available for Arrays (FindIndex<T>, for instance, which allows you to express complex finds quite easily). Anyway, this is just a suggestion to keep looking at all the tools that .NET makes available.

To answer your question:

assuming you have a class like:

public class Component {

public string Type;

public string Name;

public double Cost;

...

}

(yes... good practice would be to use private fields and provide public properties to access them. I'm not being sloppy, I'm just trying to keep this short).

First, we need a function to locate a component with a given part in an ArrayList.

public static Component FindComponentByType (ArrayList list, string type) {

foreach (Component c in list) {

if (c.Type == type) {

return c;

}

}

return null;

}

(the search is strictly linear. While it is possible to do better, it's not really worth the effort for just 10 elements)

Then we need to find the lowest price across several ArrayLists.

public static GetLowestPrice (ArrayList [] lists, string type) {

Component result = null;

foreach (ArrayList list in lists) {

Component component = FindComponentByType (list, type);

if (component != null) {

if ((result == null) || (component.Cost < result.Cost)) {

result = component;

}

}

}

return result;

}

Putting it all together:

public static void Main (string [] args) {

ArrayList part1 = new ArrayList ();

ArrayList part2 = new ArrayList ();

ArrayList part3 = new ArrayList ();

... fill the lists

ArrayList [] lists = new ArrayList [] { part1, part2, part3 };

ArrayList cheapest = new ArrayList ();

foreach (Component component in lists [0]) { // choose one of the lists, to get all the parts you need

cheapest.Add (GetLowestPrice (lists, component.Type));

}

}

Definitely a bit messy. But it should work fine as a beginning.

HTH

--mc





Re: Visual C# General couple of questions arraylist

Yakyb

foreach (Part part in this.Parts())

{

if ((part.Type == partType) && (part.Cost < cheapestCost))

{



thanks gregor this was a great help only a small issue tho

the line above is bringing the error

Error 1 Operator '&&' cannot be applied to operands of type 'string' and 'bool'

any ideas

the help section states

A binary operator is operating on data types for which it was not designed


edit doesnt matter its sorted now thanks for the help guys