Reference to class

  • Thread starter Thread starter Mark F.
  • Start date Start date
M

Mark F.

I have a dialog the displays info. When called from the main form I need to
access the property values in my CurrentProject class. The values are null
when I attempt to access them from the dialog class. I'm not sure how to
make the properties accessible to all other classes in the project.

// CurrentProject.cs
// This class holds properties for the user's project they are working on
(not the C# project).

public string ProjectPath
{
get { return _projectPath; }
set { _projectPath = value; }
}

// mainform.cs

CurrentInfoDialog dlg = new CurrentInfoDialog();
dlg.ShowDialog();


// CurrentInfoDialog.cs

public partial class CurrentInfoDialog : Form
{
CurrentProject cp = new CurrentProject();
// in this class all properties returned from 'cp' are null!

Public CurrentInfoDialog()
{

etc....

Thanks,
Mark
 
I have a dialog the displays info. When called from the main form I need to
access the property values in my CurrentProject class. The values are null
when I attempt to access them from the dialog class. I'm not sure how to
make the properties accessible to all other classes in the project.

// CurrentProject.cs
// This class holds properties for the user's project they are working on
(not the C# project).

public string ProjectPath
{
get { return _projectPath; }
set { _projectPath = value; }

}

// mainform.cs

CurrentInfoDialog dlg = new CurrentInfoDialog();
dlg.ShowDialog();

// CurrentInfoDialog.cs

public partial class CurrentInfoDialog : Form
{
CurrentProject cp = new CurrentProject();
// in this class all properties returned from 'cp' are null!

Public CurrentInfoDialog()
{

etc....

Thanks,
Mark

Dear Mark,

How many CurrentProject instances you have?

Do you need the form to create the instance or just display or
manipulate a reference of it?
You can pass an instance of a CurrentProject class to the Forms'
constructor as one way.

// Dialog constructor
public CurrentInfoDialog(CurrentProject cp)
{
// Declare member variable cp. Here we set it.
this.cp = cp;
}

Hope this helps,
Moty
 
Moty Michaely said:
Dear Mark,

How many CurrentProject instances you have?

Do you need the form to create the instance or just display or
manipulate a reference of it?
You can pass an instance of a CurrentProject class to the Forms'
constructor as one way.

// Dialog constructor
public CurrentInfoDialog(CurrentProject cp)
{
// Declare member variable cp. Here we set it.
this.cp = cp;
}

Hope this helps,
Moty

I have an instance in the main form class to set the project path property
when a new project in created or one in opened. I also tried passing the
instance of the CurrentProject class to the dialog contructor but an error
is returned:

Error 1 Inconsistent accessibility: parameter type 'PrjMkr.CurrentProject'
is less accessible than method
'PrjMkr.CurrentInfoDialog.CurrentInfoDialog(PrjMkr.CurrentProject)'

Mark
 
I have an instance in the main form class to set the project path property
when a new project in created or one in opened. I also tried passing the
instance of the CurrentProject class to the dialog contructor but an error
is returned:

Error 1 Inconsistent accessibility: parameter type 'PrjMkr.CurrentProject'
is less accessible than method
'PrjMkr.CurrentInfoDialog.CurrentInfoDialog(PrjMkr.CurrentProject)'

Mark

Mark,

Can you paste sample code? maybe the decleration of CurrentProject and
the form? (Including access keywords).

Moty
 
Moty Michaely said:
Mark,

Can you paste sample code? maybe the decleration of CurrentProject and
the form? (Including access keywords).

Moty

The CurrentProject class is just a simple generic class:

namespace PrjMkr
{
class CurrentProject
{

etc..

At the top of the mainfrm.cs class I have an instance of the CurrentProject
class;

CurrentProject cp = new CurrentProject(); // global

All properties on the CurrentProject are accessible in the main form class.

I call the dialog in the mainfrm.cs with this:

private void showInfoToolStripButton_Click(object sender, EventArgs e)
{
CurrentInfoDialog dlg = new CurrentInfoDialog(this.cp);
dlg.ShowDialog();
}

Mark
 
Thanks for your help!

I overlooked the access modifier (duh). I changed the made the
CurrentProject class public and that fixed it.

Mark
 
Thanks for your help!

I overlooked the access modifier (duh). I changed the made the
CurrentProject class public and that fixed it.

Mark

Great!

Feel free to ask further questions..

Good day,
Moty
 
Back
Top