Ian (Fluxtah) Warwick's blog RSS 2.0
# Sunday, March 07, 2010
$itemid = JRequest::getVar( 'Itemid' );
$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem( $itemid );
return $item->name;
Sunday, March 07, 2010 7:18:27 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
PHP | Joomla
# Monday, March 01, 2010

First 16 numbers of the fibonacci series

void Main(){ for(long i=0, n=0, a = 1, b = 0; i < 16; i++,  Console.WriteLine(n), b = a, a = n, n = a + b); }
Monday, March 01, 2010 12:26:57 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
C#
# Thursday, December 17, 2009
Its common practice to use a user control in a web part via LoadControl(...). By doing this you get the ASP.NET designer experience when working with .ascx files.

After seeing some implementations of encapsulating a user control in a web part, I created a version that uses a generic base class that has the benefits of giving strongly typed access to the user control, the class is as follows.
public abstract class UserControlWebPart<T> : WebPart where T : UserControl
 {
     /// <summary>
     /// The control in the ASCX file
     /// </summary>
     protected T UserControl
     {
         get;
         set;
     }

     /// <summary>
     /// Path to the ASCX file
     /// </summary>
     protected abstract string UserControlPath
     {
         get;
     }

     protected override void CreateChildControls()
     {
         base.CreateChildControls();
         if (!string.IsNullOrEmpty(this.UserControlPath))
         {
             this.UserControl = (T)this.Page.LoadControl(this.UserControlPath);
             this.UserControl.ID = "uc";
             this.Controls.Add(this.UserControl);
         }
     }

     protected override void OnLoad(EventArgs e)
     {
         base.OnLoad(e);
         EnsureChildControls();
     }

     protected override void Render(HtmlTextWriter writer)
     {
         if (this.UserControl != null)
         {
             this.UserControl.RenderControl(writer);
         }
         else
         {
             writer.Write("UserControlPath is an invalid path, it must point to a valid .ascx file");
         }
     }
To use this class you can just subclass it and implement the abstract property to define the path to the ascx file as follows.

public class MyWebPartUserControl : UserControl
{
}

public class MyWebPart : UserControlWebPart<MyWebPartUserControl>
{
    protected override string UserControlPath
    {
        get { return "~/_controltemplates/my.user.controls/mywebpartusercontrol.ascx"; }
    }
}
The MyWebPartUserControl is the code-behind for the ascx file specified for the abstract UserControlPath property, this file requires a fully qualified assembly path to work correctly within sharepoint as follows.
<%@ Assembly Name="My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234abcd1234abcd" %>
<%@ Control Language="C#" Inherits="My.Assembly.MyWebPartUserControl" %>

Thursday, December 17, 2009 11:34:00 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Sharepoint
# Wednesday, December 16, 2009

I created a custom content type and list definition, after upgrading my sharepoint solution I could see the list and content types under the new menu but when creating a new item, the fields were missing from NewForm.aspx

I discovered that it was due to the ContentType definition not having the FieldRefs tag, my content type was defined like this:-

  <ContentType ID="0x01001376F571B0DB9D4E9384D2927DC110CF02" Name="News Article" Group="Vodafone LCD Content Types" Version="0">
  </ContentType>

When it should have been like this:-

  <ContentType ID="0x01001376F571B0DB9D4E9384D2927DC110CF02" Name="News Article" Group="Vodafone LCD Content Types" Version="0">
    <FieldRefs>
    </FieldRefs>
  </ContentType>

Wednesday, December 16, 2009 8:38:54 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Sharepoint
# Wednesday, November 25, 2009

I was thinking about this, for some reason, and thought it would be good to remind myself of those little useful bits of code that you never really use much then sometimes wonder how to do them.

So my first tidbit, is to test if a number is a whole number, here is my solution, any better solution than this, answers on a postcard please.

void Main()
{
    decimal decimalNumber = 4.9M;
    decimal wholeNumber = 5;
    
    Console.WriteLine(Math.Floor(decimalNumber) == decimalNumber); // Returns false;
    Console.WriteLine(Math.Floor(wholeNumber) == wholeNumber); // Returns true;
}

 

Wednesday, November 25, 2009 10:39:40 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
C#
# Friday, November 13, 2009

I wrote a small console application to test if this was possible and it turns out all you need is an XmlInclude attribute on the base type of the sub-types you are serializing.

In the following example Ninja and Customer inherit from Person, Person is decorated with the XmlInclude attribute for both Ninja and Customer.

class Program
 {
     static void Main(string[] args)
     {
         XmlSerializer serializer = new XmlSerializer(typeof(Stuff));
         Stuff stuff = new Stuff()
         {
             People = new List<Person>()
             {
                 new Person() { Name = "<b>Bob</b>", Type="Person" },
                 new Ninja() { Name = "Hiro", Type="Ninja", Weapon = "Shuriken" },
                 new Customer() { Name = "Fred", Type = "Customer", CustomerId = "1234"}
             }
         };

         serializer.Serialize(Console.Out, stuff);
     }
 }

 public class Stuff
 {
     public List<Person> People { get; set; }
 }

 [XmlInclude(typeof(Ninja)), XmlInclude(typeof(Customer))]
 public class Person
 {
     public string Type { get; set; }
     public string Name { get; set; }
 }

 public class Ninja : Person
 {
     public string Weapon { get; set; }
 }

 public class Customer : Person
 {
     public string CustomerId { get; set; }
 }
Friday, November 13, 2009 7:56:37 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
C#
# Wednesday, November 11, 2009

It comes to that point in time when you need a country table in your database and you do not have a list to import at hand, after scouring the web a bit, everything seemed to be out of date, so I wrote a little script to scrape the details from a wikipedia page for the ISO 3166-1 standard for country codes.

I wrote and ran the following quick & dirty JQuery script on that page.

if($('#output_textarea').length == 0){
    $(document.body).append($('<textarea rows="128" id="output_textarea"></textarea>'));
}

document.output_textarea_val = "";

$('#sortable_table_id_0 tr').each(function(idx){

    if(idx > 0){
        document.output_textarea_val += "INSERT INTO country (name, code_alpha_2, code_alpha_3, code_numeric) VALUES(";
        var cells = $('td', this), cellLen = cells.length;
        cells.each(function(cellIdx){
            if(cellIdx < 4){
            document.output_textarea_val += "\"" + $(this).text().trim() + "\"";
            if(cellIdx < cellLen - 2){
            document.output_textarea_val += ", ";
            }
            }
        });
        document.output_textarea_val += ');\n';
    }
});

$('#output_textarea').val(document.output_textarea_val);

This script creates insert statements for mySQL and inserts the results into a text box at the foot of the page, in order for it to work you need to inject jQuery into the document, you can do this with the jQueryify Bookmarklet.

I have tested this script and it qualifies for the "works on my machine" certification programme.

The script produces the following output.

Wednesday, November 11, 2009 1:07:31 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Javascript | SQL
# Tuesday, September 15, 2009

Whilst working on my network engine I came across a problem that I initially solved with an interface, but it turned out that the class that implemented this interface exposed public methods that I really wanted to be private, an example will explain the problem.

public interface IMessageReceiver
{
    void Receive(string message);
}

public interface IMessageProvider
{
    IMessageReceiver MessageReceiver;
    void Update();
}

public class ConcreteReceiver: IMessageReceiver
{
    private IMessageProvider _MessageProvider;

    public ConcreteReciever(IMessageProvider messageProvider)
    {
        _MessageProvider = messageProvider;
        _MessageProvider.MessageReceiver = this;
    }
    public void Receive(string message)
    {
        Console.WriteLine(message);
    }

    public void Update()
    {
        MessageProvider.Update();
    }
}

When calling ConcreteReceiver.Update(), this will call MessageProvider.Update(), then, the concrete implementation of IMessageProvider should call IMessageReceiver.Receive, effectively like a callback.

Now the issue in my case was that I did not want the Receive method of ConcreteReceiver to be public, but of course any concrete type that implements an interface must implement those methods as public, after pondering a while I realised the design was actually flawed anyway, what I really wanted was a callback, so I changed my design as follows.

public interface IMessageProvider
{
    Action<string> ReceiveAction;
    void Update();
}

public class ConcreteReceiver
{
    private IMessageProvider _MessageProvider;

    public ConcreteReciever(IMessageProvider messageProvider)
    {
        _MessageProvider = messageProvider;
        _MessageProvider.ReceiveAction = Receive;
    }
    private void Receive(string message)
    {
        Console.WriteLine(message);
    }

    public void Update()
    {
        MessageProvider.Update();
    }
}

So now my Receive method on my ConcreteReceiver can be private, since I am now using a generic Action<T> delegate, this prevents my public API exposing methods that are really meant to be only used internally.

Tuesday, September 15, 2009 3:50:00 AM (GMT Daylight Time, UTC+01:00)  #    Comments [1] -
C#
# Wednesday, September 09, 2009

The Available property of the System.Net.Sockets class will tell you how much data is available to read.

With UDP sockets, one thing to remember is that Socket.Available will give the total size of all the datagrams ready to read, so to the only way to know how many datagrams are waiting to be read is to call ReceiveFrom repeatedly until all data is read, for instance:-

while(Socket.Available > 0)
{
   int datagramSize = Socket.ReceiveFrom(buffer, ref endPoint);
} 

The datagramSize variable will give the size of the datagram that was read, this can be troublesome to manage since you do not know what you are going to get, until you get it, so you would need to initialize a large enough buffer to hold the datagram.

In the networking framework I am currently writing, the application has a configurable MaxPacketSize option so I can initialize my buffer to this size, but this wont help in the event that a bum packet is sent that breaches this constraint so some error handling would also need to be in place to compensate for this issue.

Wednesday, September 09, 2009 5:26:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
C#
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Ian Warwick
Sign In
Statistics
Total Posts: 31
This Year: 2
This Month: 0
This Week: 0
Comments: 4
Themes
Pick a theme:
All Content © 2010, Ian Warwick
DasBlog theme 'Business' created by Christoph De Baene (delarou)