Saturday, 23 June 2012

How to Read And Write List to file

c#, load List from file uses the StreamReader ReadLine 


List<string> dataFile = new List<string>();          
using (StreamReader r = new StreamReader(edSourceFileToReplaceStrings.Text))
while ((lineData = r.ReadLine()) != null)
    dataFile.Add(lineData);


c#, save List<String> to file uses the StreamWriter WriteLine

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@oDialog.FileName))
foreach (string line in dataFile)
    file.WriteLine(line);

Friday, 23 December 2011

C# Modulo Operator - Find easy the remainder of a division.






Percentage symbol



You want to see examples of the modulo division operation in the C# language, which allows you to execute code once every several iterations of a loop. The modulo operator uses the percentage sign % character in the lexical syntax and has some unique properties. Here we look at the modulo division operation.
Estimated costs of instructions

Add:         1 ns
Subtract:    1 ns
Multiply:  2.7 ns
Divide:   35.9 ns

Example 1

Thursday, 8 December 2011

c# How to Throw Exception and how to bubble up an Exception


problem

c# - How to throw exceptions, how to bubble up exceptions, how to digg exceptions to find deep error messages and Exceptions.
difficulty level

7/10 :|
compatibility

c#
solution

When you catch an Exception and you want to bubble up (forward) the same exception, you should bubble it up by your own, define it in the 2nd argument of your new Throw Exception. When you bubble up the Exception, you can find it as .InnerException.

try{
  int i=2/0;  //division by zero is not allowed anyway
} catch (Exception e) {
    throw new Exception(
        "Something get wrong, "+
        "error message: "+e.Message,
        e  // here we bubble up the exception
    );
}


The follow code demonstrates how car your dig the InnerException you find all error messages:

string output = "Error messages: ";
Exception exp = exceptionToBubble;
string expmessage = "";

while (exp != null) {
    expmessage = "inner error: " + exp.Message + " " + expmessage;
    exp = exp.InnerException;
}
expmessage += "final error: " + exceptionToBubble.Message;

output += expmessage;

Also, don't forget the .Trace property of the Exception object where show related info.

Wednesday, 30 November 2011

c# how to dispose objects of Dictionary

 
problem

c# dispose objects of Dictionary
you cannot dispose the object of a Dictionary with "foreach" process, you can do with "while" getting the Enumerator of the Dictionary.
difficulty level

5/10 :|
compatibility

c#
solution

In the follow example, I scan serially a Dictionary generic of <string, IInDataObject> type and I dispose its hosted objects.
Each object in the dictionary, each Value is IInDataObject interface. This interface implements IDisposable so can call Dispose();

//get the Enumerator in order to scan with while (and not with foreach)
System.Collections.IEnumerator enumerator = dataObjectList.GetEnumerator();
//perform the Enumerator in while loop
while (enumerator.MoveNext()) {
    //get the pair of Dictionary
    KeyValuePair<string, IInDataObject> pair =
        ((KeyValuePair<string, IInDataObject>)(enumerator.Current)); 
    //dispose it
    pair.Value.Dispose();
       
}

Tuesday, 29 November 2011

csharp, process the items of Dictionary serial and faster

The Dictionary type abstracts out its looping logic into enumerators: these are accessed through the foreach loop and the GetEnumerator method. In this article, we demonstrate the GetEnumerator method, which exhibits better performance than the foreach loop.

Examples

Let's examine the most common and easiest way to loop through a Dictionary instance. The foreach loop here actually compiles into intermediate language that uses the GetEnumerator method, MoveNext, and Current, as well as a try/finally block.
Finally
Looping over Dictionary with foreach [C#]

static int A(Dictionary<string, int> d)
{
    int a = 0;
    foreach (var pair in d)
    {
 a += pair.Value;
    }
    return a;
}
Next, this method demonstrates the GetEnumerator and MoveNext methods and the Current property directly. This code is compiled to the same intermediate language except the try/finally block is absent.
Looping over Dictionary with GetEnumerator [C#]

static int B(Dictionary<string, int> d)
{
    int b = 0;
    var enumerator = d.GetEnumerator();
    while (enumerator.MoveNext())
    {
 var pair = enumerator.Current;
 b += pair.Value;
    }
    return b;
}

Monday, 28 November 2011

c# - how to move a project to different file location

- Get backup of the Directory with Project (you don't know what Microsoft can do for you)
- From Visual Studio remove the project for Solution
- If you use Source safe do Check in the pendings (ctrl-wg). This step is important if you use Source safe!
- Move the directory of the Project to the new directory location you desire.
- Remove all *.*scc files from all directories of the project. Some of them might be hidden and read only, so pay attention.
- Right click on Solution and click on Add -> Existing project
- Navigate the dialog and open the .csproj file you placed in new file location
- If you use the Safe Source you might get the misleading message "The associated source control plug-in is not installed or vould not me initialized". Select "Temporarily work uncontrolled" an click ok.
- If you use Source safe do Check in the pendings (ctrl-wg). This step is important if you use Source safe!
- Rebuild your solution and your will get error because this Project is not referenced any more to other Project that was before we removed it. So add this Project in References of the other project where use it.

Thursday, 24 November 2011

mssql - compare two datetime fields


problem

mssql - compare two datetime
Mssql, compare two datetime fields, if their values are close, if are in 24h difference and do on
---
> dennis
difficulty level

1/10 :)))
compatibility

mssql
solution

In mssql, the datetime fields are actually numeric with decimals. The integer part in the “days”. So if you want to compare two dates compare the integer part as bellow

-- compare if the item is sold 24h before the item is modified
Select * from items
Where ModificationDate<SaleDate-1

-- find person(s) that born on 12/11/2010 (not exactly on the same, with at least 12 hours difference)
Select * from persons
Where birthdate-0.5>=’2010-12-11’ and birthdate+0.5<=’2010-12-11’