main out of where it is created by default,
and into its own file, a good name for the file is Main.csmainmain
public class App
{
public static void Main()
{
try {
System.Windows.Forms.Application.ThreadException += System.Thread.ThreadExceptionEventHandler( OnAppException));
string logfile = System.AppDomain.CurrentDomain.BaseDirectory + "TraceLog.txt";
System.IO.TextWriter log = new System.IO.StreamWriter( logfile);
if ( Globals.Trace) {
System.Diagnostics.TextWriterTraceListener logger;
logger = new System.Diagnostics.TextWriterTraceListener( log);
System.Diagnostics.Trace.Listeners.Add( logger);
System.Diagnostics.Trace.WriteLine("App starting: " + DateTime.Now);
}
System.Windows.Form.Application.Run( new MainForm() );
} catch( Exception ex) {
App.OnAppException( null, new ST.ThreadExceptionEventArgs(ex));
}
}
private static void OnAppException( Object sender, System.Thread.ThreadExceptionEventArgs e)
{
Exception ex;
ex = e.Exception;
System.Windows.Forms.MessageBox.Show( "Halting due to error: " + ex.Message, "Fatal Error");
T.WriteLine( "Global Exception Handler:");
while( ex != null) {
T.WriteLine( ex.GetType().FullName);
T.Indent();
T.WriteLine( "ExceptMess: " + ex.Message);
T.WriteLine( "Trace: " + ex.StackTrace);
T.Unindent();
T.Flush();
ex = ex.InnerException;
}
T.Close();
System.Windows.Forms.Application.Exit();
}
}
internal target of this is public to other code
in the project, but private to other projects.
try {
throw new MyException();
} catch( MyException e) {
// handle MyException here
} catch {
// catch all for any exception type
} finally {
// Clean up stuff here, even if a return happens from anywhere,
// Unless the program is terminated in the catch block
}
In Visual Studio, goto Debug | Exceptions
System.Diagnostics.EventLog.WriteEntry( "XXX", ex.GetType().FullName); System.Diagnostics.EventLog.WriteEntry( "XXX", ex.Message); System.Diagnostics.EventLog.WriteEntry( "XXX", ex.StackTrace);
private void DrawBackground( Graphic g)
{
using (Brush backBrush = new SolidBrush(BackColor))
using (Pen borderPen = new Pen(BorderColor, 4))
{
g.FillRectangle(backBrush, new Rectangle(10, 10, 50, 50);
g.DrawRectangle(borderPen, new Rectangle(8, 8, 54, 54);
}
}
Inspired by Ten Traps in C# for C++ Programmers
// sort of like this, but you cannot actually use classes, just functions:
[DllImport("apibridge.dll")] public static extern class CBridgeApiClass {};
using System; using System.IO; TextWriter tw = new StreamWriter( "date.txt"); tw.WriteLine( DateTime.Now); tw.Close(); TextReader tr = new StreamReader( "date.txt"); Console.WriteLine(tr.ReadLine()); tr.Close();
@"this \n is not interpreted as newline"String.Compare( str1, str2, true);// Note this only: // 1) closes all windows // 2) exits window message loops // 3) returns from Application.Run() // 4) so code after Application.Run() then gets executed (of which there is usually none) Application.Exit()
System.Environment.Exit( int nStatus);
// To Get it:
System.Reflection.Assembly assm = System.Reflection.Assembly.GetExecutingAssembly ();
System.IO.Stream stream = assm.GetManifestResourceStream ("afoNETExtension.command.bmp");
System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromStream (stream);
m_HBitmap = bitmap.GetHbitmap ();
// To supply the DeleteObject func from gdi32.dll:
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
// To Release it:
DeleteObject (m_HBitmap);
// Context-bound type with the Synchronization context attribute.
[Synchronization()]
public class SampleSyncronized : ContextBoundObject {
// A method that does some work, and returns the square of the given number.
public int Square(int i) {
Console.Write("The hash of the thread executing ");
Console.WriteLine("SampleSyncronized.Square is: {0}",
Thread.CurrentThread.GetHashCode());
return i*i;
}
}
Making code with compliant and non compliant parts be compliant:
using System;
// Assembly marked as compliant.
[assembly: CLSCompliantAttribute(true)]
// Class marked as compliant.
[CLSCompliantAttribute(true)]
public class MyCompliantClass {
// Method marked as not compliant.
[CLSCompliantAttribute(false)]
public void ChangeValue(UInt32 value){ }
public static void Main( ) {
int i = 2;
Console.WriteLine(i);
}
}
// A bit field enumeration
public enum SupportedSpeeds
{
none = 0,
b1200 = 1,
b9600 = 2,
b19200 = 4,
b56000 = 8,
All = b1200 | b9600 | b19200 | b56000,
}
SupportedSpeeds mySpeeds = b1200 | b19200;
[DllImport("user32.dll")]
public extern static int SetParent(int child ,int parent );
void setParent( int parent)
{
SetParent( form.Handle.ToInt32(), parent);
}
System.Windows.Forms.MessageBox.Show( "Message Goes Here");
public class Parent
{
public int getCount() {
return 5;
}
}
public class Child : Parent
{
public int getCount() {
return 7;
}
public int getParentsCount() {
return getCount(); // Bad returns Child Count
return base.getCount(); // returns parents count
}
}
this.StartPosition = FormStartPosition.Manual; this.Location = new Point( 0, 0);
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = "C:\\";
dlg.Filter = "Images|*.img|All files|*.*";
if ( dlg.ShowDialog(this) == DialogResult.OK)
{
}