Saturday, June 21, 2008

file operation [C# VS2005]

Some notes about file operation that I have used before, including write file, listing file in directory, delete file. To use file/directory existing function don't forget to include System.IO

try
{
sPath = "C:\\TEMP";
if( Directory.Exists(sPath) )
{
string[] sFiles = Directory.GetFiles(sPath);
for (int i = 0; i < sFiles.Length; i++)
{
File.Delete(sFiles[i]);
}
}
else Console.WriteLine( sPath + " directory is not exist!");
}
catch (Exception ex)
{
Console.WriteLine( ex.Message );
}

Let say that I want to delete all files in a specified folder but excluding its sub folder, first I check whether the path is valid or not => Directory.Exists, if valid then next step is I populate all files in the directory => Directory.GetFiles, the last one is we iterate and delete all existing files in directory => File.Delete

try
{
string sErrLogPath = "C:\\TEMP";
string sErrMsg = "Error to be written...";
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString("00");
string sDay = DateTime.Now.Day.ToString("00");
string sErrFilePath = sErrLogPath + "\\" + sYear + "_" + sMonth + "_" + sDay;
string sFileName;
if( !Directory.Exists( sErrFilePath ) )
{
Directory.CreateDirectory(sErrFilePath);
}
sFileName = "ErrLog" + DateTime.Now.Hour.ToString("00") +
DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00");
StreamWriter sw = new StreamWriter(sErrFilePath + "\\" + sFileName + ".txt", false);
sw.Write(sErrMsg);
sw.Close();
}
catch (Exception ex)
{
Console.WriteLine("Failed to write log file " + ex.Message);
}
Sometimes I want to make a note in a file for errors in my programs, so I just write the error message in file I created, rather than have to debug it first, quite helpful for me :P. In the code above I'm trying to make sub folder in C:\TEMP that contains my error message, the sub folder name format is YYYY_MM_DD. After that I create file for errors naming with ErrLog
hh_mm_ss

Monday, June 9, 2008

postback [C# ASP.NET VS2005]

I have often heard this term, so I thought it might be useful if I add some notes to remind me. Postback is a condition when a web page send data to itself as a target from the form action. The Page know whether it is postback or not from its property : bool Page.IsPostBack. If it return true then it is postback.

Few notes about postback related to DropDownList component. I add the following code in my website to fill the drop down list with number from 15-30. The result for the following code is a DropDownList filled with empty string as it selected value and number from 15-30.
protected void Page_Load(object sender, EventArgs e)
{
#region Fill Age Dropdownlist
ddlAge.Items.Clear();
ddlAge.Items.Add("");
for (int i = 15; i <= 30; i++)
{
ddlAge.Items.Add(i.ToString());
}
#endregion
}
Assuming that I put this DropDownList named ddlAge in a form and submit it to itself to make a postback condition. The result is whatever value I choose in the ddlAge and then submit it, it always show empty string value. Why is that? Because the code to fill ddlAge is always run, regardless it is postback or not, so it will always fill again after the form submit to itself and the default value is always empty string. How do we solve it? We must check whether it is postback or not, if it is, then no need to run code for filling the ddlAge again. See the following code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
#region Fill Age Dropdownlist
ddlAge.Items.Clear();
ddlAge.Items.Add("");
for (int i = 15; i <= 30; i++)
{
ddlAge.Items.Add(i.ToString());
}
#endregion
}
}
Kindly refer to other references :

Sunday, June 1, 2008

partial class [C# ASP.NET VS2005]

Partial class allow us to separate definition of one class in separate file, but it behave like usual class, u can call a function that define in other partial class.
So what the use of it?
  • when the size of the file is getting bigger, it is harder to maintain, so one of the way is to split classes base on its functionality.
  • it will allow programmers to add some definition of the class simultaneously
  • when working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.
class definition without partial class, for example we save the code in Company.cs
using System;
using System.Collections.Generic;
using System.Text;
public class Company
{
static void Main(string[] args)
{
ITDepartment a = new ITDepartment();
HRDepartment b = new HRDepartment();
Console.ReadLine();
}

private class ITDepartment
{
public ITDepartment()
{
Console.WriteLine(”itdepartment”);
}
}

private class HRDepartment
{
public HRDepartment()
{
Console.WriteLine(”hrdepartment”);
}
}
}
we can break down the code into several files
Company.cs
using System;
using System.Collections.Generic;
using System.Text;

public partial class Company
{
static void Main(string[] args)
{
ITDepartment a = new ITDepartment();
HRDepartment b = new HRDepartment();
Console.ReadLine();
}
}
Company.HRDepartment.cs
using System;
using System.Collections.Generic;
using System.Text;

public partial class Company
{
private class HRDepartment
{
public HRDepartment()
{
Console.WriteLine("hrdepartment");
}
}
}
Company.ITDepartment.cs
using System;
using System.Collections.Generic;
using System.Text;

public partial class Company
{
private class ITDepartment
{
public ITDepartment()
{
Console.WriteLine("itdepartment");
}
}
}
Kindly refer to this tutorial for other reference

as keyword for typecast [C# ASP.NET VS2005]

Usually in C or C++, we use this kind of typecast
float b = 10.9f;int a;
a = (int)b;
But the type that you are making type cast must be reference type such as object, primitive data type is not by reference but by value. So if you force using as like this example below it will give an error.
a = b as int;
//Error:The as operator must be used with a reference type ('int' is a value type)
Kindly refer to this tutorial for other reference.

#region [C# ASP.NET VS2005]

#region is used to block an area and create a collapsible structure on the editor, you can also put comments on it. It will make your code more neater.
#region put your comment over here...
if (true) Console.Write("Hello nel..");
#endregion