c# programming

20070724

GetDate Snippet

///<summary>
///    Returns a string representing the 

current date
///</summary>
///<returns>date as string; formatted as YYYYMMDD</returns>
public static string GetDateAsString()
{
    string date;
    DateTime now = DateTime.Now;
    date = now.Year.ToString();
    date += 

now.Month.ToString("00");
    date += 

now.Day.ToString("00");
    return (date);
}

///<summary>
///    Returns an integer representing 

the current date
///</summary>
///<returns>date as int; formatted as YYYYMMDD</returns>
///<seealso cref="GetDateAsString" 

/>
public static int GetDateAsInt()
{
    int date;
    date = 

Convert.ToInt32(GetDateAsString());
    return(date);
}

20070717

Easy Debugging in ASP.Net

to output text in a c# application (web or form)

Option #1:
include:
using System.Diagnostics;
private void main()
{
  functionName();
}

then outside of your functions
[Conditional("DEBUG")]
private void functionName()
{//this only displays when using debug compilation
  Console.WriteLine("debugging text output");
}

Option #2:
protected void Page_Load(object sender, EventArgs e)
{

  #if DEBUG
  //only displays when web.config compilation debug="true"
  Console.SetOut(Response.Output);
  Console.WriteLine("WEBSITE IS IN DEBUG MODE!");

  #endif
}
this is a good idea for web applications to remind you to disable debugging when you push a site to your production server.

Labels: