Tuesday, May 23, 2006

Lessons Learned From C# Log Parsing File

(1) To check for the existence of a file be sure to prefix System.IO:

Image1.Visible = System.IO.File.Exists(imagePath);

(2) For Loops are as follows:


for (int a =0; a<5;>

2.

{

3.

System.Console.WriteLine(a);

4.

}



(3) Read a file and split it up as an array as follows:


//variable "myFile"
StreamReader srRead = File.OpenText(fileName);
srRead.BaseStream.Seek(0, SeekOrigin.Begin);

while (srRead.Peek() > -1)
{
myFile = myFile + srRead.ReadLine() + "\n";
}

//Close the file
srRead.Close();

//Split the file into an array
//using a newline character as the split character
char[] splitChar = {'\n'};
string [] fileArray;
fileArray = myFile.Split(splitChar);
(4) Create a member function that tests using regex's as follows:

 // Function to test for Positive Integers with zero inclusive

public bool IsWholeNumber(string strNumber)
{
Regex objNotWholePattern=new Regex("[^0-9]");

return !objNotWholePattern.IsMatch(strNumber);


(5) DateTimes are as follows. Make certain to use the correct parameter order. It is not intuitive:

private DateTime dtDob;
private DateTime dtNow;
private DateTime dtAge;
private int intDay;
private int intMonth;
private int intYear;
private int intHour;
private int intMinute;
private TimeSpan tsAge;
 dtNow=DateTime.Now;
 dtDob=new DateTime(intYear,intMonth,intDay,intHour,intMinute,0);

When you subtract one DateTime from another you get a TimeSpan, not a number.

(5) Slicing a string:


string result = param.Substring(startIndex, length);


(6) Converting a string to an Int:

string myStr = "123";
int myParsedInt = Int32.Parse(myStr);

(7) Object Oriented Hello World


using System;

public class HelloWorld

{

public void helloWorld()

{

Console.WriteLine("HELLO WORLD");

}



public static void Main()

{

HelloWorld hw = new HelloWorld();

hw.HelloWorld();

}

}





(9) Incredibly, in the 1.1 version of C# you cannot test ints and most other objects to see if they are null. You can only test a string. You must therefore create an indicator string that you set to a value whenever you set an int variable to a non-null value. Then you test that string. Unbelievable.

0 Comments:

Post a Comment

<< Home