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 Piccolo.Common { public class WordPressBlogPost { public string Title; public string Text; public string[] CategoryNames; public WordPressBlogPost(string title, string text, params string[] categoryNames) { Title = title; Text = text; CategoryNames = categoryNames; } public override bool Equals(object obj) { if (obj == null) return false; if (!obj.GetType().Equals(this.GetType())) return false; WordPressBlogPost compare = (WordPressBlogPost)obj; return compare.Title == this.Title; } public override int GetHashCode() { return this.Title.GetHashCode(); } } }
Notes:
- The last parameter in the constructor (categoryNames) is prefaced by the params keyword. This means you can pass zero or more objects (strings in this case), and these will be combined into an array (see examples below).
- The Equals method overrides the default implementation. This allows you to compare one WordPressBlogPost object with another to see if they are equivalent. I am determining equivalence based on whether their titles are the same, but you could use other criteria if you want.
- When you override Equals, it is also important to override GetHashCode. I won’t explain how this works here, but basically I’m saying the HashCode for this object is the same as the HashCode for the Title because that is what uniquely identifies a BlogPost.
WordPressBlogPost postNoCategories = new WordPressBlogPost("post 1", "this has no categories"); WordPressBlogPost postOneCategory = new WordPressBlogPost("post 2", "this has one category", "category1"); WordPressBlogPost postTwoCategories = new WordPressBlogPost("post 3", "this has two categories", "category1", "category2"); Console.Out.WriteLine(postNoCategories.Equals(postOneCategory)); //false
Hi,
Thanks you have done really creative and nice work in these post I am beginner to RPc but these post really helpful for me thanks for such and nice posting
thanks
imran