C#
Consider using System.IO.Path.Combine() instead of string concatenation
0http://dotnettipoftheday.org/tips/SystemIOPathCombine.aspx
Let’s review the following code for creating a file path:
public string GetFullPath(string fileName)
{
string folder = ConfigurationManager.AppSettings["MyFolder"];
return folder + fileName;
}
This code is prone to error. For example, when you set the folder setting, you have to remember to make sure it ends with a slash. To avoid such problems use Path.Combine() method which will ensure that the folder has ending slash:
public string GetFullPath(string filename)
{
string folder = ConfigurationManager.AppSettings["MyFolder"];
return Path.Combine(folder, filename);
}
?? operator (C#)
0http://dotnettipoftheday.org/tips/double_question_mark_operator_cs.aspx
The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand. For example:
int? x = null; ... // y = x, unless x is null, in which case y = -1. int y = x ?? -1;
The ?? operator also works with reference types:
//message = param, unless param is null //in which case message = "No message" string message = param ?? "No message";
Correct event invocation
0http://dotnettipoftheday.org/tips/correct-event-invocation.aspx
Be aware that if there are no subscribers a .NET event will be null. Therefore when raising the event from C# test it for null first.
public event EventHandler SelectedNodeChanged;
protected virtual void OnSelectedNodeChanged(object sender, EventArgs e)
{
//Event will be null if there are no subscribers
if (SelectedNodeChanged != null)
{
SelectedNodeChanged(this, e);
}
}
However in multithreaded application the last subscriber can unsubscribe immediately after the null check and before the event is raised. To avoid a null reference exception make a More >
DateTimePicker – reset focus
1Code Snippet – C#
private void dateTimePicker1_Enter(object sender, EventArgs e)
{
DateTimePickerFormat fmt = dateTimePicker1.Format;
dateTimePicker1.Format = DateTimePickerFormat.Long;
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.Format = fmt;
}
Code Snippet - VB.NET
Private Sub DateTimePicker1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.Enter
Dim fmt As DateTimePickerFormat = DateTimePicker1.Format
DateTimePicker1.Format = DateTimePickerFormat.Long
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.Format = fmt
End Sub
Inherit DataGridView in Windows Forms
8Create a user control class DataGridViewEx and paste the code snippet given. You can use this control where you want an inherited DataGridView.
Download source code & sample:
7z file – DataGridViewInheritance.7z
Zip file – DataGridViewInheritance.zip
Updated file – DataGridViewInheritance-Updated
.NET 4 – DataGridViewInheritance_NET4
using System;
using System.ComponentModel;
using System.Design;
using System.Drawing;
using System.Windows.Forms;
namespace DataGridViewInheritance
{
[Designer(typeof(System.Windows.Forms.Design.ControlDesigner))]
public partial class DataGridViewEx : DataGridView
{
public DataGridViewEx()
{
InitializeComponent();
}
}
}
Decimal to Byte array (vice versa)
14
/// <summary>
/// Decimal to Bye array
/// </summary>
/// <param name="dec">decimal</param>
/// <returns>byte array</returns>
public static byte[] DecimalToBytes(Decimal dec)
{
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(dec);
return stream.ToArray();
}
}
}
/// <summary>
/// Byte array to Decimal
/// </summary>
/// <param name="src">byte array</param>
/// <returns>decimal</returns>
public static Decimal BytesToDecimal(byte[] src)
{
if (src.Length == 1)
{
return Decimal.Parse(((char)src[0]).ToString());
}
using (MemoryStream stream = new MemoryStream(src))
{
using (BinaryReader reader = new BinaryReader(stream))
{
return reader.ReadDecimal();
}
}
}
Byte array to String (vice versa)
0
/// <summary>
/// Byte arrary to String
/// </summary>
/// <param name="bytes">byte array</param>
/// <returns>string</returns>
public static string BytesToString(byte[] bytes)
{
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetString(bytes);
}
/// <summary>
/// String to Byte array
/// </summary>
/// <param name="str">string</param>
/// <returns>byte array</returns>
public static byte[] StringToBytes(string str)
{
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetBytes(str);
}
Byte array to Integer (vice versa)
0
/// <summary>
/// Byte array to Integer
/// </summary>
/// <param name="bytes">byte array</param>
/// <returns>int</returns>
public static int BytesToInt(byte[] bytes)
{
return int.Parse(BytesToString(bytes).Trim());
}
/// <summary>
/// Byte array to String
/// </summary>
/// <param name="bytes">byte array</param>
/// <returns>string</returns>
public static string BytesToString(byte[] bytes)
{
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetString(bytes);
}
/// <summary>
/// Integer to Byte array
/// </summary>
/// <param name="value">int</param>
/// <returns>byte array</returns>
public static byte[] IntToBytes(int value)
{
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetBytes(value.ToString());
Hexadecimal to Integer (vice versa)
0
/// <summary>
/// Hexa decimal to String
/// </summary>
/// <param name="HexString">hexa decimal string</param>
/// <returns>int</returns>
public static int HexToInteger(string HexString)
{
return int.Parse(HexString, System.Globalization.NumberStyles.HexNumber);
}
/// <summary>
/// Integer to Hexa decimal string
/// </summary>
/// <param name="IntValue">int</param>
/// <returns>hexa decimal string</returns>
public static string IntegerToHex(int IntValue)
{
return IntValue.ToString("X");
}