Sunday, April 15, 2007

How to use Number Types in c#

If you need fractions: Use decimal when intermediate results need to be rounded to fixed precision - this is almost always limited to calculations involving money. Otherwise use double - you will get the rounding of your calculations wrong, but the extra precision of double will ensure that your results will be good enough. Only use float if you know you have a space issue, and you know the precision implications. If you don't have a PhD in numeric computation you don't qualify. Otherwise: Use int whenever your values can fit in an int, even for values which can never be negative. This is so that subtraction operations don't get you confused. Use long when your values can't fit in an int. Byte, sbyte, short, ushort, uint, and ulong should only ever be used for interop with C code. Otherwise they're not worth the hassle.

Powered by Bleezer

Monday, April 09, 2007

Send Msmq and Xml message

Dim oMq As New MessageQueue(sQName)Dim oMsg As New Message oMsg.Label = DateTime.Now.ToString()oMsg.Body = sBodyoMsg.Formatter = New ActiveXMessageFormatter 'required in order to send xml as-it-isoMsg.Recoverable = True 'recoverable is set so that the message can be saved in disk to recover from server crashoMq.Send(oMsg)

oXmlDoc = New XmlDocument 'builds oXMLRoot = oXmlDoc.CreateElement("Data_Point")oXmlDoc.AppendChild(oXMLRoot)oXMLRoot.SetAttribute("MsgId", "103") 'mandatory oXMLRoot.SetAttribute("Version", "1") 'mandatory oXMLVarNode = oXmlDoc.CreateElement("Variable")oXMLVarsNode.AppendChild(oXMLVarNode)oXMLVarNode.SetAttribute("ID", "var1") 'variable nameoXMLVarNode.SetAttribute("Date", DateTime.Now.ToString()) 'to override item datetime stamp 'builds 334.45oXMLValueNode = oXmlDoc.CreateElement("Value")oXMLVarNode.AppendChild(oXMLValueNode)oXMLValueNode.InnerText = "234.54" 'datapoint value

Powered by Bleezer

How to use Timers in .NET

Excerpts From: http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx

There are three different timer classes in the .NET Framework Class Library: System.Windows.Forms.Timer, System.Timers.Timer, and System.Threading.Timer. The first two classes appear in the Visual Studio® .NET toolbox window, allowing you to drag and drop both of these timer controls directly onto a Windows Forms designer or a component class designer. If you're not careful, this is where trouble can begin.

System.Windows.Forms.Timer will not raise events that occur while the UI thread is unable to process them, whereas System.Timers.Timer will queue them to be processes when the UI thread is available.

the instances of System.Threading.Timer are not inherently thread safe, given that it resides in the System.Threading namespace

Powered by Bleezer