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.
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.