Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

VS C# Structure Question

As I continue to learn and refine my skills with VS C#, I have come upon a issue that I have not been able to find by searching. I have a program with several forms and now I have to create code that is common to all of them. Instead of adding the code to each of these forms, how do I create a code file where I can put this shared code? How do I reference the code in the forms? I see I can go to Projects and add components, classes, code files, etc. Which one do I choose to store this common code? The code is going to consist of functions that will be called from any one of the forms.Any code examples would be helpful.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Typically a class is used for such a purpose. If the functions are standalone, and they don't modify any global data, then you might consider making the functions and the class housing those functions static. This means you won't have to create an instance of that particular class in order to use those functions. You will, however, have to prefix the function call with the name of the class in order to call it (e.g. TheClassName.TheFunctionName(theArg) ).

To gain access to the new class, you will either need to put it in the same namespace as the code which will call it, or you will have import the namespace you put the class within into the form(s) which are going to use the code.

For example:

namespace A_Nice_Meaningful_Namespace
{
    public static class Utility
    {
        public string Boldify(string input)
        {
            return "<b>" + input + "</b>";
        }
    }
}

Open in new window


using A_Nice_Meaningful_Namespace;

namespace YourProjectName
{
    public class Form1 : Form
    {
        public Form1()
        {
            this.Text = Utility.Boldify("Hello World!");
        }
    }
}

Open in new window

Avatar of rwheeler23

ASKER

What I have in my case is a series of forms that allows the user to scrub data before it gets imported in its final resting place. What I want to do is have one piece of code that can be called from any one of this forms that actually performs the import. So all forms are in the same namespace as will the import code.
Do I add a new class, give it a new name and put the code in there or can I add the code within the same class?

For example: My code consists of frmA, frmB and frmC. Now I want to add code that is to be shared amonsgt all three forms. Following your lead I would need to create a new class called My_Import_Namespace and put the code in there. To execute the code in each form I would put something like My_Import_Namespace.DoImport

Do I have this correct?
Well I wouldn't call the class "My_Import_Namespace" because that sounds like a namespace  : )

But other than that you have the right idea. Just note that the convention "My_Import_Namespace.DoImport" (I kept the name for simplicity) will only work if the method is declared as static. If you do not mark the method as static, then you will have to create an instance of that class (which only a non-static class can have instances). Then the code might look like:

My_Import_Namespace instance = new My_Import_Namespace();

instance.DoImport();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much. I played with this both ways, with and without static, and I see what you mean. I am still wrestling with the concept of classes. You are correct, namespace and class are not the same. I just have to keep writing more code. Eventually VS C# with become just another programming tool I can use.
I just have to keep writing more code.
Couldn't agree with that statement any less. (Generally speaking.) It seems I'm writing code day (work) and night (fun/learning). The more code you write, the more things will become instinctive to you, sometimes even when you jump languages. Don't get discouraged if you are just starting out. It will come to you with time    = )
My first language was Fortan, then Basic, then Assembler, Pascal and C.  I do not think I am fighting with C# as much as I am with .Net Framework. I will just keep experimenting and finding things that work. Do the things that pay during the day and then play at night. Thanks again for the help.