C# 'pointer' to parent

Started by
12 comments, last by JamesLewis 15 years, 11 months ago
Quote:Original post by fanaticlatic
The object instance would be a Game object see your program.cs file i believe the object will be called "Game1" providing you have not changed the program.cs source.
I'm not using XNA. I'm making a little sim/manager in WinForms.

Quote:Original post by benryves
Of course, typing this. also causes the autocomplete box to pop up, so helps if you're lazy. [wink]
That box kills me sometimes. Try to type something like 's' and you get set automatically typed for you, when all you wanted was 's'.

I have a similar question, so no need for a new thread.

The workers list will be a list of all possible workers. I also have a list of companies that can hire these workers, and I want them to each have a list referencing/pointing to workers in the first list.

I also want to make another list filled with the same workers, but sorted, so that the companies can scan this list in order to find good hires. I'm wondering how to go about this.

Is this the right thing to do?

Worker temp = Parent.Workers;
Company[n].Workers.Add(temp);

Or maybe I want a list of iterators or something?
Advertisement
So you have a list of companies and a list of workers. Then each company has a list of workers that work for that company?

Firstly, if you want to sort your list of objects by something specfic (like skill level) then have that object implement IComparable then implement the CompareTo method and make it return a comparison between the values in your object you want to compare. The following is a method out of one of my classes comparing start times of my VideoRecording objects:

        #region IComparable Members        /// <summary>        /// This method is used to compare this object with another object.  This method compares the start times of        /// both objects.        /// </summary>        /// <param name="obj">The object to compare this one with.</param>        /// <returns>Returns the result of the comparison.</returns>        public int CompareTo(object obj)        {            // Compare the start times            return this.StartTime.CompareTo(((VideoRecording)obj).StartTime);        }        #endregion


Then you just call Sort() on your list and it'll be sorted by whatever you specified in CompareTo.

So, you have a list of all workers (List<Worker> allWorkers) and a list of companies (List<Company> allCompanies) and you want the companies to be able to add workers to their own list of workers? Something like this:

public class Company{    // List of company workers    public List<Worker> CompanyWorkers    {        set;        get;    }}public class Worker{}public class SomeMainClass{    // Lists of workers and companies    List<Company> allCompanies;    List<Workers> allWorkers;    // ....... SETUP AND OTHER CODE    // .... Inside some method    {        // This example shows the current company (however you define it) adding the currently selected worker (perhaps this is taken out of a list box or something).        currentCompany.CompanyWorkers.Add(selectedWorker);    }}


Is that what you mean? Sorry if I've missed the point - was a little confussed by the first bit of your post.

Cheers,

James
My game class has a list of companies and a list of workers. The companies hire workers when needed from that list. But I don't want the companies to copy that data, just 'reference' it.

Same with the second list. It's not sorting that I need help with. I want to make new lists that are 'pointers'/references to the worker objects in the main list. Does my syntax achieve that?
OK, sorry, didn't quite grasp what you were getting at.

Quote:Original post by Vampyre_Dark
Is this the right thing to do?

Worker temp = Parent.Workers;
Company[n].Workers.Add(temp);


So yeah, Company[n].Workers will now contain a 'reference' to Parent.Workers at this point.

Reference types in C# are like, your objects - classes etc and then value types are the primitive types so when you pass one of your objects around like you have suggested, you're passing a pointer to that data's memory location (which I'm guessing you already suspected). What you need to be careful of here is automatic garbage collection - I can't see you coming accross this with your current code (your main list I can imagine would span the life of your application) but as soon as you lose all references to the initialy created Worker, it will be disposed.

James

This topic is closed to new replies.

Advertisement