Code Comments

Code Comments

Tips and short tutorials on various programming technologies

Code Comments RSS Feed
 
 
 
 

Archive for C#

How to Override toString() in Python

In popular object-oriented languages such as Java and C#, they have a built-in method to each class that allows you to obtain a String representation of that class. They call these methods toString() or ToString(), respectively. You can use the default implementation, which basically gives you the name of the class (not usually very helpful). [...] Read more »

How to Perform HTTP GET in C#

Suppose you have a Web resource that you want to download programmatically (as opposed to doing it manually from a Web browser) from C#. The first thing you need to do is send an HTTP request to the server with the URL (e.g. http://www.google.com). The server will then process the request and send an HTTP [...] Read more »

Find the Mean of an Array of Numbers in C#

Let’s say you have an array of numbers and want to be able to find the mathematical mean of those numbers in C#. To my knowledge, the .NET Framework does not have a function to do this built in. (Please let me know if you know otherwise.) So I built a method that does this. [...] Read more »

Find the Sum of an Array of Numbers in C#

Let’s say you have an array of numbers (either doubles or integers) and want to be able to find the sum of these numbers. To my knowledge, there is not a built-in way to do this in the .NET Framework, so I created a little utility method that does this for doubles and for integers. [...] Read more »

Find the Mathematical Product of Double Array in C#

If you have an array of doubles (or floats) in C#, there is no built-in method (that I know of) to find the product of those numbers. The product is where you multiply all of the numbers together. I needed this, so I created a utility method that does this simple operation. See below.

public static [...] Read more »

How to Invoke a Method Dynamically in Python (Reflection)

Sometimes you want to be able to invoke a method dynamically rather than calling it explicitly. Or in other words, let’s say you have a bunch of methods, and you have a string object telling you which method to execute, but rather than doing a bunch of if statements you want to be able to [...] Read more »

Force Collections to Accept Only One Class in C#

Collections, such as List and ArrayList, in C# used to be able to contain any class. So you could implement the following code without a problem.

ArrayList oldWay = new ArrayList();
oldWay.Add("abc");
oldWay.Add(123);
oldWay.Add(new CustomWhatever());

But sometimes this flexibility caused problems because you might want to limit what could be contained in that list (for example, limit it to string [...] Read more »

Example of Implementing IEnumerable and IEnumerator in C#

In the .NET Framework, there are two interfaces designed to allow you to iterate easily over collections of objects as you would typically do in a for loop. Many classes in the .NET Framework have implemented these interfaces and do their work behind the scenes so you don’t have to worry about how it is [...] Read more »

Publishing to a WordPress Blog via XML-RPC (Conclusion)

Please refer to this post for some background.
This series of posts has demonstrated how you can send XML-RPC requests to a WordPress server and get back responses. This allows you to do things like automatically post blog entries, modify them, delete them, etc. This is the type of thing that blogging editors do so you [...] Read more »

Publishing to a WordPress Blog via XML-RPC (Part 8)

Please refer to this post for some background.
Now that all the pieces are in place, a class called WordPressClient brings it all together. It has methods that correspond with some of the tasks you might try to accomplish (such as creating a new post, deleting a post, adding a category, etc.).

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Piccolo.Common
{
public [...] Read more »

Publishing to a WordPress Blog via XML-RPC (Part 7)

Please refer to this post for some background.
The next class is used for actually sending and receiving XML-RPC messages over HTTP. I’ve also added some statements that write the requests and responses to Console.Out so you can see what they look like as they are going across the wire.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
 
namespace [...] Read more »

Publishing to a WordPress Blog via XML-RPC (Part 6)

Please refer to this post for some background.
This section builds on the previous post about regular expressions. When the XML-RPC server sends a response, you need to be able to parse through the XML. The following class can be used for that purpose.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
 
namespace Piccolo.Common
{
public class WordPressXmlParser
{
public static string[] ParseArray(string result)
{
result = [...] Read more »

Publishing to a WordPress Blog via XML-RPC (Part 5)

Please refer to this post for some background.
After you send a request to the XML-RPC server, it sends a response back to you in an XML structure. You have to parse out the values that are important (for example, whether the request was successful, a new post ID that was generated, etc.)
Again, the .NET Framework [...] Read more »

Publishing to a WordPress Blog via XML-RPC (Part 4)

Please refer to this post for some background.
When you’re sending requests to the server, you have to build text in XML. There are a few ways to do this in C#. One way is to use classes that were designed for this in the .NET Framework. However, I decided not to use these because they [...] Read more »

Publishing to a WordPress Blog via XML-RPC (Part 3)

Please refer to this post for some background.
OK, this post is going to talk about some of the plumbing. When you are sending requests to the server and when the server is replying with responses, you need to deal with key-value pairs. For example, userName = “steve” or password = “abc123″. C# has some classes [...] Read more »

Publishing to a WordPress Blog via XML-RPC (Part 2)

Please refer to this post for some background.
Using XML-RPC, you can create new blog posts, delete posts, edit posts, add categories, delete categories, etc.
XML-RPC operates over HTTP (the protocol we use to access a typical Web page). You send information in a request to the server, and the server replies with a response, telling you [...] Read more »

Publishing to a WordPress Blog via XmlRpc (Part 1)

Please refer to this post for some background on what I’m trying to do here.
Because it’s simple, I’m going to start with a class called WordPressBlogPost. This class holds information about a post–in this case, title, text, and categories. It’s not comprehensive but covers the core information you would need to know about a post.

namespace [...] Read more »

How to Publish to a WordPress Blog Using XmlRpc

This is the first in a series of posts about how to publish to a WordPress blog using an interface called XmlRpc that is supported by WordPress as well as other blogging platforms.
My motivation for writing these posts is that I have a hobby project that is written in the C# language, and I wanted [...] Read more »