Inline function within XAML

Most of the time, the event in XAML is assigned to a codebehind function
Example:
*.xaml
<button click="OnBtnClicked" content="Click Me" height="30" name="btn" width="80"></button>
*.cs
void OnBtnClicked(object sender, RoutedEventArgs e)
{
        btn.Content = "Thank you";
}


Another way is to use inline code in the XAML file by using <x:code> element.
<button click="OnBtnClicked" content="Click Me" height="30" name="btn" width="80">
  <x:code>
    <![CDATA[
      void OnBtnClicked(object sender, RoutedEventArgs e)
      {
        btn.Content = "Thank you";
      }
    ]]>
  </x:code>
</button>

Read more...

Comparison for C# and VB.Net


C#VB.Net
Case sensitiveYesNo
Self reference to current objectthisme
No explicit instance of object neededstaticShared
Refer to base classbaseMyBase
Declare class that can't be inheritedsealedNotInheritable
Declare method that can't be overriddensealedNotOverridable
Declare method that can be override in derived classvirtualOverridable
Declare method that must be override in derived classabstractMustOverride
Declare a class must be inherited. An instance of the class cannot be createdabstractMustInherit
Comments/* comment */
//comment
 'comment
Empty objectnullNothing
Method that does not return valuevoidSub
Declare a variableint a = 0;Dim a as Integer = 0
Declare an arrayint[] a =new int[3];Dim a() As Integer
Declare an enumerationenumEnum
...
End Enum
Iteration loop structurefor
foreach
For ... Next
For Each ... Next
Conditional loop structurewhileWhile ... End While
Conditional statementif ... else ...If...Else...End If


Operator and data types
C#VB.Net
Equal===
Not equal!=<>
Division/\
Modulus%Mod
Logical AND&&And
Logical OR||Or
Logical NOT!Not
Integer datatypeintInteger
String datatypestringString
Boolean datatypeboolboolean
Byte datatypebyteByte
Decimal datatypedecimalDecimal
Float datatypefloatSingle
Double datatypedoubleDouble
Date datatypeDateTimeDate


Read more...

C# - Monitoring folder and files

If you want to get a notification whenever a folder or any files is changed, .Net Framework provides a class name FileSystemWatcher to ease your coding. It provides an event for any files that is modified, created or deleted.

FileSystemWatcher fileWatcher = new FileSystemWatcher();
fileWatcher.Path = "C:\Data";
fileWatcher.Filter = "*.txt";
fileWatcher.IncludeSubdirectories = false;
fileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
fileWatcher.Changed += new FileSystemEventHandler(OnFileChanged);

The 'Filter' property is used to set what type of file being monitor. If you want to monitor all file types, just change it to "*.*" or you can specify other file type like "*.jpeg", "*.wmv" or others. The 'IncludeSubdirectories' as the name suggested is set to either include the sub directory in FileSystemWatcher monitoring or ignore it. The 'NotifyFilter' is used with Changed event, when the file attribute like last write or file name is change, the Changed event will be invoke. There is also some other event for this class as shown below:

// When a file is created in the folder
fileWatcher.Created += new FileSystemEventHandler(OnFileModified);
// When a file is deleted from the folder
fileWatcher.Deleted += new FileSystemEventHandler(OnFileModified);
// When a file in the folder is renamed
fileWatcher.Renamed += new FileSystemEventHandler(OnFileModified);

fileWatcher.EnableRaisingEvents = true;

private void OnFileModified(object sender, FileSystemEventArgs e)
{
    if (e.ChangeType == WatcherChangeTypes.Created || e.ChangeType == WatcherChangeTypes.Deleted)
    {
        ....
    }
}


You must set the 'EnableRaisingEvents' as true to start the FileSystemWatcher.

Read more...

C# - Simple SQL command

Here are some simple code in C# if you want to select data from your database.
Product
IdProductNameDescription


DataTable dataTable = new DataTable();
string connStr = "Data Source=MachineName\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True";
string query = "select Id, ProductName, Description from Product";
using (SqlConnection conn = new SqlConnection(connStr))
{
    SqlDataAdapter da = new SqlDataAdapter(query, conn);
    da.Fill(dataTable);
}

foreach (DataRow row in dataTable.Rows)
{
    Console.WriteLine(row["Id"].ToString(), row["ProductName"].ToString(), row["Description"].ToString());
    ....
}

Data Source is the name of the database server. Initial Catalog is the database name. If you set the Integrated Security to true, it will used the Windows account of the current process to login to the server. In case the user id and password is different from the windows account, use this

string connStr = "Data Source=MachineName\SQLEXPRESS;Initial Catalog=DatabaseName;User ID=username;Password=yourPassword";



You can also use SqlCommand to read data from the database. For other SQL command like insert, delete and update, I'm not sure if there is other way beside using SqlCommand.

using (SqlConnection conn = new SqlConnection(connStr))
{
    SqlCommand dataCommand = new SqlCommand();
    dataCommand.Connection = conn;
    dataCommand.CommandText = "update Product set ProductName='NewProductName' where Id='30'";

    conn.Open();
    int x = dataCommand.ExecuteNonQuery();
    conn.Close();
}

Read more...

Google Wave invite

If you are looking for a Google Wave invite, just leave a thank you and your e-mail. I will sent it to you. Current invitation left 13 and after it is send, the invitation might take 1 or 2 days to reach you.


Read more...
top