Saturday, March 21, 2015

C# Common Question

1) String vs string
string for creating variable
//string name = "Sam"

String for class methods and reference
// String.Concat(name, name2)

2) Count Occurrences of a String
Use Regex.Matches(), Not use Loop

3) Break vs Continue
Break - Exit Immediately
Continue - allow loop to continue (Stop the current and continue next )

5)
String can be null (reference type)
Datetime not null (value type )

6)
double x = 5.0;
int y =5;
console.writeline(x==y);

7) Ref vs Out

ref tells the compiler that the object is initialized before entering the function.
out tells the compiler that the object will be initialized inside the function.
ref is two-ways
out is out-only. (1 way )
out not allowed to leave it unassigned.


int a, b; 
swap(ref a, ref b); Compile Error (ref)

int a = 0;
int b = 0;

swap(ref a, ref b); Right (ref)

int a, b; 
swapOut(out a, out b); Right (out)

static void swapOut(out int o, out int p)
{
    int x = 0;
     x = o; Error(Out is not allow to assign);
     x = 1000;
     p = 2000;

}

10)Inheritance

If Parent have constructor, child also must have constructor

public Rectangle(double l, double w) //Parent
{
      length = l;
      width = w;

}

public Tabletop(double l, double w) : base(l, w) //Child 
{
}

10)Const Vs Readonly
const must be initialized, initialization must be at compile time
readonly can use default value, without initializing. initialization can be at run time

11)Convert.tostring vs .tostring()
Convert.tostring handle null.

Tostring() not handle null.


Sunday, March 15, 2015

Update & Delete Statement With Inner join

UPDATE Table1 SET Column1=@Value
FROM Table1 a
INNER JOIN Table2 b ON a.ID = b.ID
WHERE b.ID=

-----------------------------------

DELETE Table1
FROM Table1 a
INNER JOIN Table2 b on a.ID = b.ID
WHERE b.ID is not null

------------------------------------

Delete Top Number in Table

DELETE TOP (8) FROM Table1
WHERE ID = 1

Thursday, March 5, 2015

Extract From Excel File

// Row and Column Count is Start From 1  

private
List<long> SelectDefaultRouteClassByExcel()
{
    List<long> result = new List<long>();

    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    Excel.Range range;

    int rowCnt = 0;

xlApp = new Excel.Application();

xlWorkBook = xlApp.Workbooks.Open("D:\\Load\\Route\\excelList.xlsx", 0, true, 5, "", "",      true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true,      1, 0);

    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    range = xlWorkSheet.UsedRange;

    for (rowCnt = 2; rowCnt <= range.Rows.Count; rowCnt++)
    {
        var hasObject = ((range.Cells[rowCnt, 3] as Excel.Range).Value2);

        if (hasObject != null)
        {
           long item = range.Cells[rowCnt, 3] as Excel.Range).Value2);
           result.Add(item);
         }
     }

       xlWorkBook.Close(true, null, null);
       xlApp.Quit();

       releaseObject(xlWorkSheet);
       releaseObject(xlWorkBook);
       releaseObject(xlApp);

       return result;
}


private void releaseObject(object obj)
{
Try
{
       System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
     }
     catch (Exception ex)
     {
         obj = null;
         MessageBox.Show("Unable to release the Object " + ex.ToString());
      }
      finally
      {
          GC.Collect();
       }
}
----------------------------------

Date Time = DateTime.FromOADate(((range.Cells[rowCnt, 3] as Range).Value2))