473,938 Members | 14,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ReadAllLin es - 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(sale smanname)
and Que.MoveDown(sa lesmanname)

and Que.Rename(sale sman 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.ReadAllLin es, 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 1628
On 2 Feb, 12:48, "Gary" <garyuse...@myw ay.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.ReadAllLin es - 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(sale smanname)
and Que.MoveDown(sa lesmanname)

and Que.Rename(sale sman 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.ReadAllLin es, 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(o bject sender, System.EventArg s 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.WriteLi ne(".");
}

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

public int CompareTo(objec t obj)
{
if(QueuePos ((Item)obj).Que uePos)
{
return 1;
}
if(QueuePos < ((Item)obj).Que uePos)
{
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(o bject sender, System.EventArg s 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(fals e));
Item[] export = (Item[])a.ToArray(i.Ge tType());
Console.WriteLi ne(".");
}

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(_forwar d == -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(objec t obj)
{
if(QueuePos ((Item)obj).Que uePos)
{
return 1;
}
if(QueuePos < ((Item)obj).Que uePos)
{
return -1;
}
return 0;
}

}

Feb 2 '07 #3
On 2 Feb, 13:53, "DeveloperX " <nntp...@operam ail.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<clas s 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(KeyValu ePair<int,strin gperson in people)
{
// ...
}

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

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

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

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...@operam ail.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<clas s 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(KeyValu ePair<int,strin gperson in people)
{
// ...
}

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

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

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

Feb 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1648
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 usort function in order to easily pass runtime information to the custom sort callback function. It has been my habit in the past(and with other languages) to do this by using a class for my callback, setting the information in the constructor(or...
10
4872
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
1470
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 explanation help you to help me :=) ....Let's start with some background: ..- I'm building an asp.net application that requires users to upload text files in cvs format with data exported from an AS-400. ..- The files will be something between 800kb...
6
4919
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
4
2143
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) and the Language Objects Library) that I have converted over to C#. It works okay. However, in the C# version, one has to build the class library into the generated application, because I haven't structured this one thing right. I would like to...
5
1323
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 and I figured if anyone knows, more than likely that someone is here. I have a class for storing filenames and it's path along with a little more info. Here it is Public Class myFiles Public oldFileName As String
19
2464
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; //every class may have this statement self.hello = function() {
9
1512
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 'Key Field
1
1563
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 a structure (includes 3-dimensional array) to C++/CLI Dll, and the C++/CLI Dll will return a structure (includes 2- dimensional array) back to VB.Net for future handling
12
2509
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 used ICloneable on, namely "Clone();" (deep and/or shallow copies worked the same for this particular class). Using ICloneable, I am able to successfully make a copy of the variables like so:
0
10134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11524
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10657
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9857
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7384
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3501
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.