.NET
I found a really useful article about the ASP.NET page compilation process. The thing I was looking for was actually where to find the generated page source files. They’re stored in a directory like: C:WINDOWSMicrosoft.NETFrameworkv1.1.4322Temporary ASP.NET Files
For once I’m going to resist the urge to reinvent the wheel.. :) What I’ve found so far: Actipro CodeHighlighter
If statements: If condition Then ' do something Else ' do something else End If For loop: For i As Int16 = start To end ' do something Next For each: For Each item As Type In Collection ' do something Next
To get a resource from the current assembly: Stream stream = this.GetType().Assembly().GetManifestResourceStream(“namespace.file”); To get all the names of all the resources in your current assembly: string[] resources = this.GetType().Assembly.GetManifestResourceNames(); foreach(string resourceName in resources) { Debug.WriteLine(resourceName); }
How interesting! Turns out that VS.NET 2003 actually supports debugging of clientside scripts! I’m going to miss the javascript alert function. :)
The error “System.InvalidCastException: Object must implement IConvertible” in the context of saving things to a database (in my case through a command object with pre-set typed parameters) means that you’re asking the command object to set a parameter with an incompatible type. In my case, I was trying to set a SqlDbType.Text field in my [...]
Found a rather nifty trick for formatting double values into currency strings. It’s really simple! You just specify “C” as an argument to the ToString method and it automatically generates a currency string that’s in the right format for whatever your local settings are. value.ToString(“C”); There’s lots of other stuff you can do with the [...]
Came across an interesting problem yesterday trying to handle the SelectedIndexChanged event for a DropDownList that was inside a Repeater. I found a wonderful article on Databinding that gave me most the information I needed, but their method of handling events just didn’t work for me. My first problem was that I originally did the [...]
I wasn’t really surprised to find that my virus checker slows my VS.NET compilations somewhat, but I was surprised by exactly how much. With Macafee’s real time scanning on, my P4 with 720mb of RAM compiles about the same speed as the P600 with 128MB of RAM I was using before. When I turn off [...]
Sending mail in ASP.NET is simple! You just create a message and then call the SmtpMail’s static send method: MailMessage message = new MailMessage();message.To = "me@email.com";message.From = "me@email.com";message.Subject = "An email from helen";message.Body = "Hello Helen. From Helen.";SmtpMail.Send(message); If you need to talk to a non-local server, that’s not really much more difficult. You just [...]
Roy Osherove’s put together a list of must-have tools for .NET developers. My favourite is definitely Resharper which is a VS.NET plugin that makes Visual Studio a lot nicer to use. If you’ve ever thought “I wish VS.NET did x”, chances are Resharper will do it. The refactoring tools alone made it worth the $100US [...]
I found a couple of interesting articles about ASP.NET and web standards when I was looking for information about seperating presentation and business logic in ASP.NET using XML. The guy from ASP.NET resources makes the argument that it’s not economically viable to put the resources into developing ASP.NET applications that return properly compliant XHTML. I [...]
public abstract int GetX { get;}
Here’s how you create a property (though it could be variable, method or I think even class) that can be see by things that subclass the object it belongs to and things that are in the same package: protected internal object myProperty { get { return m_property; } set { m_property = value; } } [...]
Saving info to the registry for the currently logged in user: RegistryKey key = Registry.CurrentUser.CreateSubKey(“MyRegistryKey”); key.SetValue(“MyKeyValue”, “Some value”); Loading the same info from the registry: RegistryKey key = Registry.CurrentUser.CreateSubKey(“MyRegistryKey”); if(key != null) { string keyValue = (string) key .GetValue(“MyKeyValue”); } Reference: RegistryKey Class MSDN documentation
My quick-start file writing example for those who go months between needing to write a file. StreamWriter writer = new StreamWriter(filename, false); writer.Write(“some text”); writer.Close(); I remember it being harder than this.. Reference: Streamwriter documentation
This is the C# equivalent of the Main(String[] args) method in Java: [STAThread]static void Main() { Application.Run(new MyStartupClass());} This code usually goes in the class that defines your first windows form.
Here’s a nifty trick if you’ve got a chunk of XML you want to format nicely: XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(xmlStringVariable); MemoryStream memoryStream = new MemoryStream(10240); xmldoc.Save(memoryStream); string formattedXml = Encoding.ASCII.GetString(memoryStream.GetBuffer()); memoryStream.Close();
Just found a good reason to use properties in C# rather than public variables: they’re much easier to debug. When I was getting an unexpected value in the variable, I put a break point in the property and was able to really easily find out where it was being set.
First you need to declare an event in the class that’s going to raise the event. This is just like declaring a normal variable: public event EventHandler MyEvent; Then you need a method that’s going to be called to raise the event. This goes into the same class that you just declared the event in. [...]
Here’s a useful bit of C# code for creating a method that takes a Type as a parameter from the microsoft.public.dotnet.languages.csharp group. Calling code: MyComponent component;object myobject;MyMethod(myobject, typeof(MyComponent)); Method: public static void MyMethod(object myobject, Type type) { if(myobject.GetType() == type) { // do something }} Useful references: MSDN typeof documentation MSDN using typeof reference
Learnt something new today from Merak on the UK Microsoft web developer’s list. One of my early problems when writing regular expressions in C# was that I didn’t realise that is actually an escape character when used in a string in C#. So when I wanted to write an expression like this: /d/ (which means [...]
I’ve been investigating doing unit testing in ASP.NET using NUnit. It’s a very interesting idea, particularly the idea about Test Driven Development, but I’ll post a bit more about that later. NUnit is a really nice piece of software, but I run into a problem when I tried running my first unit test. The error [...]
An easy way to get this exception is by refering a property to itself: public string myproperty { set { myproperty = value; } } The exception looks like this: [StackOverflowException: Exception of type System.StackOverflowException was thrown.] Granted it’s a pretty dumb thing to do.. :)






