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

Make my own class? Or just use an array?

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
4 1604
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: justin allen | last post by:
I'm wondering if there would be a way to do such a thing as overloading the () operator of a class in order to use that class as a callback function. I presently would love to do this with the...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
1
by: Esteban Felipe | last post by:
Hi, thanks for reading. I hope to find some help here before I commit suicide because this is driving me crazy. Please excuse me if this looks like a long post, but I hope that a complete...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
4
by: Chris F Clark | last post by:
Please excuse the length of this post, I am unfortunately long-winded, and don't know how to make my postings more brief. I have a C++ class library (and application generator, called Yacc++(r)...
5
by: Kyote | last post by:
Sorry, but I have no idea how to phrase the subject better than that. I've come across this a few different times and decided to ask in case there's a way to do it. It would simplify things a bit...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
9
by: Peri | last post by:
Dear All, I have written a common array class which has insert, search and other functions. For Example (Code in VB.NET): ' Client Class Public Class Client Public ClientCode As String ...
1
by: lia.leon | last post by:
Can anyone give me a simple example to demonstrate the captioned question? Actually, instead of PInvoke, we'd like to utilize the united .Net platform to support our requirement:- VB.Net sends...
12
by: raylopez99 | last post by:
I have an array that I wish to preserve as "read only". The array holds references to variables myObjects instantiated by new that change all the time. The variables are part of a class that I...
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
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.