views:

351

answers:

4

(I still feel like a complete newbie in MS Visual environments... so please bear with!)

I'm using Microsoft Visual C# 2008 Express Edition.

I have a project and in that project are two different forms. The .cs file for each form starts out:

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 differences besides that)

I want to write a function that I know both forms are going to need to access. 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's in my project.

How do I set this 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() { } within that namespace I get the following error:

"Expected class, delegate, enum, interface, or struct"

How do I set things up so I can have "mytestfunc" be available to both MyFormName and MyFormName2?

Thanks!

-Adeena

UPDATE: Understand (now) that everything must be in a class, but then I don't understand how to really use it. Does that mean I have to create an object? This common function happens to just be some math...

so now if I have this:

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

...how do I call testFunc from my Form? Must I do the following:

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

or is there another way to call testFunc?

A: 

Code need to be inside classes.

It would look something like this:

using System;

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

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.

Steven Robbins
Ah - got it... Thanks!
adeena
No worries, Learning C# 3.0 by Jesse Liberty is quite a nice book for starting out in C#, even if you haven't done any object orientated programming before.
Steven Robbins
I have done OOP before... 8-10 yrs ago I was a C++ programmer, but in unix, not windows. Some I'm relearning some things I used to know, and also doing it in this new environment... thanks for the help!
adeena
+1  A: 

The short answer is that everything needs to be inside a class; I'd suggest you sit down with a basic tutorial to help you get to grips with the basics...

Rowland Shaw
please see my edit to the original question... understand "everything needs to be inside a class" but then what?
adeena
A: 

If the function you call is not related to the 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 form-related, e.g. common functionality across multiple forms, derive a class from Form (does not need a visual form) and make it base class of the visual forms:

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

and in your form's .cs:

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