Reputation: 327
I have a question, is there a way to somehow save the code you write in some sort of file and call it in to my app? I have started to learn visual C# for a couple of weeks now and I've noticed as I am doing more complex applications the amount of code to write for a single operation is getting out of hand. For example i need to write things like
richtextbox1.text ="";
for multiple richtextboxes. I don't mind writing a lot of code but I wish I could somehow save it and call it by the file's name so I can be organized and be able to keep track of things. Is there such a way to do this?
example:
private void button_click(object sender, eventargs e)
{
do "from the file"
}
and that's all i need to write. all help is appreciated.
Upvotes: 0
Views: 504
Reputation: 931
If you are using Visual Studio, you could have a look at using code snippets. These can be a bit annoying to define (though there are plugins/addons that can help, eg Snippet Designer), but very useful when writing code that has common elements. If using Visual Studio 2005/2008, see this article which links to some examples. If using Visual Studio 2010, have a look here. Other IDE's probably also support snippets in some form.
A code snippet is a small piece of code that can be modified when you insert it into your file via the IDE. You can do things like click once to add timing code around a block of code, or easily create getters and setters (though most IDEs seem to offer this anyway).
If the code you want to add does exactly what existing code does, I would recommend refactoring, as per Daniel's answer. However if some small part is different, code snippets can be very useful.
Upvotes: -1
Reputation: 4517
Normally you put your code in other classes. You mention you have too much code in one file. The code in your file should only have something to do with the task. i.e MyApp form should only handle the very basics. Load/save and setting up the rest of the app.
Your code that has nothing to do with user interface you put in a so-called business object so you might reuse them someday.
The different sections in your ui, you put in smaller panels so these small panels will each have a file which will be nice and small. Repeat untill everything is in nice and small files.
That was just the 10k overview. In short a better question would be: how do I put different sections of my ui in userpanels. And how do I design classes with low coupling and high cohesion.
Upvotes: 2
Reputation: 3057
Would not calling a method be much easier?
Example:
private void restTextboxes()
{
richtextbox1.text = "";
richtextbox2.text = "";
}
private void button1_click(object sender, eventargs e)
{
resetTextboxes();
}
private void button2_click(object sender, eventargs e)
{
resetTextboxes();
}
Upvotes: 3