Help with "forms" container

  • Thread starter Thread starter Troy Bull
  • Start date Start date
T

Troy Bull

Greetings

I am trying to use a singleton to hold a group of forms. I have a
MDIMaster form. I have a class called Forms; Forms is a singleton. I
want to do something like the following.

In MDIMaster (and other forms), I want to get an instance of Forms (the
only instance). Inside Forms then there are public variables for each
of the possible forms, if a "form" has never been accessed the Forms
factory creates a new instance of the particular form and returns that
instance of it.

Basically I want a container that will keep track of all the currently
open forms, and make sure that no particular form is ever opened 2
times. This also effectively "registers" forms with the singleton so I
can send messages between different forms.

For my first example I tried adding an about box. In MDI Master under
the menu item help / about, I have the following code:

private void aToolStripMenuItem_Click(object sender, EventArgs e)
{
Forms.Instance.aboutBox.Show();
}

Inside my Forms class I have the following:

public AboutBox aboutBox
{
get
{
if (aboutBox == null)
{
aboutBox = new AboutBox();
}
return aboutBox;
}
}

I am getting an error :
Error 1 Inconsistent accessibility: property type 'AcroDesktop.AboutBox'
is less accessible than property 'AcroDesktop.Forms.aboutBox'
C:\Users\bull\Desktop\Acro\trunk\AcroDesktop\AcroDesktop\Forms.cs 14 25
AcroDesktop

I looked at the definition of AcroDesktop.AboutBox and it appears to be
public..

If anyone could give me any pointers I would really appreciate it, I
thought I had came up with a great idea here, until i tried to compile it :)


Is there some other way to do this? The main thing I want to get is the
registration part so that my forms can invoke one another, or send
messages to each other..


Troy
 
Greetings
Hi.

I am trying to use a singleton to hold a group of forms. I have a
MDIMaster form. I have a class called Forms; Forms is a singleton. I
want to do something like the following.

In MDIMaster (and other forms), I want to get an instance of Forms (the
only instance). Inside Forms then there are public variables for each
of the possible forms, if a "form" has never been accessed the Forms
factory creates a new instance of the particular form and returns that
instance of it.

Question 1: let's clear up some terminology. What do you mean by "public
variables"? A class can have fields and properties. Do you mean "public
fields"? Or "public properties"?

Question 2: why do you want a singleton? So far, I haven't seen anything
that would suggest you need even a single instance of your Forms class.
Can't it just be a static class with a bunch of static fields and
properties? Or is it derived from some other class that's not a static
class, preventing you from making it static? Or does the class need to be
referenced at some point by some other class as an instance (like, putting
into a collection, or exposing some interface, or something like that)?
Basically I want a container that will keep track of all the currently
open forms, and make sure that no particular form is ever opened 2
times. This also effectively "registers" forms with the singleton so I
can send messages between different forms.

Seems fine to me. But unless there's something else you need from this
class as well, I'm still not clear on the need to make it an instanced
class at all.
For my first example I tried adding an about box. In MDI Master under
the menu item help / about, I have the following code:

private void aToolStripMenuItem_Click(object sender, EventArgs e)
{
Forms.Instance.aboutBox.Show();
}

Inside my Forms class I have the following:

public AboutBox aboutBox
{
get
{
if (aboutBox == null)
{
aboutBox = new AboutBox();
}
return aboutBox;
}
}

I am getting an error :
Error 1 Inconsistent accessibility: property type 'AcroDesktop.AboutBox'
is less accessible than property 'AcroDesktop.Forms.aboutBox'
C:\Users\bull\Desktop\Acro\trunk\AcroDesktop\AcroDesktop\Forms.cs 14 25
AcroDesktop

I looked at the definition of AcroDesktop.AboutBox and it appears to be
public..

Well, apparently it's not. You haven't posted the relevant code. That
is, the code where the type is actually declared. But clearly, the type
"AcroDesktop.AboutBox" is _not_ public. That's what the compiler is
telling you.

There may be some less-than-intuitive reason for this, but I don't think
the compiler would just yank your chain for the fun of it. If it says the
type's not public, then I'm sure it's not.
If anyone could give me any pointers I would really appreciate it, I
thought I had came up with a great idea here, until i tried to compile
it :)

Is there some other way to do this? The main thing I want to get is the
registration part so that my forms can invoke one another, or send
messages to each other..

The main thing I would change is not bother with making the Forms class a
singleton, unless you have some other need to refer to the class as an
actual instance. You haven't mentioned such a need so far, and the
functionality you're describing here can be done with a static class.

Pete
 

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

Back
Top