By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,154 Members | 1,405 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,154 IT Pros & Developers. It's quick & easy.

Make my own class? Or just use an array?

P: n/a
I'm a bit confused hopefuly someone can help.

I have a text file which stores names and an associated number. Each
line in the text file represents one person, and a semicolon ' ; '
seperates person from associated number.

The associated number is used as a que position reference (i.e. the
person next has the lowest number.)

Here is an example: -

e.g. : filename: people.txt

Tod;232
Sarah;939
Jane;34
Natash;2

I need to do things like : -
- rename a persons name,
- move a person up in the que, or down in the que.

So far I've been using Arrays. To rename a person I would search an
array created from File.ReadAllLines - for an element beginning with
the name in question, split that line into name, and number using
element.split and then simply replace the name, and copy back into the
original array, and then back into the file.

However now i'm looking at moving a person up and down in the que
position I need to be able to search accross name,number pairs and
sort according to number.

Should I be createing my own class called que for instance, so that I
could then create my own methods that would do something like: -

Que.MoveUp(salesmanname)
and Que.MoveDown(salesmanname)

and Que.Rename(salesman original name, salesman new name)

etc...

If so how do i create my own class that works like that?

My main problem at the moment is that I'm finding it hard to sort the
array generated by File.ReadAllLines, according to the que number
(which appears at the end of each line).

I think it would be a lot cleaner if i had my own Que class, but I'm
not sure how to start making one.

Thanks,

Gary.

Feb 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2 Feb, 12:48, "Gary" <garyuse...@myway.comwrote:
I'm a bit confused hopefuly someone can help.

I have a text file which stores names and an associated number. Each
line in the text file represents one person, and a semicolon ' ; '
seperates person from associated number.

The associated number is used as a que position reference (i.e. the
person next has the lowest number.)

Here is an example: -

e.g. : filename: people.txt

Tod;232
Sarah;939
Jane;34
Natash;2

I need to do things like : -
- rename a persons name,
- move a person up in the que, or down in the que.

So far I've been using Arrays. To rename a person I would search an
array created from File.ReadAllLines - for an element beginning with
the name in question, split that line into name, and number using
element.split and then simply replace the name, and copy back into the
original array, and then back into the file.

However now i'm looking at moving a person up and down in the que
position I need to be able to search accross name,number pairs and
sort according to number.

Should I be createing my own class called que for instance, so that I
could then create my own methods that would do something like: -

Que.MoveUp(salesmanname)
and Que.MoveDown(salesmanname)

and Que.Rename(salesman original name, salesman new name)

etc...

If so how do i create my own class that works like that?

My main problem at the moment is that I'm finding it hard to sort the
array generated by File.ReadAllLines, according to the que number
(which appears at the end of each line).

I think it would be a lot cleaner if i had my own Que class, but I'm
not sure how to start making one.

Thanks,

Gary.
Try this: It covers sorting and inserting. If you look at the
overrides for sort you'll get a few more ideas as well.

private void button7_Click(object sender, System.EventArgs e)
{
ArrayList a = new ArrayList();
Item i = new Item();
i.Name = "person 1";
i.QueuePos = 2;
a.Add(i);
i = new Item();
i.Name = "Person 2";
i.QueuePos = 1;
a.Add(i);
i = new Item();
i.Name = "Person 3";
i.QueuePos = 3;
a.Insert(0,i);
a.Sort();
Console.WriteLine(".");
}

public class Item : IComparable
{
public string Name;
public int QueuePos;

public int CompareTo(object obj)
{
if(QueuePos ((Item)obj).QueuePos)
{
return 1;
}
if(QueuePos < ((Item)obj).QueuePos)
{
return -1;
}
return 0;
}

}

Feb 2 '07 #2

P: n/a
I fleshed it out a little to show how to control things like direction
of sort. This is all framework 1.1, in 2 it's worth looking at the
generic collections.

private void button7_Click(object sender, System.EventArgs e)
{
ArrayList a = new ArrayList();
Item i = new Item();
i.Name = "person 1";
i.QueuePos = 2;
a.Add(i);
i = new Item();
i.Name = "Person 2";
i.QueuePos = 1;
a.Add(i);
i = new Item();
i.Name = "Person 3";
i.QueuePos = 3;
a.Insert(0,i);
a.Sort();
a.Sort(new MyComparer(true));
a.Sort(new MyComparer(false));
Item[] export = (Item[])a.ToArray(i.GetType());
Console.WriteLine(".");
}

public class MyComparer : IComparer
{
private int _forward;
public MyComparer(bool pForward)
{
Forward = pForward;
}
public bool Forward
{
set
{
if (value)
{_forward = -1;}
else
{_forward = 1;}
}
get
{return(_forward == -1);}
}
public int Compare(object x, object y)
{
Item one = (Item) x;
Item two = (Item) y;
if(one.QueuePos two.QueuePos)
{return 1 * _forward;}
if(one.QueuePos two.QueuePos)
{return -1 * _forward;}
return 0;
}
}

public class Item : IComparable
{
public string Name;
public int QueuePos;

public int CompareTo(object obj)
{
if(QueuePos ((Item)obj).QueuePos)
{
return 1;
}
if(QueuePos < ((Item)obj).QueuePos)
{
return -1;
}
return 0;
}

}

Feb 2 '07 #3

P: n/a
On 2 Feb, 13:53, "DeveloperX" <nntp...@operamail.comwrote:
This is all framework 1.1, in 2 it's worth looking at the
generic collections.
To extend DeveloperX's suggestion for framework 2.0+, specifically the
SortedList<class sounds like it should do what you want. Since your
'key' is ostensibly an int, you can rely on the default comparer
[terminology??] to sort the people for you. Then you just need to
deal with incrementing/decrementing people's keys.

Something like this:

SortedList<int, stringpeople = new SortedList<int,
string>();

people.Add(232, "Tod");
people.Add(939, "Sarah");
people.Add(34, "Jane");
people.Add(2, "Natash");

// Loop through each person
foreach(KeyValuePair<int,stringperson in people)
{
// ...
}

// Retrieve a specific person by Id
string name = people[34];

// Retrieve a specific person by name
int id = people.Keys[people.IndexOfValue("Tod")];

// Remove a specific person by Id
people.Remove(34);

Feb 2 '07 #4

P: n/a
Thankyou both very much, i'm getting back to my c# project in a couple
of days and will investigate your very kind comments. DeveloperX
thanks for such the extensive comments, and Bobbo thanks for your
wonderful comments to.

Gary.

On Feb 2, 3:31 pm, "Bobbo" <robin.willi...@choicequote.co.ukwrote:
On 2 Feb, 13:53, "DeveloperX" <nntp...@operamail.comwrote:
This is all framework 1.1, in 2 it's worth looking at the
generic collections.

To extend DeveloperX's suggestion for framework 2.0+, specifically the
SortedList<class sounds like it should do what you want. Since your
'key' is ostensibly an int, you can rely on the default comparer
[terminology??] to sort the people for you. Then you just need to
deal with incrementing/decrementing people's keys.

Something like this:

SortedList<int, stringpeople = new SortedList<int,
string>();

people.Add(232, "Tod");
people.Add(939, "Sarah");
people.Add(34, "Jane");
people.Add(2, "Natash");

// Loop through each person
foreach(KeyValuePair<int,stringperson in people)
{
// ...
}

// Retrieve a specific person by Id
string name = people[34];

// Retrieve a specific person by name
int id = people.Keys[people.IndexOfValue("Tod")];

// Remove a specific person by Id
people.Remove(34);

Feb 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.