In a C# program, I have an error check that gets repeated a lot:
try
{
File.Move(searchfolder + question1 +"_"+ filestring +".txt",
searchfolder + question1 +".txt");
}
catch (Exception ex)
{
File.AppendAllText(adminfolder + question1 +"_l.txt", "!");
side.Value = Convert.ToString(ex) + "[Check-In error at "
+ Convert.ToString(MYLINE) +"] "+ side.Value;
}
MYLINE is some number, and MYLINE is the only thing that changes across my program.
So a normal C++ #define macro would make this much simpler to work with (I would just write the full “#define CHECKIN(MYLINE) …” once at the top of the program).
How would a pro deal with this in C#?
Well, perhaps, but since C# doesn’t have a concept of macros… just use a method:
On a side note, there is glaring problem in your code. In your
catchblock you callFile.AppendAllText()… which, of course, can throw an exception as well. You need to account for that.