simple c# does not exist in the current context question

sydneyguy
sydneyguy used Ask the Experts™
on
have set up two forms form1 and form2, i have a messagebox.show on form2 and from form1 want to run the cold that displays the message on form2 , have been reading but still a bit vague on how to get the other function to run by setting up the correct scope to be able to reference it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            msgdisplay();
        }
       
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


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

        private void msgdisplay()
        {
            MessageBox.Show("hello world");
        }
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
You need an instance of Form2, AND the msgdisplay() method must be Public (not Private):
    public partial class Form2 : Form
    {
        public void msgdisplay()
        {
            MessageBox.Show("hello world");
        }
    }

Open in new window


Then, in Form1:
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            f2.msgdisplay();
        }

Open in new window

Author

Commented:
thanks for that, but it opens up a new form, what i want to do is to add a codefile and then put this code on there so that it runs but does not open up another form
so i added a new file
which is blank but what goes in here
i understand that    //      f2.Show(); will stop the form from dispalying but the object still runs in the background.
any ideas
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
?

Do you want a form or not?  Why is this code you're trying to run in the Form2 class, if you don't want Form2?

Give me a "big picture" of what you're doing and how Form2 fits into this picture.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
big picture is i wish to load up code to do certain jobs on to files instead of forms so that i can use the whole file again and make my program more modular and portable.
so i thought if i loaded them up on codefiles the i coudl better manage them.
so what i want to do is to learn how to run a hello world box and from there progress to passing and returning variables that do there certain jobs.
so this is were i am heading towards
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Are you trying to dynamically load Form code from a file? In other words, you want to compile the code "on-the-fly", essentially? It is possible, but I don't think it's advisable. If this is what you are after, then I think you would do better with a plugin-style architecture.

Author

Commented:
all i want to do is have some data base managament code sitting on a code file so that i can call the function on the codefile and it does its job then returns or would a class file be better to use
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
You keep saying "file", and it makes me think you are actually referring to a .cs file (i.e. source code file). Is this what you mean, or are you referring to a compiled library which contains the functions you want to call (a.k.a DLL file)?

Author

Commented:
just a cs file not compiling at the moment.
i would just be using a cs file instead of a form
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Gotcha...add a CLASS to your Project and then add that code into the class.  Create an instance of the class (just like with the form) and use that instance to access the code in the methods.

Author

Commented:
i see that now
but when you add a new class it takes on the same name space.
should it not be able to see the function if it exists under the same namespace but on a different cs file, as i have added a new Class1.cs
i though i could ref the file at the top object back on form1 so that it would see all the code on the Class1 page.
if you have any good sites or examples showing that i am very happy to read up which is what i am doing any way. this area always tricked me up so want to get it mastered
thanks for you help

using System;
using System.Collections.Generic;
using System.Text;

namespace CSV_import_export
{
    class Class1
    {

    }
}

Author

Commented:
but not wanting to define every function that i need in the class its self

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            //Form2 f2 = new Form2();
      //      f2.Show();
            msgdisplay();
           
         }

       
    }
}

Author

Commented:
i guess this does expose the whole code for the page that i require so will keep going with this and keep reading to better under stand it
do you know if any really good tutroials that go through setting up an manipulating classes and namespaces ect on the web
thanks
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
You might be looking for STATIC methods, which can be accessed without an instance:
    class Class1
    {

        private static string _msg = "Hello World!";

        public static string Foo()
        {
            return _msg;
        }

    }

Open in new window


Then, in the Form:
    public partial class Form1 : Form

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = Class1.Foo(); // <-- no instance of "Class1" needed to access static methods
        }

    }

Open in new window

Author

Commented:
thanks for all your help am up and running so thanks you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial