How do I add generic C # code to a Visual C # 2008 Express project / solution?

(I still feel like a complete newbie to MS Visual environment ... so please bear with me!)

I am using Microsoft Visual C # 2008 Express Edition.

I have a project and there are two different forms in this project. Cs file for each form is run:

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

namespace MyNameSpace
{
    public partial class MyFormName : Form
    {
...

      

(... and the second is "MyFormName2", but no difference beyond that)

I want to write a function that I know needs to access both forms. I right clicked on my project, selected Add, selected New Item, then selected Code File and named my file "Common.cs" and it gave me a completely blank file that is in my project.

How to set it up ...? I thought I should do the following ...

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

namespace MyNameSpace
{
}

      

... but then when I try to add a function like:

public void mytestfunc () {} inside this namespace I get the following error:

"Expected class, delegate, enumeration, interface, or structure"

How do I configure so that I can have "mytestfunc" for MyFormName and MyFormName2?

Thank!

-Adeena

UPDATE: Understand (for now) that everything should be in the class, but then I don't understand how to actually use it. Does this mean that I have to create an object? This general function only happens with some math ...

so now if I have this:

namespace MyNameSpace
{
    public class MyCommonClass
    {
        public void testFunc()
        {
            MessageBox.Show("Hee hee!");
            return;
        }
    }
}

      

... how can I call testFunc from my form? Should I do the following:

MyCommonClass temp = new MyCommonClass;
temp.testFunc();

      

or is there any other way to call testFunc?

+1


source to share


4 answers


If you do something like:

namespace MyNameSpace
{
    public class myclass
    {
        public myMethod()
        {
            // Code
        }
    }
}

      

You will be able to instantiate and access it. If you change it to:



namespace MyNameSpace
{
    public class myclass
    {
        public static myMethod()
        {
            // Code
        }
    }
}

      

You will be able to call myClass.myMethod without instantiating a new myClass.

+2


source


The short answer is: everything must be inside a class; I would suggest you sit down with a basic tutorial to help you get through the basics ...



+1


source


The code must be inside classes.

It will look something like this:

using System;

namespace MyNameSpace
{
   public class CommonHelper
   {
       public string FormatMyData(object obj)
       {
            //do something
            return String.Empty;
       }
   }
}

      

0


source


If the function you are calling is not bound to forms, make it static

namespace myns
{
    public static class myhelper
    {
        public static void DoSomething()
        {
        }
    }
}

      

and call the method using myhelper.DoSomething ();

If the function you want to call is somehow related to forms, eg. common functionality in multiple forms, take the class out of the form (no visual form needed) and make it the base class of visual forms:

namespace myns
{
    public class MyFormBase : Form
    {
        protected void DoSomethingWithTheForm()
        {
        }
    }
}

      

and in your .cs form:

namespace myns
{
    public partial class MyFormName : MyFormBase
    {
    }
}

      

0


source







All Articles