Archive

Archive for the ‘.net’ Category

Asp.net jQuery like Closest()

May 2nd, 2012 No comments

Here is a very simple asp.net .Closest() function like the jquery one.

	public static class ControlExtensions
	{
		public static Control Closest(this Control control, Type type ){
			Control closest = null;
			Control parent = control.Parent;
			while(closest == null && parent != null){
				if(parent.GetType().ToString() == type.ToString()){
					return parent;
				}
				parent = parent.Parent;
			}
			return null;
		}
	}
Categories: Asp.Net, c# Tags:

Queue that only excepts N items

March 20th, 2012 No comments

I am trying to keep a list of the last 10 pages a user has been too. I tried to use Stack but it doesn’t support a fixed length so here is a dirty little class to only keep the last N items. Yes I know its a bit dirty!

public class NStack<T>
	{
		public int MaxEntries { get; set; }
		public List<T> Items = new List<T>();
		public void Add(T value)
		{
			if (Items.Count == MaxEntries)
			{
				//remove last item
				Items.RemoveAt(MaxEntries - 1);
			}
			Items.Insert(0, value);
		}
	}
Categories: c# Tags:

c# Coalesce function

November 16th, 2011 No comments

When you’ve been working heavily with c# for a few days, coalesce jumps into your head for a lot of c# issues. Heres a little helper to provide coalesce in .net

/// <summary>
/// Returns the first non null value, same as SQL's COALESCE()
/// </summary>
/// <param name="p">Args array</param>
/// <returns>First non null value</returns>
public static String Coalesce(params object[] p)
{
	foreach (Object o in p)
	{
		if (o != DBNull.Value && o != null)
		{
			return o.ToString();
		}
	}
	return "";
}
Categories: c# Tags: ,

UAD 9 – Numbers To Words

March 13th, 2011 No comments

Utility to take a number and convert it to the english text. Not mine this one but lost the original url! If it’s yours let me know :D

public class NumberToWords
	{
		// Single-digit and small number names
		private string[] _smallNumbers = new string[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
 
 <a href="http://www.fruitbatscode.com/net/c/uad-9-numbers-to-words#more-454" class="more-link">Read more...</a>
Categories: Asp.Net, c# Tags:

UAD8: Query String Builder

March 13th, 2011 No comments

Ever had controls that need to add to the Query String but not duplicate or erase what’s already there? This is a little utility I dug out and just changed to use a generic dictionary recently. Just use it as you would a normal dictionary. When you instantiate a copy it picks up the current query string. There are two ToString() methods, one just return the query string and the other appends another string to the end and takes care of the ? & issues.

Read more…

Categories: Asp.Net, c# Tags: ,

UAD7 – File size, nearest unit mb,kb,gb.

December 22nd, 2010 No comments

Okay, so Util-a-day was a bit optimistic! Here’s the next one, simple function to convert a file size in a user friendly description. It converts the size to the nearest kb, mb or gb.

public static String ConvertBytes(double bytes){
	String FORMAT = "{0:N2}";
	if (bytes > 1073741824)
		return String.Format(FORMAT, (bytes / 1024 / 1024 / 1024)) + " GB";
	else if (bytes > 1048576)
		return String.Format(FORMAT, (bytes / 1024 / 1024)) + " MB";
	else if (bytes >= 1024)
		return String.Format(FORMAT, (bytes / 1024)) + " KB";
	else
		return bytes + " Bytes";
}
Categories: c# Tags: