June 25th, 2008 Luke
For a recent project, I had to deal with a fair number of controls on a page that all needed to be disabled at once. This is the function that I wrote to do so:
void DisableControls(List types, ControlCollection controls) {
foreach(Control control in controls) {
if(types.Contains(control.GetType())) {
((WebControl)control).Enabled = false;
}
}
}
Posted in ASP.Net, Code, csharp, snippet | No Comments »
June 10th, 2008 Luke
For a recent project, we needed to store as many possible values into 3 characters as we could, based on a numeric ID. So, we created a function that would convert that ID to base 62, which we could then use to increase the number of available combinations that we could store. Here’s the function:
public const string base62digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public string ToBase62(int input)
{
int modulo;
string sTemp = "";
do
{
modulo = input % 62;
sTemp = base62digits.Substring(modulo + 1, 1) + sTemp;
input = input / 62;
} while (input != 0);
return sTemp;
}
It’s been modified from the VB code to do the same thing, which you can find here.
Posted in Code, conversions, csharp | No Comments »
May 23rd, 2008 Luke
Sometimes you’ll encounter a situation where you need an array of strings, just so that you can pass it into a function. Doing it in two lines is ugly if it’s just a temporary variable - and it tends to pollute your Intellisense. Here’s how to do it in one:
new string[3]{"foo","bar","baz"};
Let’s say you were calling a function that took in an array of strings. You could use it like this:
myFunction(new string[3]{"foo","bar","baz"});
And you’d be good to go!
Posted in csharp, oneliner | 2 Comments »
February 20th, 2008 Luke
Have you ever been working on a project and needed a relative path for something?
Under C# itself you could use something like ~/path/to/foo, but that won’t work on a website - the tilde doesn’t always get expanded.
However, there is a nicer way. If you use Page.ResolveClientUrl(), you can easily resolve files from their ASP.net path of ~/path/to/foo to a relative path like folder/path/to/foo, or even path/to/foo, depending on where the page that is making the call to Page.ResolveClientUrl() is located. Here’s an example of how to use the code:
string myPath = Page.ResolveClientUrl("~/admin/pages.aspx");
If that code had been called from the file located at ~/admin/page2.aspx, myPath would be “pages.aspx”. If it had been called from ~/foo/main/default.aspx, myPath would be “../../admin/pages.aspx”.
Posted in csharp, snippet | No Comments »
January 31st, 2008 Luke
For a recent project, I needed to output integers to show the change in two values.
When an integer is negative, the negative sign(’-') is automatically added to it - but when you output an integer that is positive, there is no positive sign(’+') added to the beginning. I wanted to display the positive sign as well, so that users could easily tell when the values were positive vs. negative.
If you pass this format string into String.Format(), you will receive numbers with the proper signs applied:
"{0:+0;-0;0}"
Here are some examples:
String.Format("{0:+0;-0;0}",22); // returns "+22"
String.Format("{0:+0;-0;0}",-22); // returns "-22"
String.Format("{0:+0;-0;0}",0); // returns "0"
If you’re looking for a little more information on things you can do with String.Format, check out Kathy Kam’s .Net Format String 101 post.
Posted in csharp, format, snippet | No Comments »