rwheeler23
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.
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.DoImpo rt
Do I have this correct?
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.DoImpo
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.DoImp ort" (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:
But other than that you have the right idea. Just note that the convention "My_Import_Namespace.DoImp
My_Import_Namespace instance = new My_Import_Namespace();
instance.DoImport();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 = )
ASKER
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.
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:
Open in new window
Open in new window