by David Loo | Mar 22, 2014 | C#, General, Uncategorized, Utilities
I have been working on .Net projects where I need to work with JSON to a class in C#. I came across a web site created by Jonathan Keith using the popular JSON.Net library by James Newton-King.
The web site allows me to copy and paste JSON code to a form and by pressing the generate button I was able to get back a class that I can copy and use in my .Net project. This web site is very useful to me since I am also using the JSON.Net library in my current project to Deserialise JSON data in to an object.
http://json2csharp.com
by David Loo | Feb 20, 2014 | C#
If you need to convert an array to singe delimited string it is easy to do. Below is an example C# code to demonstrate how to join an array of integer values into a single delimited string:
using System;
namespace JoinArrayElements
{
class MainClass
{
public static void Main (string[] args)
{
int[] numbers = new int[] { 1,2,3,4 };
foreach (int num in numbers)
{
Console.WriteLine(num);
}
var strJoined = string.Join(",", Array.ConvertAll(numbers, Convert.ToString));
Console.WriteLine ("Joined Integer Array as a Single String: {0}", strJoined);
}
}
}
I am using the string.Join() to join my array, the first parameter would be the delimiter character for this example I am using the comma “,”. Then the second parameter is the array that contains all the elements, but this parameter only takes an array of string. Okay we can fix this by converting the array of integers to an array of strings, by using the Array.ConvertAll() method and passing the array values to be converted and set the data type to convert to. (more…)
by David Loo | Apr 19, 2011 | C#
Recently I wrote a small program to allow me to connect to a SQL database and export a selected table to a CSV file. So I needed a method that can take in a DataTable and the delimiter character to use. The returned result is a string which can be save straight to a text file.
Below is the method in my program, you may copy, modify and use it in your own program.
I use a StringBuilder class to build my lines of comma separated values and then return it as a String type. Currently the code only format string values with double quotes inserted around the value and the rest is a straight output.
(more…)
by David Loo | Nov 28, 2010 | Windows Phone Development
Got a Windows Phone 7 and want to write some cools apps for it? Microsoft has release some tools to the public so that programmers like yourself can get started! You actually don’t need a Windows Phone because the kit comes with an emulator. Anyways let get start by downloading the developer’s kit from the App Hub and press on “Download the free tools”.
The installation package includes the following:
- Visual Studio 2010 Express
- Windows Phone Emulator
- Silver Light
- XNA Game Studio 4.0
- Microsoft Express Blend for Windows Phone
- .NET Framework 4
(more…)
by David Loo | Nov 13, 2010 | C#
If you want to get your computer and user name from Windows in C# try this by using the WindowsIdentity class from System.Security.Principal namespace:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Console.Out.WriteLine(WindowsIdentity.GetCurrent().Name);
}
}
}