2

Recently I've picked an old project in VB.NET and I keep finding things like this that annoy me:

Dim answer as String
answer = Form_Input_Text.Lbl_Answer.Text

Apparently, in VB.NET it is allowed to access stuff which should belong to an instance of a Form as if it is something static. In C# one would need to write this:

string answer = null;
using (Form_Input_Text my_form = new Form_Input_Text())
{
    //stuff
    answer = my_form.Lbl_Answer.Text;
}

The first bit of code is compiled with no errors, but sometimes I get NullReferenceException as expected.
How do I enforce this behaviour on VB.NET?

Tea With Cookies
  • 950
  • 6
  • 18
  • If `Form_Input_Text` is an instance of a form with the name `Form_Input_Text` and `Lbl_Answer` is a public control or public property that maps to a control, the VB.NET code is not accessing a shared field. The C# code is pointless anyway since you're creating an instance on the fly, so no user can change the `Text` on it. – Tim Schmelter May 04 '15 at 11:09
  • @TimSchmelter Form_Input_Text is not an instance of a form, it's the name of the class, i need a way to prevent visual studio from allowing this sort of thing – Tea With Cookies May 04 '15 at 11:13
  • You're right, it compiles. Never tried it, but no idea why. +1 **Edit** Related: https://msdn.microsoft.com/en-us/library/aa289529(v=vs.71).aspx (search: _" a special feature of Visual Basic prior to .NET allows you to work with Forms without creating an instance"_) – Tim Schmelter May 04 '15 at 11:17
  • @TimSchmelter Where in your link it says this is legal in VB.net? It doesn't compile for me. (I'm using linqpad and not using Form obviously). I tried with String.Length. It shouldn't make any difference I guess. – Sriram Sakthivel May 04 '15 at 11:26
  • 2
    @SriramSakthivel: i have tried it in VS2010 with Option Explicit and Option Strict and it compiles and creates a default instance at runtime. The behaviour is documented and exists due to backwards compatibiliyt with VB6. http://stackoverflow.com/questions/4698538/why-is-there-is-a-default-instance-of-every-form-in-vb-net-but-not-in-c By the way, here is a duplicate of this question http://stackoverflow.com/questions/26732778/disable-default-instance-in-vb-2010 It seems not to be possible to disallow this behaviour. – Tim Schmelter May 04 '15 at 11:27
  • You said *Recently I've picked an old project in VB.NET* You mean VB6 project? – Sriram Sakthivel May 04 '15 at 11:28
  • @SriramSakthivel: it is a special "feature" for windows forms, so no, it doesn't work with all types. You have to test it with a form, say `Form1.Text`. – Tim Schmelter May 04 '15 at 11:30
  • @TimSchmelter Cool. Good to know. Why don't we close this as duplicate then. – Sriram Sakthivel May 04 '15 at 11:31
  • @SriramSakthivel: because you can't vote to close with a duplicate that has no accepted or upvoted answer. – Tim Schmelter May 04 '15 at 11:33
  • Ok, thanks, seems like it was designed to allow this. – Tea With Cookies May 04 '15 at 11:34
  • @TeaWithCookies: unfortunately, yes. It's one of those ugly compiler tricks that generate magic code. They should make a clean break. – Tim Schmelter May 04 '15 at 11:34
  • If nothing works out you could write a custom code analysis rule or roslyn analyses which flags the use of this as warning or error. – Sriram Sakthivel May 04 '15 at 11:39

0 Answers0