Investigating the ordering of static and instance constructor calls.
Static constructors get called only once in the entire program’s lifetime- the first time the class or any of its members gets referenced.
Instance constructors are called per object instantiation.
public class Base
{
static Base()
{
Console.WriteLine("Base static class constructor");
}
public Base()
{
Console.WriteLine("Base instance constructor");
}
}
public class Derived : Base
{
static Derived()
{
Console.WriteLine("Derived static class constructor");
}
public Derived()
{
Console.WriteLine("Derived instance constructor");
}
}
class Program
{
static void Main(string[] args)
{
Base b = new Derived();
Console.WriteLine("Done");
}
}
The program results are shown here:
Derived static class constructor
Base static class constructor
Base instance constructor
Derived instance constructor
Done
Something interesting came out of this test.
- instance constructors are called base class first, [...]
Read more...
So I just ran into a problem in C# where I have to parse out certain names from a string. The name can be parsed out using a certain pattern. The problem is how to do it in C#.
<GRMetaData name=’CREDIT’/><GRMetaData name=’HISTCREDITDETAIL’/><GRMetaData name=’FX’/>
How to get at the strings CREDIT, HISTCREDITDETAIL, and FX ?
After a little experimentation, here is the code to do it in C#:
Regex nodeNameMatch = new Regex(@"GRMetaData name='(\w+)'");
MatchCollection test = nodeNameMatch.Matches(res);
for (int i = 0; i<test.Count; i++) {
CaptureCollection cc2 = test[i].Groups[1].Captures;
for (int j = 0; j < cc2.Count; j++) {
Console.WriteLine(cc2[j].ToString());
}
}
Read more...
I’m currently using C1Component FlexGrid. To enable a cell to popup a list of allowed values, create a pipe-delimited string (ex: “Red|Green|Orange”) and set it to the Cols(index).ComboList property. Setting the ComboList property causes C1FlexGrid to display a dropdown box next to the cell.
To allow a user to select from a dropdown list of values AND allow the user to enter their own values, prepend a pipe to the same pipe-delimited string as used for the dropdown list previously (ex: “|Red|Green|Orange”).
cfg.Cols[1].ComboList = “Red|Green|Orange”; // drop-down list
cfg.Cols[2].ComboList = “|Red|Green|Orange”; // drop-down combo
Read more...
To add a new control to the Toolbox in Visual Studio, click Tools -> Choose Toolbox Items.
Read more...
C#’s event handling takes event handling notification on a twist from Java’s tried-and-true broadcast-subscription mechanism. Using events, disguised in the form of delegates, event subscribers can similarly subscribe to event notification. The event source notifies all event subscribers to calling back on the delegate method they initially sent for subscribing to the event notification list.
using System;
using System.Threading;
namespace TestEvents
{
public class WakeupEventArgs : EventArgs
{
// Information to be distributed from the event source
// to each event subscriber (sink)
private DateTime WakeupDateTime_;
public WakeupEventArgs(DateTime dt)
{
WakeupDateTime_ = dt;
}
public string GetWakeupDateTimeStr {
get { return WakeupDateTime_.ToShortTimeString(); }
}
}
public class Context
{
// context object- usually the server application-
// or any object that controls the notification of events.
// publicly declare the signature of the event callback method
// any object that wants to be notified of an event should
// have a method prepared [...]
Read more...
Click on the window or control
Right click for properties
Click the Events (looks like lightning icon) button
Select the list of events to generate an event handler
Read more...
The leftmost column in a default C1FlexGrid has always bothered me. How to turn it off? This consumed me about half a day a month ago. To turn off the left most column, click on the Property page of the C1FlexGrid. Look for the Cols property. Set the “Fixed” setting to 0. This does the trick.
Read more...
string tmp = "a;b;c";
ArrayList a = new ArrayList( tmp.split(";" ) );
Read more...
So today at startup, I saw this error at startup of Visual Studio 2.0: “The application data folder for visual studio could not be created”. The message had an OK button. I clicked the OK button (this is certainly NOT OK)- and then the dialog box disappeared and went away. Visual Studio did not startup. Huh? WTF? What application data folder are you talking about?
As usual, Microsoft’s error message is of no help. I googled the message- and found that the key to this problem is by changing the following registry key entry (like some everyday user’s going to be able to find this)
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
After I set this folder to my local drive/path, Visual Studio started. Thanks Mr. Google.
Read more...
Microsoft really sucks. Even their DotNet CLR runtime error reporting sucks. Last week, a user in London called me and reported a startup problem with the service I support. I could see the error in the event log as:
EventType clr20r3, P1 ripgrcubeaggregatoranalytic.exe, P2 3.3.0.2, P3 46267b76, P4 log4net, P5 1.2.9.0, P6 42a6f2e9, P7 259, P8 0, P9 system.typeinitialization, P10 NIL
This error message is completely completely utterly useless. Somehow this runtime error even avoided getting trapped by my service’s unhandled exception handler.
After endless iterations of testing with a previous version followed by more testing, I finally tracked down the cause of the problem to the London user’s messing up of the service’s App.Config file. He put in a one byte code- which invalided the entire App.Config xml- and threw the DotNet application into a fit.
So if you see this runtime problem, first check the App.Config.
Read more...