Search

Tutorials, tips&tricks, snippets..

#KISS: KEEP IT SIMPLE, STUPID!

Category

Miscellaneous

Round number keeping trailing decimals without using string

I need all values to rounded to 8 decimal places. So, for example, 1.2 should round to 1.20000000, but Math.Round(value, 8) rounds to 1.2.

I’m using a C# class to populate and then serialise to xml. One of the values is a double. If I assign a ‘1.2’ to the value this gets serialised to ‘1.2’ when I actually need ‘1.20000000’.

I found this fast way to do this without converting the number to string:

foo = doubleValue + 0.00M;

And that makes the value 1.20000000 instead of just 1.2. This was better then doing a ToString and then parsing it back.

Advertisements

Convert a List<int> to a List<int?>

Here a very fast but useful snippet:

List<int?> nullList = myIntList.ConvertAll<int?>(x => x);

VIES: using European VAT Verification SOAP Service

EU created a service to obtain confirmation of the validity of the VAT identification number of any specified person. This service works with SOAP and can validate all the EU VAT. You just need to send the state code and the VAT.

This service works within the VIES environment (VAT Information Exchange System)

Continue reading “VIES: using European VAT Verification SOAP Service”

Traceroute an IP and list the hops using C#

This code snippet perfoms traceroute (“tracert command”) an IP and allow you to know all the hops with some infos, like hostname, reply time address and other stuff.

Below the code.
Continue reading “Traceroute an IP and list the hops using C#”

C#: right pad an array

Here there is a code snippet showing how to right pad an array (in this case, a byte array).

public static byte[] ByteArrayLeftPad(byte[] input, byte padValue, int len)
{
    var temp = Enumerable.Repeat(padValue, len).ToArray();
    for (var i = 0; i < input.Length; i++)
        temp[i] = input[i];
}

Here you can find how to left pad an array.

Wait Until A Condition Is True (and not stuck the CPU!!)

Sometimes the need arises to wait for a condition or property to become true, this is especially useful for unit / integration tests. The System.Threading namespace contains SpinWait which will enable use to enter a condition that we want to wait on to see if it becomes true.

Continue reading “Wait Until A Condition Is True (and not stuck the CPU!!)”

HOWTO: close the TCP connection of other process calling Windows API

We know that TCPview, for example, can close the TCP connection for the other process without closing the process. So how to implement it? Suppose there is a process connecting to the a remote ip and port, how to terminate that connection?

Continue reading “HOWTO: close the TCP connection of other process calling Windows API”

Fast and useful hint to analyze your disk usage

Just click here and discover the power of WinDirStat. If you have a full disk to clean up, this will be your savior.

CopyDatabaseWizard version error

This morning I found this annoying message in Event Viewer of my machine where I have a SQL Instance:

Activation context generation failed for “c:\program files (x86)\microsoft sql server\120\Tools\Binn\managementstudio\CopyDatabaseWizard.exe”.Error in manifest or policy file “c:\program files (x86)\microsoft sql server\120\Tools\Binn\managementstudio\CopyDatabaseWizard.exe” on line 8. The value “1.0” of attribute “version” in element “assemblyIdentity” is invalid.

A quick solution is:

Continue reading “CopyDatabaseWizard version error”

Powered by WordPress.com.

Up ↑