c# - winform - how to avoid repetitive task
-
Saturday, April 14, 2012 3:21 PM
In vba I had the "call" procedure, i.e. - execute some stored procedures, to avoid repeating the same line of code menu times.
Whati is the way to do it in c# - winform?
I should populate my datagridview from multiple buttonsClickEvents. And i want to call the same lines of code - stored somewhere.
All Replies
-
Saturday, April 14, 2012 3:39 PM
You can create a function and achieve this
function void SomeMethod() { // Code goes here } //Now you can call it as SomeMethod();
A.m.a.L Hashim

Dot Net Goodies
- Marked As Answer by Bonaca Saturday, April 14, 2012 3:52 PM
-
Saturday, April 14, 2012 3:46 PM
Hi Bonaca,
you can create Procedure like FillDataGridView() as below
private void FillDataGridView() { // DataGridView Fill Code goes here; }
private void btnModify_Click(object sender, EventArgs e)
{
FillDataGridView();
}Regards,
Gautam
- Marked As Answer by Bonaca Saturday, April 14, 2012 3:51 PM
-
Saturday, April 14, 2012 3:51 PM
Thank You ! Both solutions - Works.
Especially thanks for fastReplay.
-
Saturday, April 14, 2012 4:05 PM
I now tried to put all that code on separate file - CodeFile1.cs.
And I get the error:
"Expected class, delegate, enum, interface, or struct"
I supose sam linking of that file with my form is needed.
What is the way, pls?
-
Saturday, April 14, 2012 4:49 PMModeratorI suspect that you didn't actually create a class in that new file. You can't have a method without it being in a class. Post the code you tried to use ...
~~Bonnie Berent DeWitt [C# MVP]
geek-goddess-bonnie.blogspot.com -
Saturday, April 14, 2012 5:04 PMAt a guess you just need to copy the class declaration from the Form1 file i.e. make it a partial class in the new file. You will of course need to add a closing brace at end of file.
Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute. -
Saturday, April 14, 2012 5:36 PM
Yes, a class declaration was missing. ThankYou.
I just moved myself from vba to c#.
My far goal is to make an application for alarm security agency.
Some buildings (sometimes over 1000 objects) have alarm sensors and if they are activated, alarm signal travels (by phone or by IP) to the call centar, where operaters have some application to deal with that alarm signals. That application - that's it - of my interrest. Possibly - i would have a client for such application.
I ask myself - maybe some template exists? Or tutorial, book...whatever i could use. Google search gives very poor help.

