Monday, 1 October 2012

C# - Classes and Member (Static/Non-static)

Static Classes

(1) Cannot be instantiated (no instance)
/***********Static Class - FactorialClass ***********/
public static class FactorialClass //Static Class declaration
...
FactorialClass a = new FactorialClass (); // <= Not allow
/***********Static Class - FactorialClass ***********/

(2) Contain only static member
/***********Static Class - FactorialClass ***********/
    public static class FactorialClass                          //Static Class
    {
        public static int Factorial(int i)                      //Static Member
        {
            return ((i <= 1) ? 1 : (1 * Factorial(i - 1)));
        }
        public int Factorial2(int i)                      //<= Non Static Member not allowed
        {
            return ((i <= 1) ? 1 : (1 * Factorial(i - 1)));
        }
    }
/***********Static Class - FactorialClass ***********/

(3) Is Sealed (cannot inherit)

(4) Cannot contain instance constructor

(5) Access static member using class itself
/***********Static Class - FactorialClass ***********/
            int fac = FactorialClass.Factorial(5);          // Static class, all member must be static
/***********Static Class - FactorialClass ***********/

Static Member

(1) Static member can defined in static class or non-static class
  • public static class FactorialClass  //Static Class
  • public class FactorialClass           // Non Static Class

(2) Always accessed by the class name
  •  int fac = FactorialClass.Factorial(5); // Static class, all member must be static

(3) Can be called without instantiated

(4) Does not exist in instances.
  • AddClass a = new AddClass();     //Instance a does not contain static member






Monday, 24 September 2012

C# - Skeleton of a C# Program

  1. C# program can consist of one or more files (cs)
  2. Each file can contain zero or more namespaces
  3. Each namespace can contain types such as classes, structs, interfaces, enumerations, delegates, other namespaces

/*****************************************/
// A skeleton of a C# program
using System;
namespace YourNamespace
{
    class YourClass
    {
    }
    struct YourStruct
    {
    }
    interface IYourInterface
    {
    }
    delegate int YourDelegate();
    enum YourEnum
    {
    }
    namespace YourNestedNamespace
    {
        struct YourStruct
        {
        }
    }
    class YourMainClass
    {
        static void Main(string[] args)
        {
            //Your program starts here...
        }
    }
}
/*****************************************/

VC++ - LNK1000 error when incrementally linking

Error Description from Microsoft
http://support.microsoft.com/kb/948127

Download Hotfix from Microsoft
http://archive.msdn.microsoft.com/KB948127/Release/ProjectReleases.aspx?ReleaseId=871

C# - Create DLL Project

New Project
  • Project Types = Visual C#
  • Templates = Class Library


Project Properties
  • Output Type = Class Library
  • Output Path = "bin\Debug\"
 

Compile and Build => dll file will be created in output path.

Example:
  1. Type the following in the Class1.cs
  2. /**********************************/
    namespace MathFunctions
    {
        public class AddClass
        {
            public static int Add(int a, int b)
            {
                return (a + b);
            } 
        }
    }
    /**********************************/
  3. After compile and build, MathFunctions.dll file is created in output bin. (MathFunctions.dll file name is based on Assembly name in Project Properties)
  4. User can then use the dll by
    • Add reference the MathFunctions.dll
    • Using MathFunctions;
  5. Then "AddClass" can now be use in the user project.
External Link
http://www.codeguru.com/csharp/csharp/cs_misc/dllsandexecutables/article.php/c4239/Creating-and-Using-C-DLLs.htm