Passing Form as parameter to non-form class

  • Thread starter Thread starter Ron Rohrssen
  • Start date Start date
R

Ron Rohrssen

I want to show a dialog and when the form (dialog) is
closed, return to the calling form. The calling form
should then be able to pass the child form to another
object with the form as a parameter.

For example,

FormOptions formOptions = new FormOptions();
if (formOptions.ShowDialog(this) == DialogResult.OK)
{
ConfigurationManager configurationManager = new
ConfigurationManager();

configurationManager.saveConfiguration(formOptions);
}

formOptions.Dispose();

My ConfigurationManager has a method that looks like
this:


public void saveConfiguration(FormOptions formOptions)
{
ConfigurationData configurationData = new
ConfigurationData
(formOptions);
saveConfiguration(configurationData);
}

When I build this code I keep getting an error like this:
ConfigurationData.cs(20): The type or namespace
name 'FormOptions' could not be found (are you missing a
using directive or an assembly reference?)

These classes are all within the same namespace.

Is it possible to pass the form object to another class?
Or do I need to extract values from the form and pass
them on to my ConfigurationManager?

I've even tried qualifying the FormOptions class in my
parameter list as SCT.FormOptions. "SCT" is my namespace.

Intellisense doesn't see any of my forms when I try to
qualify the FormOptions class.

I suppose this is a pretty simple solution. But, it's
frustrating.
 
Ron,

They might all be in the same namespace, but is the form and the class
in the same assembly? If not, then you have to make sure that the class
that the form is being passed to has a reference to the Form class that you
created.

Passing a form around is like passing any other object around. It is
nothing more than an object, and should be treated as such, as all the same
rules apply. It has no more or less privledge than any other object (in
terms of how it is used).

Hope this helps.
 
Thanks Nicholas.

But, how do I create a reference to my form from the
class that receives the form as a parameter?

Ron
-----Original Message-----
Ron,

They might all be in the same namespace, but is the form and the class
in the same assembly? If not, then you have to make sure that the class
that the form is being passed to has a reference to the Form class that you
created.

Passing a form around is like passing any other object around. It is
nothing more than an object, and should be treated as such, as all the same
rules apply. It has no more or less privledge than any other object (in
terms of how it is used).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ron Rohrssen said:
I want to show a dialog and when the form (dialog) is
closed, return to the calling form. The calling form
should then be able to pass the child form to another
object with the form as a parameter.

For example,

FormOptions formOptions = new FormOptions();
if (formOptions.ShowDialog(this) == DialogResult.OK)
{
ConfigurationManager configurationManager = new
ConfigurationManager();

configurationManager.saveConfiguration(formOptions);
}

formOptions.Dispose();

My ConfigurationManager has a method that looks like
this:


public void saveConfiguration(FormOptions formOptions)
{
ConfigurationData configurationData = new
ConfigurationData
(formOptions);
saveConfiguration(configurationData);
}

When I build this code I keep getting an error like this:
ConfigurationData.cs(20): The type or namespace
name 'FormOptions' could not be found (are you missing a
using directive or an assembly reference?)

These classes are all within the same namespace.

Is it possible to pass the form object to another class?
Or do I need to extract values from the form and pass
them on to my ConfigurationManager?

I've even tried qualifying the FormOptions class in my
parameter list as SCT.FormOptions. "SCT" is my namespace.

Intellisense doesn't see any of my forms when I try to
qualify the FormOptions class.

I suppose this is a pretty simple solution. But, it's
frustrating.


.
 
Ron,

You don't add references to classes, rather the project that you are in
needs to reference the project that the form is in.

However, are these two in the same project? If they are, then I think
that the namespace declaration is incorrect for one of the elements (the
form or the class trying to use the form).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ron Rohrssen said:
Thanks Nicholas.

But, how do I create a reference to my form from the
class that receives the form as a parameter?

Ron
-----Original Message-----
Ron,

They might all be in the same namespace, but is the form and the class
in the same assembly? If not, then you have to make sure that the class
that the form is being passed to has a reference to the Form class that you
created.

Passing a form around is like passing any other object around. It is
nothing more than an object, and should be treated as such, as all the same
rules apply. It has no more or less privledge than any other object (in
terms of how it is used).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ron Rohrssen said:
I want to show a dialog and when the form (dialog) is
closed, return to the calling form. The calling form
should then be able to pass the child form to another
object with the form as a parameter.

For example,

FormOptions formOptions = new FormOptions();
if (formOptions.ShowDialog(this) == DialogResult.OK)
{
ConfigurationManager configurationManager = new
ConfigurationManager();

configurationManager.saveConfiguration(formOptions);
}

formOptions.Dispose();

My ConfigurationManager has a method that looks like
this:


public void saveConfiguration(FormOptions formOptions)
{
ConfigurationData configurationData = new
ConfigurationData
(formOptions);
saveConfiguration(configurationData);
}

When I build this code I keep getting an error like this:
ConfigurationData.cs(20): The type or namespace
name 'FormOptions' could not be found (are you missing a
using directive or an assembly reference?)

These classes are all within the same namespace.

Is it possible to pass the form object to another class?
Or do I need to extract values from the form and pass
them on to my ConfigurationManager?

I've even tried qualifying the FormOptions class in my
parameter list as SCT.FormOptions. "SCT" is my namespace.

Intellisense doesn't see any of my forms when I try to
qualify the FormOptions class.

I suppose this is a pretty simple solution. But, it's
frustrating.


.
 
Yes! The my form class is in the same project as the
class that I'm trying to pass it to.

I think that's why I'm confused on why I would need to do
anything special to use this feature.

I've verified that all of my classes (only 7 right now)
are in the same namespace. I've copied the "namespace
SCT" line to each of my classes, replacing what was
there. Plus when I use my class browser, all of the
classes appear within the SCT namespace.

Confusing, isn't it?

Thanks for your help.

I hope that you (or anyone else) is able to offer a way
to make this work.
-----Original Message-----
Ron,

You don't add references to classes, rather the project that you are in
needs to reference the project that the form is in.

However, are these two in the same project? If they are, then I think
that the namespace declaration is incorrect for one of the elements (the
form or the class trying to use the form).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ron Rohrssen said:
Thanks Nicholas.

But, how do I create a reference to my form from the
class that receives the form as a parameter?

Ron
-----Original Message-----
Ron,

They might all be in the same namespace, but is
the
form and the class
in the same assembly? If not, then you have to make sure that the class
that the form is being passed to has a reference to
the
Form class that you
created.

Passing a form around is like passing any other object around. It is
nothing more than an object, and should be treated as such, as all the same
rules apply. It has no more or less privledge than
any
other object (in
terms of how it is used).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I want to show a dialog and when the form (dialog) is
closed, return to the calling form. The calling form
should then be able to pass the child form to another
object with the form as a parameter.

For example,

FormOptions formOptions = new FormOptions();
if (formOptions.ShowDialog(this) == DialogResult.OK)
{
ConfigurationManager configurationManager = new
ConfigurationManager();

configurationManager.saveConfiguration(formOptions);
}

formOptions.Dispose();

My ConfigurationManager has a method that looks like
this:


public void saveConfiguration(FormOptions formOptions)
{
ConfigurationData configurationData = new
ConfigurationData
(formOptions);
saveConfiguration(configurationData);
}

When I build this code I keep getting an error like this:
ConfigurationData.cs(20): The type or namespace
name 'FormOptions' could not be found (are you
missing
a
using directive or an assembly reference?)

These classes are all within the same namespace.

Is it possible to pass the form object to another class?
Or do I need to extract values from the form and pass
them on to my ConfigurationManager?

I've even tried qualifying the FormOptions class in my
parameter list as SCT.FormOptions. "SCT" is my namespace.

Intellisense doesn't see any of my forms when I try to
qualify the FormOptions class.

I suppose this is a pretty simple solution. But, it's
frustrating.


.


.
 
Back
Top