Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

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.
0
rwheeler23
Asked:
rwheeler23
  • 4
  • 3
1 Solution
 
käµfm³d 👽Commented:
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

0
 
rwheeler23Author Commented:
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?
0
 
käµfm³d 👽Commented:
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
Here's a quick example (I changed the name this time:  My_Import_Namespace == Utility").

public class frmA : Form
{
    // Some code

    public void A_Method_Which_Uses_the_Utility_Class()
    {
        Utility.SayHello();
    }

    // Some more code
}

public class frmB : Form
{
    // Some code

    public void A_Method_Which_Uses_the_Utility_Class()
    {
        Utility.SayHello();
    }

    // Some more code
}

public class frmC : Form
{
    // Some code

    public void A_Method_Which_Uses_the_Utility_Class()
    {
        Utility.SayHello();
    }

    // Some more code
}

public static class Utility
{
    public static void SayHello()
    {
        MessageBox.Show("Hello World!");
    }
}

Open in new window

0
 
rwheeler23Author Commented:
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.
0
 
käµfm³d 👽Commented:
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    = )
0
 
rwheeler23Author Commented:
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now