Last couple of weeks I have been busy trying out my new project in VS2010 / C#. Couple of basic exercises for my learning purpose are listed below. Please modify the code to make it work.
Tip #1
- Working on Folder Open and Browse Dialog for a Windows FormsApp
- Best option is to reuse codeplex code. Link
Tip #2
- Working with Threads
- Below is a Simple Threadpool (For sum of two numbers). Try it out as a Forms app so see how operations are performed parallely.
using System;
using System.Threading;
using System.Windows.Forms;
class InputNumbers
{
public int a { get; set; }
public int b { get; set; }
}
class Example
{
public Example()
{
// Declare a new argument object.
InputNumbers IS = new InputNumbers();
IS.a = 10;
IS.b = 20;
for(int i = 0; i<100 ; i++)
{
IS.a = i;
IS.b = i+2;
// Send the custom object to the threaded method.
ThreadPool.QueueUserWorkItem(new WaitCallback(AddNumbers), InputNumbers);
}
private void AddNumbers(object a)
{
InputNumbers IS = a as InputNumbers;
Messagebox.show((IS.a+IS.b).ToString());
}
}
Tip #3
To keep methods seperate, DB Layer, Web Methods and UI Layer, I decided to keep them in different classes. This
question was useful for my learning for this tip. Below is a simple example.
Class A
Class B
ProgramFile.cs
All classes in different file
classA.cs
using System;
namespace class_A
{
class classA
{
public void methodA()
{
Console.WriteLine("In Method A");
}
}
}
classB.cs
using System;
namespace class_B
{
class classB
{
public void methodB()
{
Console.WriteLine("In Method B");
}
}
}
program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using class_A;
using class_B;
namespace ClassExamples
{
class Program
{
static void Main(string[] args)
{
classA A = new classA();
classB B = new classB();
A.methodA();
B.methodB();
Console.ReadLine();
}
}
}
Tip #4
- Working with Log4Net
- The following two posts link1, link2 were useful to get started.
- Below is simple Windows Forms App
- Add the Log4Net DLL in project references
LogHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
[assembly: log4net.Config.Repository()]
namespace loggingns
{
public class Loghelper
{
#region Data Members
public static readonly ILog testLogger = LogManager.GetLogger("log4NetExample");
#endregion
}
}
Program.cs
Invoke the following method to log entries . Use the namespace loggingns in Form1.cs.
using log4net;
using log4net.Config;
using log4net.Core;
using loggingns;
Sample Test method
public void testMethod()
{
Loghelper.testLogger.Debug("Debug Log");
Loghelper.testLogger.Info("Info Log");
}
App.config file entry
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<appSettings file="" >
<clear/>
<add key="log4net.Internal.Debug" value="false"/>
</appSettings>
<!-- This section contains the log4net configuration settings -->
<log4net debug="true">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<layout type="log4net.Layout.XMLLayout" /> -->
<param name="File" value="TestLoggerExample.log"/>
<param name="AppendToFile" value="false" />
<layout type="log4net.Layout.PatternLayout">
<header type="log4net.Util.PatternString" value="[START LOG] %newline" />
<footer type="log4net.Util.PatternString" value="[END LOG] %newline" />
<conversionPattern value="%d [%t] %-5p - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<!-- Specify the level for some specific categories -->
<logger name="log4NetExample">
<!-- <appender-ref ref="B" /> -->
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</logger>
</log4net>
</configuration>
I hope these tips would help for future reference.
More Reads
N-Tier Architecture Best Practices, Part 1: 2-Tier Architecture with just a Data
N-Tier Architecture Best Practices, Part 2: 3-Tier Architecture with interfaces and a Data Tier
Happy Learning!!