Re: Arrays - Which One?
From: Mark Givens (site_developer_at_yahoo.com)
Date: 08/19/04
- Next message: Tony Caduto: "Having a problem with some socket code"
- Previous message: Darryn Ross: "Re: KeyPress Event not Firing???"
- In reply to: Mark Givens: "Re: Arrays - Which One?"
- Next in thread: Jon Skeet [C# MVP]: "Re: Arrays - Which One?"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 19 Aug 2004 03:30:28 GMT
Ok, getting closer ...
After my for each loops, I added...
conversions.Sort(new mysort());
and got rid of ...for now, the following. Not really interested in
seein the results ...can make that happen whenever everything else is
ok.
mysort s = new mysort();
Array.Sort(c,s); // ERRORS (The name "c" does not exist...)
foreach (conversions s in c)
{
// output results to textbox
this.textBoxResults.Text += s.ToString() +
"\r\n--------------------------------------------------------\r\n";
}
return 0;
}
-------------------------
It compiles, but IComparer throws an exception when it tries to work.
On Wed, 18 Aug 2004 23:55:01 GMT, Mark Givens
<site_developer@yahoo.com> wrote:
>Hi Jon, Thanks for the example. I tried to use it, but for some
>reason could not get it to work, and so I ventured down the IComparer
>road.
>
>I seem to have it almost working, but am having difficulty getting my
>array with all the calculated conversions in it to be recognized
>before it is sent to be compared. Hope that made sense.
>
>Portion that errors:
> mysort s = new mysort();
> Array.Sort(c,s); // ERRORS (The name "c" does not exist...)
> foreach (conversions s in c)
> snip...
>
>I guess I need to assign a value to my array???? Below is what I
>have....Thanks again for the help.
>
>Mark
>
>
>private void ApplyCalculations()
>{
>
>------------------------------snip------------------------------
>-----------Lots of pre-calculate stuff was here-----------
>------------------------------snip------------------------------
>
>// place header in textbox
>
>this.textBoxResults.Text = "RATIO TIME
>\r\n------------------------\r\n";
>
>
>// create array
>
> ArrayList conversions = new ArrayList();
>
>// run inputs through loops for each gear1/gear2 combination
>
> for (int i = 0; i < gearOneSteps; i++)
> {
> for (int x = 0; x < gearTworSteps; x++)
> {
>
>------------------------------snip------------------------------
>------------ Lots of "do logic" stuff was here--------------
>------------------------------snip------------------------------
>
> // add results to array
> conversions.Add( RATIO + "|" + TIME);
>
> } // end inside for loop
> } // end outside for loop
>
>
> mysort s = new mysort();
> Array.Sort(c,s); // ERRORS (The name "c" does not exist...)
> foreach (conversions s in c)
> {
>
>// output results to textbox
>
> this.textBoxResults.Text += s.ToString() +
>"\r\n--------------------------------------------------------\r\n";
> }
> return 0;
>
> }
>}
>
>
>---------------------------------------------------------------
>---------Below appears to be valid - No errors---------
>---------------------------------------------------------------
>
>public class mysort : IComparer
>{
> public int Compare ( object a, object b )
> {
> int m1 = ((conversions)a).mph ;
> int m2 = ((conversions)b).mph ;
> int r1 = ((conversions)a).ratio ;
> int r2 = ((conversions)b).ratio ;
>
> if ( m1 == m2 )
> {
> return r1.CompareTo(r2) ;
> }
>
> if ( m1 < m2 )
> {
> return -1 ;
> }
> else
> {
> return 1 ;
> }
> }
>
> class conversions
> {
> public int ratio ;
> public int mph ;
>
> public conversions ( int m, int r)
> {
> mph = m ;
> ratio = r ;
> }
>
> public new string ToString()
> {
> return mph + " " + ratio ;
> }
> }
>
>}
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>On Wed, 18 Aug 2004 12:15:55 +0100, Jon Skeet [C# MVP]
><skeet@pobox.com> wrote:
>
>>Mark Givens <site_developer@yahoo.com> wrote:
>>> The fields represent gears and ratios and time to complete.
>>>
>>> Column 1: Gear 1
>>> Coumnl 2: Gear 2
>>> Column 3: Ratio of 1 and 2
>>> Column 4: Time to complete based on gears
>>
>>Right. That very much sounds like something you should be
>>encapsulating.
>>
>>> Field4 is the field I need to sort by ...the number of records/rows is
>>> determined by what gears the user selects for fields 1 and 2.
>>> Performing the calculatons is no problem.
>>>
>>> Have read a bit about IComparer and it sounds like something that may
>>> do the trick, but have no idea about making each an instance of a
>>> custom type ...sorry, but some parts of C# is Greek to me, but I am
>>> learning!
>>
>>Well, there are two options, really:
>>
>>1) implement IComparer in a class which can compare a gear pair.
>>
>>2) implement IComparable in your GearPair (or whatever) class
>>
>>IComparer is used to compare other objects, IComparable is used to
>>compare the implementing class with something else (usually another
>>instance of the same class).
>>
>>It could well be that implementing IComparable will be simpler for you.
>>You could have a class such as:
>>
>>public class GearPair : IComparable
>>{
>> int first;
>> /// <summary>
>> /// The number of teeth on the first gear
>> /// </summary>
>> public int First
>> {
>> get { return first; }
>> set { first = value; }
>> }
>>
>> int second;
>> /// <summary>
>> /// The number of teeth on the second gear
>> /// </summary>
>> public int Second
>> {
>> get { return second; }
>> set { second = value; }
>> }
>>
>> /// <summary>
>> /// Ratio of first to second
>> /// </summary>
>> public double Ratio
>> {
>> get { return first/second; }
>> }
>>
>> /// <summary>
>> /// Time to complete
>> /// </summary>
>> public TimeSpan Time
>> {
>> // Just an example - I don't know what your logic is
>> get { return TimeSpan.FromMilliseconds(Ratio*10000); }
>> }
>>
>> public GearPair (int first, int second)
>> {
>> this.first = first;
>> this.second = second;
>> }
>>
>> public int CompareTo (object obj)
>> {
>> GearPair other = obj as GearPair;
>> if (other==null)
>> {
>> throw new ArgumentException("obj");
>> }
>>
>> return Time.CompareTo(other.Time);
>> }
>>}
- Next message: Tony Caduto: "Having a problem with some socket code"
- Previous message: Darryn Ross: "Re: KeyPress Event not Firing???"
- In reply to: Mark Givens: "Re: Arrays - Which One?"
- Next in thread: Jon Skeet [C# MVP]: "Re: Arrays - Which One?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|