Feeds:
Posts
Comments

Archive for June, 2008

In standard C++, we know that a virtual method call in a base class constructor will not invoke the derived class override. This is an area where C++/CLI behaves differently from standard C++. The vtables are initialized even before the base class constructor is called. There was a post in the MSDN forums earlier today where someone asked a question related to this and I thought I’d blog about it. Take a look at the following code snippet :

  ref class ManA
  {
  protected:
    NatA* _natA;

    virtual void Init()
    {
      _natA = new NatA();
    }

  public:
    ManA()
    {
      Init();
    }
  };

  ref class ManB : ManA
  {
    NatB* GetNatB()
    {
      return static_cast<NatB*>(_natA);
    }

  protected:
    virtual void Init() override
    {
      _natA = new NatB();
    }

  public:
    ManB()
    {
    }
  };
}

Now, if you do a gcnew ManB() you’ll see that ManB::Init is called first (this is done during the base class ctor) and then ManB‘s constructor is called. Of course this means that you should not access any member in ManB that needs to be created or initialized in the constructor. In fact this is probably not a good practice at all – but the poster asking this question had a special requirement and this was one straightforward way to address his problem. Note that C# users won’t find this particularly odd because this is just the way C# behaves too. This behavior is dictated by the fact that the CLR works with vtables directly, and both C# and C++/CLI just followed the curriculum.

Read Full Post »

Okay I’ve been called many things in my life, but this was a first for sure. My wife ordered pizza and I went to pick it up on my way home from work. She gave my name as Nish, and she spelled out the first letter as “N for Neptune”. I’ve heard her use “S for Saturn” too. Though they are both planets from her perspective I usually tell her that for a lot of folks, one of those is a car and the other’s an odd word they’ve never heard before. Anyway the guy on the phone wrote it down as Neptune. So when I went to pick it up I was addressed as Neptune, and though I was surprised I just thought I had misheard him. Then I saw my name on the receipt and there it was – “Neptune”. I didn’t bother correcting him of course, instead I put on my best “Lord of the sea” look and walked out with a godly swagger.

Read Full Post »

Recently at work I had to use the TypeDescriptionProvider attribute while prototyping some new features for our product, and I thought it would be a good idea to demonstrate how to use this attribute and its related classes by writing an article.

Read Full Post »

Joe Stagner blogged today about some basic coding guidelines one of which was to avoid using any sort of prefix for member variables (including the underscore). I personally prefer to use the underscore prefix on private member fields and mainly so they would be easily identifiable from local variables. But Joe’s guidelines suggested a workaround recommending that we use this to indicate a member variable.

private int _age;

void Foo()
{
    int age;
    //...
    if(age > _age) // <-- My way
    {
        //...
    }
}
private int age;

void Foo()
{
    int age;
    //...
    if(age > this.age) // <-- Joe's recommendation
    {
        //...
    }
}

While I did think that was a fair approach, I am still not convinced it’s fully alright since the onus is on the coder to remember to do this – there’s nothing stopping him from forgetting to prefix this for member fields. So for the time being I am going to continue using underscore for private members.

Read Full Post »

I am a bit fixated on how people spell my name – for those of you who don’t know, it’s “Nishant” – 7 unrepeated letters that even a 4-year old should find it trivial to remember. Unfortunately enough, many Indian names that end in “nt” are sometimes pronounced and spelled as “nth”, so there’s a common misspelling of my name where there’s an “h” suffixed to the “ant”. Westerners never spell my name wrong, they always get it right. It’s the Indian folks who sometimes think they know what’s best and misspell my name with the trailing “h”. And sometimes it happens on my own blog. People address me using the misspelling when leaving a comment or sending me an email, and when you consider that a majority of these communications are requests for help, you’d think they’d have the basic courtesy to at least get my name correct. Oh well, at least nobody’s misspelled “Nish” so far.

Read Full Post »

This one was simple to resolve but it did halt my progress for about 5-6 minutes recently. I had a form (actually a user control) that had a control on it added at design time docked to the right. During run time I was adding a new WinForms control that I had set to DockStyle.Fill. Imagine my surprise when the dynamically added control filled the entire user control instead of adhering to the expected docking behavior. Turns out the control that fills the rest of the space always needs to be the first control that’s on the form or user control. When doing it dynamically, you need to call ControlCollection.SetChildIndex. Here’s a sample code snippet :

private void MainForm_Load(object sender, EventArgs e)
{
    TextBox textBox = new TextBox() { Multiline = true, Dock = DockStyle.Fill };
    this.Controls.Add(textBox);
    this.Controls.SetChildIndex(textBox, 0); // move it to the 0th position
}

Read Full Post »

Follow

Get every new post delivered to your Inbox.