I'm sorry if the title is a bit vague. Hehe.

I've had this idea to use different namespaces to control the contents of a second form. Unfortunately, this version uses five namespaces: three for the forms, and two for the contents of the other two forms. What I need to do here is to have the user click one of two buttons in the first form, and it will load the second form, with the using namespace added on the second form, which will load all the classes I need.

Did I make any sense there? I wrote some code, and it works, but it uses three forms. I only need two.

(I cut out the 'using' on the top of the forms, because it is generated by Visual C#, anyway)

The First Form (main)

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 x = new Form2();

            x.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form3 x = new Form3();

            x.ShowDialog();
        }
    }
}

The Second Form

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            label1.Text = namespace2.Class1.text();
        }

    }
}

The Third Form

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();

            label1.Text = namespace2.Class2.text();
        }
    }
}

(The second and third forms are almost identical, except for the namespace bit.)

The first namespace

namespace namespace2
{
    class Class1
    {
        public static string text()
        {
            return "blah 1";
        }
    }
}

The second namespace

namespace namespace2
{
    class Class2
    {
        public static string text()
        {
            return "blah 2";
        }
    }
}

What I'm trying to do here is to replace the "namespace1.Class1.text()" bits on the form to something like "using namespace1;" then "Class1.text();" on the main forms. I think that way, I can get rid of the third form, and use the buttons on the first form to show two possible forms with the second form.

PS: Sorry for the bad names; I was a bit on the rush when the idea came to me.

PPS: Sorry for the lack of the specific terms and whatever. I'm totally new to C#.

Recommended Answers

All 4 Replies

If all the forms and classes are in the same project, you can add

using Namespace2

in the top of the forms, and you will be able to get the expected sintaxis.

Hope this helps

It's not clear what you want to accomplish. Do you want to have both buttons link to the same event, then have the code determine which form should be generated and shown?

Ys, we are interested to know what is the final goal for that

Basically, both buttons link to one form, but the loaded form is accessing two different namespaces. namespace1 (which would load 'blah1') and namespace2 (which would load 'blah2') - I think this part caused the confusion. The fourth group of cods was supposed to be "namespace1". I'm so sorry! :D - had the same classes, and the same function names, but not the stuff they return. Now, if the user click on the first button, namespace1 would load on the loaded form, and if the user clicks on the second one, namespace2 would load on the loaded form.

I think I was able to solve this, but the code became ridiculously massive. It involved a function for every single method in the two namespaces.

(It's for a game... Thing, which uses two languages for the text. If I used the methods I used in the original code I posted in the first post, I'd have to create two programs... which kinda ticks away one of my project's objectives.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.