Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, March 2, 2011

measure of response in method

I had the task to test a method and measure how much time was taken.

Here is how i did it:


Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();

FillDropDownList();
buttonContinue.Text = "it take... (ms): " + sw.ElapsedMilliseconds;

sw.Stop();


Notice that in order to use StopWatch you will need the System.Diagnostics.

Tuesday, January 25, 2011

Send Mail on a quick way

here is a sending e-mail on a quick way

string strTo = "christophw@sleeper.Dev.AlfaSierraPapa.Com"; string strFrom = "webmaster@aspheute.com"; string strSubject = "Hi Chris";  SmtpMail.Send(strFrom, strTo, strSubject,   "A real nice body text here");  
Response.Write("Email was queued to disk");

Friday, August 6, 2010

File.Copy method in C#



this is a quick example of how to use the copy method of the System.IO namespace

using System;
using System.IO;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string path2 = path + "temp";

try
{
// Create the file and clean up handles.
using (FileStream fs = File.Create(path)) {}

// Ensure that the target does not exist.
File.Delete(path2);

// Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} copied to {1}", path, path2);

so try it for the common use of files..

Tuesday, June 15, 2010

Example WebSiteMap



                                                       //close the node                                //close the node                                          //close the node

Tuesday, March 16, 2010

Polymorphism in C#

so here's a basic question on Polymorphism in C#, i found at SO. looking for the basics these days.

class A {
public virtual void Method1(){}

public void Method2() {
Method1();
}
}

class B:A {
public override void Method1() { }
}

class main {
A myobject
= new B();
myobject.Method2();
}

So which function gets called?
B.Method1();

gets called because it properly overrides the virtual method A.Method1();

Monday, March 15, 2010

VB.Net equivalent of C# “As”

Here is an equivalance of the as of C# to VB programmers.

in C# you do this:

var x = y as String;
if (x == null) ...

in VB you do this :

Dim x As String = TryCast(y, String)
If x Is Nothing Then ...

meaning  "as" = "TryCast"



Thursday, February 18, 2010

setting value to a RadDatePicker on load

I had to come with a page who has a link and returns to the selected values of a list . So here is the way i did it.


private void SetDateRequest(string dateValue, RadDatePicker datePicker, DateTime valueDate)
{
DateTime dateOut;
datePicker.SelectedDate = valueDate;
if (DateTime.TryParseExact(dateValue, "yyyy-MM-dd", null, DateTimeStyles.None, out dateOut))
{
datePicker.SelectedDate = dateOut;
}
}

and this is how I call it :

SetDateRequest(dateTo, ToDateRadDatePicker, endDate);


Cheers,

Tuesday, February 2, 2010

DateTime.ToString() display “A.M.” or “P.M.”


Is there any way to use the DateTime.ToString() method to display the meridiem of the time portion as "A.M." instead of "AM"?


here is the work around.

using System;
using System.Globalization;

class Program {
public static void Main() {
CultureInfo c = (CultureInfo)CultureInfo.CurrentCulture.Clone();
c.DateTimeFormat.AMDesignator = "A.M.";
c.DateTimeFormat.PMDesignator = "P.M.";
Console.WriteLine(DateTime.Now.ToString("tt",c));
}
}

This are a second way to try with a designator. But here you have a static date format and therefore miss the culture.


DateTime time = DateTime.Now;
string s = time.ToString("yyyy.MM.dd hh:mm:ss t.\\M.");
Console.WriteLine(s);



Cheers








Wednesday, January 27, 2010

Dictionary on C#

this will get a mapping for values of page names and attach the resource to redirecto to each value.

string previousPage = Request.QueryString["Src"];
string DP = Request.QueryString["DP"];
var dictionary = new Dictionary();
dictionary["AccountingDetail"] = "AccountingDetail.aspx?DP="+DP;
dictionary["Fixing"] = "Fixing.aspx";
dictionary["Payment"] = "Payment.aspx";

if (dictionary.TryGetValue(previousPage, out pageMap ))
{
Response.Redirect(pageMap );
}

and here is the data format when you want to pass a datetime value in de parameters of an url from a source page:

DataNavigateURLFormatString="MyPage.aspx?product={0}&category={1}&date={2:d}"





Wednesday, November 25, 2009

here they are Semantics.

speaking about semantics...heres a short example.

class car {
int wheels = 4;
string engine;
}

car mybike = new car();
mybike.wheels = 2;
mybike.engine = null;

The code is error-free, but is semantically incorrect. It reflects poorly on the programmer





Wednesday, November 18, 2009

Adding a value with Insert instead of Add method


Adding a value with Insert instead of Add method, wich gets me to the index, as an first argument
and the text as second argument.


var query = from filtxxxr in db.Entity
where fxxxr.Page == thispage
where fixxxr.Shared || filterUser > 0
orderby fixxxr.id descending
select new
{
id,
Name,
Page,
Shared
};
DropDownList.DataSource = query;
DropDownList.Items.Insert(0,"No Applied Filter");
DropDownList.DataValueField = "id";
DropDownList.DataTextField = "Name";
DropDownList.DataBind();

Tuesday, November 17, 2009

formating a datetimepicker

Some times we want to give a certain format on a datepicker control that we created. here's a quick example

private void picker_Loaded(object sender, RoutedEventArgs e)// the datapicker's load event.
{
this.picker.Text = DateTime.Now.ToString("dd-MMM-yyyy");
}

private void picker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
this.picker.Text = this.picker.SelectedDate.Value.ToString("dd-MMM-yyyy");
}

a quit of a short example

var valueDate = valueDatePicker.SelectedDate.Value.ToString("s");

returns the set with the ISO format (yyyy-mm-dd)




Wednesday, November 11, 2009

getting done things

public static string GetFilterExpression(int filterId)
{
var db = new UserQuery();
var filter = db.Filters.Single(f => f.Id == filterId);
var filterExpressions = filter.FilterExpressions;
var expressions = new List();
foreach(FilterExpression expr in filterExpressions)
{
expressions.Add(string.Format(expr.Operator.Operation, expr.ColumnName, expr.Value));
}
string logicalOperator = (filter.MatchAll) ? " AND " : " OR ";
return String.Join(logicalOperator, expressions.ToArray());
}

Friday, November 6, 2009

about how to calculate a checkin -checkout at nights


the solution to the date is like these in the case we just care for the entrance.