SyntaxHighlighter

Sunday, April 25, 2010

IIS 7 and SSL Error

If ever faced with the error ssl_error_rx_record_too_long in IIS 7, check to see if you have IIS 6 Compatibility switched on (Control Panel -> Programs and Features -> Turn Windows features on or off -> Internet Information Services -> Web Management Tools -> IIS 6 Management Compatibility).

Monday, March 29, 2010

WPF: Dynamically create a Table

Being new to WPF, there are a few funnies you'd need to know about before being able to create a simple table with text.

First, one can't simply add a Table within a Grid container. Tables need to be housed within a FlowDocument. One also need a container to display these FlowDocuments. I found the FlowDocumentScrollViewer to be the simplest. Some code to display a blank table:
<FlowDocumentScrollViewer
VerticalScrollBarVisibility="Disabled"
HorizontalScrollBarVisibility="Disabled">
<FlowDocument>
<Table Name="myTable"></Table>
</FlowDocument>
</FlowDocumentScrollViewer>

To populate the table programmatically with some basic text one need to follow the next procedure (the code is pretty much self-explanatory):
int cols = 5;
int rows = 10;

for (int c = 0; c < cols; c++)
myTable.Columns.Add(New TableColumn());

for (int r = 0; r < rows; r++)
{
TableRow tr = new TableRow();

for (int c = 0; c < cols; c++)
tr.Cells.Add(New TableCell(New Paragraph(New Run("Some Text"))));

TableRowGroup trg = new TableRowGroup();
trg.Rows.Add(tr);
myTable.RowGroups.Add(trg);
}

I must say that this approach (a Run within a Paragraph within a TableCell within a Row within a RowGroup) isn't terribly intuitive and the process seems a bit tedious when only trying to create a simple table.

Thursday, December 10, 2009

C#: Calling/Overriding Grandparent Base Class Methods

I use a base class for 99% of my ASP.Net pages, which executes a few operations in the OnInit() method. Here's a simplified version:
public abstract class BasePage : System.Web.UI.Page, IBasePage
{
protected override void OnInit(EventArgs e)
{
// Do stuff
Method1();
Method2();
Method3();

// Call System.Web.UI.Page.OnInit
base.OnInit(e);
}
}
This works great for all my child classes/pages, but there are a few scenario's where I'd only like to call a specific set of methods, as illustrated here:
public class MyPage: BasePage
{
protected override void OnInit(EventArgs e)
{
// Ommit Method2()
Method1();
Method3();

// This calls BasePage.OnInit, which makes
// redudant calls to Method1() and Method3(),
// and the undesired call to Method2().

base.OnInit(e);
}
}
It is obvious that the last base.OnInit(e) call will have the undesired effect. Being 6:23 PM, after coding all day, my fried brain's initial thought was to bypass the base class' OnInit(e) method and to call the grandparent's OnInit(e) method directly.

An obvious faux pas. Whenever venturing into the "grandparent" or "grandchild" realm (more than one degree of seperation), you're probably going down the wrong path.

The proper and easy approach is merely to move the method calls out to a seperate method, which one overrides in the child class like so:
public abstract class BasePage : System.Web.UI.Page, IBasePage
{
protected override void OnInit(EventArgs e)
{
// Do stuff
Initialize();

// Call System.Web.UI.Page.OnInit
base.OnInit(e);
}

protected virtual void Initialize()
{
Method1();
Method2();
Method3();
}
}

public class MyPage: BasePage
{
protected override void Initialize()
{
// Do stuff
Method1();
Method3();
}
}

Monday, December 7, 2009

GNU Plot and SVG: Change the terminal size and font

Again, not really a dev related issue, but I did not find much info online regarding this.

When using gnuplot to create SVG files, one use the set term svg command that sets the following defaults:
gnuplot> set term svg
Terminal type set to 'svg'
Options are 'size 640 480 fixed fname 'Arial' fsize 12 butt '

One can speficy custom parameters to get the desired output:
gnuplot> set term svg size 640,350 fname 'Times New Roman' fsize 10
Terminal type set to 'svg'
Options are 'size 640 350 fixed fname 'Times New Roman' fsize 10 butt '

Monday, November 23, 2009

Type 'DataContract' is not defined

This error can be resolved by adding a reference to the .net System.Runtime.Serialization assemly and also importing this namespace in the file where applicable.

Also, applies to Type 'DataMember' is not defined.

Tuesday, November 3, 2009

VB.NET: Check if an event handler has been assigned to a method

This is how one checks if an event handler (or any delegate handler for that matter) has been assigned to a method.

' Create the delegate
Public Delegate Sub UpdateEvent()
' Create a handler
Public Update As UpdateEvent

' ...
' Rest of the class
' ...

' Check if the event handler has been assigned to
If Not Update Is Nothing Then
Update()
End If

'SyncLock' operand cannot be of type '' because '' is not a reference type

Just had this little error in VB.NET:

Error Message: 'SyncLock' operand cannot be of type 'Boolean' because 'Boolean' is not a reference type.
Error ID:
BC30582

I tried to change the variable to type Object, but this seems to be converted to Boolean during runtime, which raises the same error.

A clumsy way around this is to simply use a Boolean array of length 1. There must be a better way though, and I'll update this post when I find a better approach.

Yes, I did say vb. Yes, I am ashamed. Client's request.


Update:
Ok here's the deal on using SyncLock or Monitor.Enter (mutex) with value types such as Boolean, Integer, or Double. One is not supposed to lock value types, as they do not have the necessary overhead fields (MethodTablePointer and SyncBlockIndex) to allow a lock to be acquired. One gets around this issue, by creating a reference type, which acts as a flag when the value type is being written to. More on this at the end of Jeffery Richter's article on Safe Thread Synchronization.