Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Reference Parent Object Property from child

Status
Not open for further replies.

moongirl129

Programmer
Sep 5, 2002
38
GB
I have a class called company as follows:

Code:
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:

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

 ....other properties
}

On my UI I have a datagrid for each bindinglist - I've set up a bindingsource for company.issues1 and company.issues2. The user enters each issue straight into the datagrid and this in turn keeps the 'thisCompany' object updated.

Here's my problem:

Each issue needs to have a unique reference number, an integer starting from 1. But these numbers need to increment across both bindinglists. For instance if the user enters 5 issues for 'issues1' the first Issue entered for the 'issues2' bindingList should have a reference number of 6.

I am really stumped as to how to do this!! Can anyone help??
 
What happens if the user enters 3 items in datagrid #1, another in datagrid #2, then skips back and adds another one in datagrid #1?

This doesn't seem to make any sense - if the two sets of issues are linked this way, why do you need to split them up on the screen?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Point taken - they're split up because that's the way the client wants them separated. Later in the process each of these items will be displayed in a report by reference number. I do have the option of giving each Issue object a reference number which is incremental only in it's list but I still can't see how I can pass it a reference number automatically when it is created from a datagridview via a bindinglist. Am I making any sense?!!
 
What I'm asking is if a datagridview's datasource is a BindingList of custom objects, as the user enters those new objects into the datagridview how can they create their own reference number? The object can't reference it's index within it's parent can it?? So is my only option here to create a custom collection of my objects?
 
In case anyone stumbles across this thread in the future here is the answer - this change to the Issue class enables each Issue object to have an automatically incremented reference number which continues across all of the bindinglists.

Code:
public class Issue : NotifyProperyChangedBase
{

 // Declare static int
  private static int nextRef = 0;

 
 // Assign it in the constructor
 public Issue()
 {
     refNumber = ++nextRef;
 }

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

 ....other properties

}
 
Should you have a public setter for the refNumber? Once it's set in the constructor, you shouldn't be able to change it.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
oops yes - forgot to remove it from the example - thanks stevexff!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top