How can I reference a parent objects properties?

  • Thread starter Thread starter moongirl
  • Start date Start date
M

moongirl

I have an object called Company, and this contains 3 public properties
which are all of type BindingList<Issue> where Issue is another class
of mine. Each issue needs to have a unique reference number which
should be automatically incremented when a new issue is added to a
binding list. The list of numbers should be global, i.e. if an issue
is added to one binding list after 3 have already been added in
another, its reference number should be 4.

Firstly I am not sure about the best way to implement this. I have
thought about adding a property to the Company object called
numberOfIssues which would be equal to the total count of the 3
binding lists. I would then need to be able to reference this property
when a new Issue is created in any of the binding lists.

Can anyone help me with this?
 
You could use a static property on the Issue class, though you'd have to do
some work if you're doing multi-threading. Or if they don't have to be
numbers you could use Guids for unique reference numbers.
 
I have a class called company as follows:

public class Company
{
public BindingList<Issue> Issues1
{
get { return issues1; }
set { issues1 = value; }
}
public BindingList<Issue> Issues2
{
get { return issues2; }
set { issues2 = value; }
}

...other properties
}

and my Issue class looks like:


public class Issue : NotifyProperyChangedBase
{
public int RefNumber
{
get { return refNumber; }
set { refNumber = value; }
}

....other properties
}


I have a datagridview who's datasource is one of the bindinglists.
When the user adds data to the datagridview a new Issue object is
added to the bindinglist. As this object is added I want it
toautomatically create it's own incremental reference number. This
number could be the same as the objects index in the bindinglist.....
but the object can't get to this property can it? Could I do this with
a custom collection? Or does anyone have any other ideas? Thanks!
 
moongirl,

What I believe Zoodor was referring to was something as follows,
assuming you are not in a multithreaded environment:

public class Issue : NotifyProperyChangedBase
{
static int globalCounter = 0;
public Issue( ... )
{
// .. do your normal creation stuff
this.refNumber = globalCounter++;
}

public int RefNumber
{
get { return refNumber; }
// set { refNumber = value; }
}

....other properties
}

With this, every time you create an instance of this class, you will
be assigning a number to the instance's refNumber and incrementing the
static globalCounter so the next instance will get the next number.

Also, depending upon your management system/requirements you may want
to reconsider allowing external manipulation of your reference
numbers.

Hope this helps,
John
 
Back
Top