calling function from other namespace

  • Thread starter Thread starter GK
  • Start date Start date
GK said:
Is there a way I can call a variable from other namespace? thanks.

Define "call a variable". If by that, you mean "access a variable",
then sure. Namespaces don't restrict accessibility. They are simply a
way to group types. If you couldn't from code in one namespace access
class members found a different namespace, you couldn't use .NET at
all...after all, every class in .NET is by default not the same
namespace as your own code (and should not be in any case).

If you don't mean "access a variable", then you need to rewrite your
question. Taken literally, "call a variable" is meaningless, since you
call methods, not variables.

Pete
 
I was hoping the answer will be a simple one.

here is the full question:

I need to add an additional feature to a large application. in stead of
insert the new code into part of the original application, I created a new
project with the features needed. The original application is C# in VS2005,
my new project is also c# in VS2005. I inserted my new project into the
original solution. now, from the main form (say FormMain) of the original
application. I need to initial a call to a form(say FormA) that belongs to
the new project, and hide the FormMain. when the FormA is closed, I put a
code on FormA_FormClosing to show the FormMain.

the new project is under a new namespace.

I want to I try to make the reference to each other. but the VS gives me an
error message "circular dependncy".

See the below sample code:

namespace OrginalApplication
{
private void button1_click(...)
{
newProject.FormA d = new newProject.FormA();
this.Hide();
d.Show();
}
}

namespace newProject
{
private void FormA_FormClosing(...)
{
//code to show the main form
}
}
 
GK said:
I was hoping the answer will be a simple one.

You can only have simple answers to questions that are a) simple and b)
understandable. :)
here is the full question:

I need to add an additional feature to a large application. in stead of
insert the new code into part of the original application, I created a new
project with the features needed. The original application is C# in VS2005,
my new project is also c# in VS2005. I inserted my new project into the
original solution. now, from the main form (say FormMain) of the original
application. I need to initial a call to a form(say FormA) that belongs to
the new project, and hide the FormMain. when the FormA is closed, I put a
code on FormA_FormClosing to show the FormMain.

Your code example is difficult to understand, not the least of which
because it doesn't even comply with the rules for the structure of a C#
program (e.g. no class declarations), never mind could it compile.

But, if I understand your question correctly, the correct answer is that
rather than your new "FormA" class knowing about "FormMain" and trying
to re-show the form after it's been closed, you instead should have your
"FormMain" subscribe to the FormClosed event in "FormA", and then show
itself when the event is raised.

For example:

namespace OriginalApplication
{
class FormMain
{
void button1_Click(object sender, EventArgs e)
{
NewProject.FormA d = new NewProject.FormA();

d.FormClosed += (sender, e) => this.Show();
this.Hide();
d.Show();
}
}
}

namespace NewProject
{
class FormA
{
// don't reference FormMain here at all
}
}

Of course, to the extent that this problem came up because you tried to
add code to the original application, but without putting that code into
the same project as the original application, you could also fix the
problem simply by not doing that. Instead, put the new code in the old
project, rather than it's own project.

But IMHO the above approach should work well, and it's better to avoid
explicit dependencies between classes anyway.

By the way, you _really_ should take the time to understand that your
question has nothing at all to do with namespaces. You can have more
than one namespace in a project, and different projects can both use the
same namespace. The namespace here is completely irrelevant.

What's relevant is that the parts of your code causing problems are in
two different assemblies (the output of a project), in which you need
code in each assembly to refer to something in the other assembly. It's
the circular asembly reference that's causing trouble, not the namespaces.

(And this answer wouldn't be 100% complete without me mentioning that
you can in fact have circular assembly references as long as you compile
from the command line. But really, you should avoid the circular
reference regardless).

Pete
 
Peter Duniho said:
GK wrote:
namespace OriginalApplication
{
class FormMain
{
void button1_Click(object sender, EventArgs e)
{
NewProject.FormA d = new NewProject.FormA();

d.FormClosed += (sender, e) => this.Show();
this.Hide();
d.Show();
}
}
}

namespace NewProject
{
class FormA
{
// don't reference FormMain here at all
}
}

Of course, to the extent that this problem came up because you tried to
add code to the original application, but without putting that code into
the same project as the original application, you could also fix the
problem simply by not doing that. Instead, put the new code in the old
project, rather than it's own project.

Could'nt he stille seperate the code i different project's and use the same
Namespace in both project's. That would do the same or ??

/Finn
 
Peter,

You understand my problem very well. would you explain this line:

d.FormClosed += (sender, e) => this.Show();

I was not able to have it compiled.
 
Finn said:
Could'nt he stille seperate the code i different project's and use the same
Namespace in both project's. That would do the same or ??

He could do that, but it wouldn't fix the problem. That's my point. He
doesn't have a namespace problem, he's got a project reference problem.

Pete
 
GK said:
Peter,

You understand my problem very well. would you explain this line:

d.FormClosed += (sender, e) => this.Show();

I was not able to have it compiled.

What version of C# are you using? The lambda expression syntax is
available only in C# 3.0 and later (VS 2008, 2010).

Try this as an alternative:

d.FormClosed += delegate { this.Show(); };

And if that doesn't work:

d.FormClosed += d_FormClosed;

where you also have a named method in your FormMain class:

void d_FormClosed(object sender, EventArgs e)
{
this.Show();
}

Pete
 
Hi Pete,

I figured it out. I use these statements and they work:

d.FormClosed += new
System.Windows.Forms.FormClosedEventHandler(RemoteShowMainForm);

....

private void RemoteShowMainForm(...)
{
this.Show();
}


question remains that this line of code does not compile, is it a legal
statement?

d.FormClosed += (sender, e) => this.Show();

thank you for your help.
 
the reasons I use totally different namespaces, projects are to make my code
maintenance easier later. because I know that my new and old code are
seperated, there have only min impact on each other.

thanks all.
 
Back
Top