Indexed class / list

Z

Zach

In the code below two indexed classes are created, viz: People and Jobs.
I would like to merge the two classes into one class called Employee. I
have tried using a List, rather than an array for data, but cannot find
out how to do that. I am aware that I can simply create a List and fill
it with employee objects, but that is not what I am trying to find out
how to do. Could anyone please help me out?

using System;
using System.Collections.Generic;
using System.Text;

namespace IndexedClass
{
class Program
{
static void Main(string[] args)
{
int index_size = 2;
People person = new People(index_size); Jobs job = new
Jobs(index_size);
person[0] = "Jones"; job[0] = "waiter";
person[1] = "Smith"; job[1] = "fitter";
Console.WriteLine(person[0] + "is a " + job[0]);
Console.WriteLine(person[1] + "is a " + job[1]);
Console.ReadKey();
}
}
public struct People
{
private string[] data;
public string this[int Position] { get { return data[Position];
} set { data[Position] = value; } }

public People(int index_size)
{
data = new string[index_size];
}
}
public struct Jobs
{
private string[] data;
public string this[int Position] { get { return data[Position];
} set { data[Position] = value; } }
public Jobs(int index_size)
{
data = new string[index_size];
}
}
}
 
Z

Zach

What _are_ you "trying to find out how to do" then? Your post is
surprisingly devoid of an actual question.

You can index List<T> objects just like arrays. The main difference
between the two is that since an array is fixed-size, its elements
always exist as soon as it's created, while with a List<T> any given
indexed element does not exist until enough elements have been added to
the List<T> instance.

Unfortunately, you did not describe what it is you tried, nor why that
attempt failed to produce the results you wanted, nor have you actually
described any specific problem at all. So it's impossible to really know
what it is you want.

But if all you're having trouble with is indexing an element in the
List<T> that doesn't exist yet, the answer to that is to make sure
you've added enough elements to the List<T> for the List<T> element to
exist (you can initialize elements with "null" before you have actual
string objects to store).

If that doesn't answer your question, trying being specific.

Pete
My question was how top lump the two indexed objects in my trial example
together into one object. (Formulating questions doesn't appear to be my
strongest skill. Sorry.)
Zach.
 
J

Jeff Johnson

My question was how top lump the two indexed objects in my trial example
together into one object. (Formulating questions doesn't appear to be my
strongest skill. Sorry.)

But HOW??? What is the new object supposed to look like? How does the data
in one list correlate to the data in the other?
 
Z

Zach

But HOW??? What is the new object supposed to look like? How does the data
in one list correlate to the data in the other?
What is the new object supposed to look like: that is precisely my question.

How does the data in one list correlate with the data in the other: I am
not on about LISTS, I am wanting to lump the two indexed structs
together int an employee object. So the Employee struct will incorporate
a person and a job.

Zach.
 
J

Jeff Johnson

What is the new object supposed to look like: that is precisely my
question.

How does the data in one list correlate with the data in the other: I am
not on about LISTS, I am wanting to lump the two indexed structs together
int an employee object. So the Employee struct will incorporate a person
and a job.

Okay, so I read your original code sample closer. Is it really this simple:

public struct Employee
{
public string Name;
public string Job;
}

and then you just make an array of Employees???

If not, then I STILL don't understand what you want. Also, I have no idea
why you're fixated on structs. C# philosophy leans heavily toward classes
and away from stucts.
 
R

Rick Lones

An item in one list contains a reference to an item in the other. That is how
it's normally done and that is quite different from lumping them together, which
really would make no sense for a whole lot of reasons. Your Person instance
could contain a field which references a Job instance. That would be the
"correlation" you are looking for.
What is the new object supposed to look like: that is precisely my question.

How does the data in one list correlate with the data in the other: I am not on
about LISTS, I am wanting to lump the two indexed structs together int an
employee object. So the Employee struct will incorporate a person and a job.

Very simplistic hint:

class Employee
{
int employeeID;
string employeeName;
. . . etc, and somewhere in there would be:
Job employeeJob; // not a whole Job instance, it's only a reference!
}

class Job
{
int jobID;
string jobTitle;
. . . etc;
}

List<Employee> employees;
List<Job> jobs;

There are many other and more sophisticated ways to organize this kind of data,
but I am sensing that you are not yet experienced enough to be anything but
confused by them. So I would keep it simple, even though that means you will
have to deal with searching the Job list to find the appropriate record which
contains the job title you want to assign your employee.

Also, notice I am using classes rather than structs - and you should be as well.
Ask your professor why, that's what (s)he is there for.

HTH,
-rick-
 
Z

Zach

An item in one list contains a reference to an item in the other. That
is how it's normally done and that is quite different from lumping them
together, which really would make no sense for a whole lot of reasons.
Your Person instance could contain a field which references a Job
instance. That would be the "correlation" you are looking for.


Very simplistic hint:

class Employee
{
int employeeID;
string employeeName;
. . . etc, and somewhere in there would be:
Job employeeJob; // not a whole Job instance, it's only a reference!
}

class Job
{
int jobID;
string jobTitle;
. . . etc;
}

List<Employee> employees;
List<Job> jobs;

There are many other and more sophisticated ways to organize this kind
of data, but I am sensing that you are not yet experienced enough to be
anything but confused by them. So I would keep it simple, even though
that means you will have to deal with searching the Job list to find the
appropriate record which contains the job title you want to assign your
employee.

Also, notice I am using classes rather than structs - and you should be
as well. Ask your professor why, that's what (s)he is there for.

HTH,
-rick-

Well, well young man.
 
Z

Zach

Okay, so I read your original code sample closer. Is it really this simple:

public struct Employee
{
public string Name;
public string Job;
}

and then you just make an array of Employees???

If not, then I STILL don't understand what you want. Also, I have no idea
why you're fixated on structs. C# philosophy leans heavily toward classes
and away from stucts.

As I said in my query: "I am aware that I can simply create a List and
fill it with employee objects, but that is not what I am trying to find
out how to do."

I simply asked for help to create an indexed class called Employee, that
allows person and job to be set for each instance of the class,
that is all! I am sorry, your suggestion doesn't seem to answer my query.

I happened to use structs in the example, but that fact is irrelevant in
the context of my query. For your information, I never use them anyway.
But for the sake of the argument, I could refer you to literature which
says they are faster.
 
R

Registered User

- snip -
I simply asked for help to create an indexed class called ...
- snip -
I've been following this thread and from the beginning it has not been
clear what you mean by 'an indexed class'. The code in the original
post used indexes into arrays. The contents are only indexed by virtue
of the container.
I would like to put my question to bed now because there doesn't appear
to be an answer to it.
Perhaps you mean a class which makes use of indexers.
<http://msdn.microsoft.com/en-us/library/2549tw02(v=VS.100).aspx>

regards
A.G.
 
J

Jeff Johnson

- snip -
I've been following this thread and from the beginning it has not been
clear what you mean by 'an indexed class'. The code in the original
post used indexes into arrays. The contents are only indexed by virtue
of the container.

Yes, yes! Zach, PLEASE explain what you mean by "indexed class." That should
go a long way towards helping us answer your question.
 
A

Arne Vajhøj

As I said in my query: "I am aware that I can simply create a List and
fill it with employee objects, but that is not what I am trying to find
out how to do."

I simply asked for help to create an indexed class called Employee, that
allows person and job to be set for each instance of the class,
that is all! I am sorry, your suggestion doesn't seem to answer my query.

It is not very clear what you want.

But maybe you are looking for something like this:

using System;
using System.Collections.Generic;

namespace E
{
public class Person
{
public string Name { get; set; }
public string Address { get; set; }
public override string ToString() { return "{" + Name + "," +
Address + ")"; }
}
public class Job
{
public string Title { get; set; }
public decimal Salary { get; set; }
public override string ToString() { return "{" + Title + "," +
Salary + ")"; }
}
public class PersonList
{
private List<Person> data = new List<Person>();
public void Add(Person p)
{
data.Add(p);
}
public Person this[int ix]
{
get { return data[ix]; }
}
}
public class JobList
{
private List<Job> data = new List<Job>();
public void Add(Job j)
{
data.Add(j);
}
public Job this[int ix]
{
get { return data[ix]; }
}
}
public class Index
{
public int Ix { get; set; }
}
public class PersonIndex : Index { }
public class JobIndex : Index { }
public class EmployeeList
{
private PersonList persons = new PersonList();
private JobList jobs = new JobList();
public void Add(Person p, Job j)
{
persons.Add(p);
jobs.Add(j);
}
public Person GetPerson(int ix)
{
return persons[ix];
}
public Job GetJob(int ix)
{
return jobs[ix];
}
public Person this[PersonIndex ix]
{
get { return GetPerson(ix.Ix); }
}
public Job this[JobIndex ix]
{
get { return GetJob(ix.Ix); }
}
}
public class Program
{
public static void Main(string[] args)
{
EmployeeList lst = new EmployeeList();
lst.Add(new Person{ Name="A A", Address="A road 123"}, new
Job{ Title="Developer", Salary=10000.00m});
lst.Add(new Person{ Name="B B", Address="B ave 456"}, new
Job{ Title="Tester", Salary=9000.00m});
Console.WriteLine(lst.GetPerson(0));
Console.WriteLine(lst.GetJob(1));
Console.WriteLine(lst[new PersonIndex{ Ix=0 }]);
Console.WriteLine(lst[new JobIndex{ Ix=1 }]);
Console.ReadKey();
}
}
}

Arne
 
A

Arne Vajhøj

As I said in my query: "I am aware that I can simply create a List and
fill it with employee objects, but that is not what I am trying to find
out how to do."

I simply asked for help to create an indexed class called Employee, that
allows person and job to be set for each instance of the class,
that is all! I am sorry, your suggestion doesn't seem to answer my query.

It is not very clear what you want.

But maybe you are looking for something like this:

using System;
using System.Collections.Generic;

namespace E
{
public class Person
{
public string Name { get; set; }
public string Address { get; set; }
public override string ToString() { return "{" + Name + "," + Address +
")"; }
}
public class Job
{
public string Title { get; set; }
public decimal Salary { get; set; }
public override string ToString() { return "{" + Title + "," + Salary +
")"; }
}
public class PersonList
{
private List<Person> data = new List<Person>();
public void Add(Person p)
{
data.Add(p);
}
public Person this[int ix]
{
get { return data[ix]; }
}
}
public class JobList
{
private List<Job> data = new List<Job>();
public void Add(Job j)
{
data.Add(j);
}
public Job this[int ix]
{
get { return data[ix]; }
}
}
public class Index
{
public int Ix { get; set; }
}
public class PersonIndex : Index { }
public class JobIndex : Index { }
public class EmployeeList
{
private PersonList persons = new PersonList();
private JobList jobs = new JobList();
public void Add(Person p, Job j)
{
persons.Add(p);
jobs.Add(j);
}
public Person GetPerson(int ix)
{
return persons[ix];
}
public Job GetJob(int ix)
{
return jobs[ix];
}
public Person this[PersonIndex ix]
{
get { return GetPerson(ix.Ix); }
}
public Job this[JobIndex ix]
{
get { return GetJob(ix.Ix); }
}
}
public class Program
{
public static void Main(string[] args)
{
EmployeeList lst = new EmployeeList();
lst.Add(new Person{ Name="A A", Address="A road 123"}, new Job{
Title="Developer", Salary=10000.00m});
lst.Add(new Person{ Name="B B", Address="B ave 456"}, new Job{
Title="Tester", Salary=9000.00m});
Console.WriteLine(lst.GetPerson(0));
Console.WriteLine(lst.GetJob(1));
Console.WriteLine(lst[new PersonIndex{ Ix=0 }]);
Console.WriteLine(lst[new JobIndex{ Ix=1 }]);
Console.ReadKey();
}
}
}

Note that these classes are an absolutely horrible OO design
and should never ever be used.

Arne
 
A

Arne Vajhøj

I happened to use structs in the example, but that fact is irrelevant in
the context of my query. For your information, I never use them anyway.
But for the sake of the argument, I could refer you to literature which
says they are faster.

Generally preferring structs over classes for performance reasons
i a very bad practice.

Arne
 
Z

Zach

Yes, yes! Zach, PLEASE explain what you mean by "indexed class." That should
go a long way towards helping us answer your question.
Schematically it would allow doing like so:

Employee emp = new Employee(index_size);

emp[0] <--- input the value for person and job
emp[1] <--- input the value for person and job
etc.

elsewhere:

emp[0] ---> output the value for person and job
emp[1] ---> output the value for person and job
etc.

"input" and "output" are fixations :)

Regards,
Zach.
 
Z

Zach

Please define "fixations". All of the accepted meanings (i.e. those
found in a dictionary of the English language) have no relevance to this
context.

A fixation - for a psychologist / psychotherapist - is a pathological
focus on something. With "input" and "output" are fixations" I was
simply poking a little fun at Jef, who write: "... I have no idea why
you're fixated on structs ..." (The subject of structs is irrelevant in
the context of my query; apart from having a bit of fun, I waned to
inoculate my text against more gratuitous ribaldry.)
 
Z

Zach

Yes, I'm aware of that dictionary definition. It did not seem to have
any relevance to the actual programming question we're working to
address here.

For that matter, it still doesn't seem to. At least, not to me.


Okay. Well, that was all very interesting. But what about the actual
question?

Have you given up on the idea of providing a more precise question?

Pete

"... Have you given up on the idea of providing a more precise
question? ..."

Pete, I am very grateful for your response, and for those of the others,
however, before getting back to you I need to digest what has been
written, and to have a good think about it all, because up until now my
endeavors to explain what I would like to achieve appear to have been
ineffective. However, if the essence of the miscommunication is the term
"indexed class" that I have been using, by it I mean a user defined
class[index]. Zach.
 
Z

Zach

Whatever the hell that means. I think we are done here.

My professor of "iformatica" as the good man was called at the
university where I did one of my degrees (he could multiply big numbers
doing funny things with both his hands :) will have died some twenty
years ago. So your "Ask your professor why" stuff is rather hilarious.
And as to "... sensing that you are not yet experienced enough to be
anything but confused ..." Sure, I was only successfully managing rather
big IT projects when you were probably still in diapers. So your "...
Whatever the hell that means. I think we are done here..." might be the
best thing you have (ever?) said.
 
Z

Zach

Whatever the hell that means. I think we are done here.

My professor of "iformatica" as the good man was called at the
university where I did one of my degrees (he could multiply big numbers
doing funny things with both his hands :) will have died some twenty
years ago. So your "Ask your professor why" stuff is rather hilarious.
And as to "... sensing that you are not yet experienced enough to be
anything but confused ..." Sure, I was only successfully managing rather
big IT projects when you were probably still in diapers. So your "...
Whatever the hell that means. I think we are done here..." might be the
best thing you have (ever?) said.
 
R

Rick Lones

My professor of "iformatica" as the good man was called at the university where
I did one of my degrees (he could multiply big numbers doing funny things with
both his hands :) will have died some twenty years ago. So your "Ask your
professor why" stuff is rather hilarious. And as to "... sensing that you are
not yet experienced enough to be anything but confused ..." Sure, I was only
successfully managing rather big IT projects when you were probably still in
diapers. So your "... Whatever the hell that means. I think we are done here..."
might be the best thing you have (ever?) said.

My mistake then. You write code just like a clueless kid (and whine about good
advice you are getting just like one too) so I assumed you must be one. But if
you've been a manager for a long time that could explain it, I guess. When was
the last time you personally architected, wrote, debugged, shipped, and then
supported a complete non-trivial commercial-quality app, say of 100K lines or
more? And I mean hands-on down & dirty with all of it, not just "specifying"
the kind of vague crap you have given us here and then handing if off to some
hapless minion?

Frankly I still think you may be a clueless kid, only now posing as a clueless
manager. Experienced engineers know how to pose a coherent question and they
also understand the need for precision of expression in technical discussions.
An "indexed class" which isn't even a class at all? LOL.

(FWIW, I have 30+ years experience designing and programming computers at
various levels ranging from COBOL business applications to CPU architecture &
microcode and have remained technical the entire time, stubbornly resisting the
occasional pressure to move to the dark side. Before that I worked as an
electronics technician for some 10 years both in and out of the military (long
before I ever went to college or even saw a computer). So I really didn't
appreciate your snotty "young man" comment much.)

Regards,
-rick-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top