(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?
If you do something like:
You will be able to instantiate and access it. If you change it to:
You will be able to call myClass.myMethod without instantiating a new myClass.