Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Friday, April 30, 2010

.NET timers

To use a timer:

using System.Timers;
...
// NB put this next declaration at class level,
// not in a method where 'tick' will be
// garbage collected before it can fire
System.Timers.Timer tick;

...
tick = new System.Timers.Timer(10000);
// include the next line if you want event handler to interact with controls
// (eg setting text)
tick.SynchronizingObject = this;
tick.Elapsed += new ElapsedEventHandler(this.tickElapsed);
tick.Enabled = true;
...
void tickElapsed(object source, ElapsedEventArgs e)
{
...
}

If your timer is failing to eg set text on a control, check that you included the SynchronizingObject set.

Thursday, April 29, 2010

DataRow Field

DataRow.Field is both an instance method and a static method. Either way, the reason it keeps not compiling is that it's some kind of template which you have to supply with a type name, eg

therow.Field<string>("fieldname")

Wednesday, April 28, 2010

Insert into DataRowCollection

DataRowCollection.InsertAt(...) appears to do an insert-before, ie InsertAt(row, 0) results in /row/ being added at index 0.

To add after the last row, use index >= .Count .

Tuesday, April 27, 2010

.NET collections

.NET collections have 0-based indexing.

Saturday, April 24, 2010

C# constructors

One version of an overloaded constructor can call another thusly:

public class Doctor {

public Doctor(int incarnation) {
...
}

public Doctor(): this(1) {
...
}

}

ie Doctor() makes a call Doctor(1) - this happens before the body of Doctor() executes.

The colon syntax is the same as used for calling a base class constructor in an inheriting class.

C# properties

You can't have just a get or set accessor with auto properties (because without an underlying variable there's no way to perform the opposite operation even inside the class). But this is acceptable:

public int Regenerations { get; protected set; }

Btw, the expanded syntax is eg

public int Incarnation {
get { return _incarnation; }
set {
if (value == 4) DonScarf();
_incarnation = value;
}
}

Wednesday, April 21, 2010

.NET treeview

TreeView.Nodes works just like TreeNode.Nodes, ie it only returns its own immediate children (the top-level nodes).

C# foreach

The data type in foreach isn't just a shortcut, you have to use it:

foreach (int i in intarray)

It won't compile if you predeclare int i and just use (i in intarray) in the loop expression.

.NET treenode key

The reason a Treenode doesn't have a Key property as such is that the key (such as you can set with some of the Add methods) is also the Treenode's Name property. Apparently.

Tuesday, April 20, 2010

rank()

rank() over (partition by X... order by Y....) produces, for each row, its rank when all the rows are subdivided according to (basically, grouped by) fields X... and sorted within each subdivision by fields Y...

So if with this query

select story_name, episode, rating from episode

Space Museum,1,4
Space Museum,2,3
Space Museum,3,2
Space Museum,4,2
Castrovalva,1,3
Castrovalva,2,2
Castrovalva,3,4
Castrovalva,4,4

you wanted the episodes of each story to be ranked in order of rating, you'd specify

select story_name, episode, rating, rank() over (partition by story_name, episode order by rating desc) from episode

and get

Space Museum,1,4,1
Space Museum,2,3,2
Space Museum,3,2,3
Space Museum,4,2,3
Castrovalva,3,4,1
Castrovalva,4,4,1
Castrovalva,2,2,3
Castrovalva,1,3,4

NB that you get a sportsman's rank, not a mathematician's (two equal firsts get 1, not 2).

NB2 that you don't seem to have to specify all the candidate fields for partition, as you would with group by.

Monday, April 19, 2010

C# shell command

stdout capture example:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c reg query HKEY_CURRENT_USER\Software\Crapware\UniCrap /s";
p.Start();
string output = p.StandardOutput.ReadToEnd();

C# window interaction

The easiest way to do the window activation/control stuff that AutoIt and VB can is to add a reference to the Microsoft.VisualBasic assembly to your project.

You can then have code like

Microsoft.VisualBasic.Interaction.AppActivate("Calculator");

- SendKeys is already available in System.Windows.Forms, but there's no simple way in there to activate a window so as to send keys to it.

- SendKeys.Send won't work from a form constructor, there'll be a run-time error. Put it in the form Load event handler or anywhere 'later' - looks like the form has to be running and handling messages before it can send keys.

Sunday, April 18, 2010

VNC

NB that when running a listening viewer on your machine, you should have allowed incoming access to port 5500. There needs to be incoming access in at least one direction...

Wednesday, April 14, 2010

C# index out of bounds

Don't be lulled into a false sense of security by C#'s habit of throwing exception dialogues for file not found, etc. You can't always expect one - an array index out of bounds will still give you an old-fashioned crash, for example.

C# command line arguments

Windows app - the array returned by Environment.GetCommandLineArgs has the executable filename as element 0, ie it is CommandLine split up into an array.

BUT

Console app - args[0] is the first argument, not the executable name. So args[] is like Perl's @ARGV, and different from Environment.GetCommandLineArgs and from C's argv[].

Thursday, April 8, 2010

C# string indexes

First character of a string has index 0.

Monday, April 5, 2010

C# implicit conversion

Even in this scenario where b and c are both byte variables

c = b + 64;

C# converts the result of the calculation to int, and then complains that you're assigning int to byte. Casting 64 to (byte) doesn't help either, you have to say

c = (byte) (b + 64);

ffs.

Thursday, April 1, 2010

C# and static-ness

Deductively - in a console app, Program.Main is defined as static (presumably for a good reason) and if Main is static (= a class method, remember) then presumably all the other methods should be too.

And you can't refer to a non-static (instance) variable from a static method, because 'Object reference required' and /this/ isn't allowed in static methods.

So all the Program-level variables should be static.

Followers

Blog Archive