Unable to pass an object

L

Lilith

I have a form that invokes two other dialog forms for controlling the
configuration of the program. The dialogs are invoked modally. Rather
than pile up a large number of variables in the primary form I created
a class to hold the configuration data, validate it, save it. etc. An
object of this class is in the main form. The data is all private but
I have public accessors for all of it. My need is to make it
availlable to the two sub-forms when I call them but no matter my
method of approach I get stymied in the compile with a variety of
errors, depending on the methodology I'm trying.

I've make the object public, private and tried to pass it to a newly
constructed form via a public method in the sub-form before showing
the dialog (I'm successful with individual items, but not the class
object.) When I make it private, it's not available in the context of
the sub-form. This I can understand. When I make it public and try
to pass it via a function I get an error along the lines of

Inconsistent accessibility: parameter type 'GWIAmon.AllCfg' is less
accessible than method 'GWIAmon.Config.SetCfg(GWIAmon.AllCfg)

I think what I'm seeing from searches is that the object has to be of
a public class, though I'm not sure what that means.

Any help would be appreciated. Right now the code is looking awfully
ugly and since I need to do this in at least two places, I'd like to
make sure that I'm not dropping data by passing member data one at a
time.

On this same note, if I do

form2.ShowDialog(this);

Where is "this" represented in form2 to such that I might have acces
to it? Or is this solely for the benefit of the form to know what
owns it?
 
P

Peter Duniho

Lilith said:
[...]
Inconsistent accessibility: parameter type 'GWIAmon.AllCfg' is less
accessible than method 'GWIAmon.Config.SetCfg(GWIAmon.AllCfg)

I think what I'm seeing from searches is that the object has to be of
a public class, though I'm not sure what that means.

It sounds as though your class is called "AllCfg", that you have a main
form class called "Config", which has a public method "SetCfg". Is that
correct?

If so, then the error is complaining that the method is public, but the
class "AllCfg" is not. If you don't put the "public" key word as part
of your class declaration, then the class isn't public and can't be used
as the type in other public members, like a method return type or
parameters.

So, in other words, the searches you've apparently already done have
already told you the problem. You just need to declare the class as
public. If you understand the difference between other things being
public, protected, private, or internal, then it's not clear to me why
you wouldn't also understand what it means for a class to be public.
It's the same thing.

If the above doesn't answer your question, you need to post a
concise-but-complete sample of code that demonstrates the problem you're
having. That way someone can explain how to change the code so that it
works.

Pete
 
L

Lilith

Lilith said:
[...]
Inconsistent accessibility: parameter type 'GWIAmon.AllCfg' is less
accessible than method 'GWIAmon.Config.SetCfg(GWIAmon.AllCfg)

I think what I'm seeing from searches is that the object has to be of
a public class, though I'm not sure what that means.
It sounds as though your class is called "AllCfg", that you have a main
form class called "Config", which has a public method "SetCfg". Is that
correct?

Actually it's the sub-form that's of class Config and it's that class
that has the SetCfg method. From the main class I've done something
like the follow (code is back at the office right now.)

public AllCfg allcfg = new AllCfg();
..
..
..
using (Config cfg = new Config()) {
cfg.SetCfg(allcfg);
cfg.Show.Dialog();
// other stuff to do after return from the Config form
}
If so, then the error is complaining that the method is public, but the
class "AllCfg" is not. If you don't put the "public" key word as part
of your class declaration, then the class isn't public and can't be used
as the type in other public members, like a method return type or
parameters.
So, in other words, the searches you've apparently already done have
already told you the problem. You just need to declare the class as
public. If you understand the difference between other things being
public, protected, private, or internal, then it's not clear to me why
you wouldn't also understand what it means for a class to be public.
It's the same thing.

I grok an object being public. In my reading I haven't come across
the use of a public class, or at least, not a discussion of it such
that it sunk in. How is it different from a declaration of a public
object of the class?
 
P

Peter Duniho

Lilith said:
I grok an object being public. In my reading I haven't come across
the use of a public class, or at least, not a discussion of it such
that it sunk in. How is it different from a declaration of a public
object of the class?

A class has an access level, just as a class member such as a field,
property, or method does. And just as you can explicitly specify that
access level for a class member, you do so for a class itself.

The important thing here is that it sounds as though your public method
is using a private class, which isn't allowed. All of the types that
are part of the method signature need to be public if the method itself
is public. Otherwise, there could be a situation in which the caller
has access to the method, but not the types required by the method.

I've left the following text from my previous post, which you quoted, as
a hint:

Pete
 
L

Lilith

A class has an access level, just as a class member such as a field,
property, or method does. And just as you can explicitly specify that
access level for a class member, you do so for a class itself.

The important thing here is that it sounds as though your public method
is using a private class, which isn't allowed. All of the types that
are part of the method signature need to be public if the method itself
is public. Otherwise, there could be a situation in which the caller
has access to the method, but not the types required by the method.

I think I've managed to grasp what you're saying.
I've left the following text from my previous post, which you quoted, as
a hint:

I did some testing against some code I had available by making some
temporary modifications. It works fine.

Many thanks for your help.
Lil
 

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

Top