2

i have 2 forms

Form 1:

 Public Class Form1
     Public Sub MySub()
     End Sub
 End Class

Form 2:

Public Class Form2
 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       Form1.MySub()//why??
End Sub
End Class

why can i write Form1.MySub() in VB but C# does not

Hai Duong
  • 21
  • 2
  • What do you mean you can't in C#? Can you give an example? As long as it is public function, it can be called outside class – Pratik Gaikwad Nov 19 '16 at 00:49
  • 6
    Form1 is a type name, not an object reference. VB's default instance feature was created to help programmers move to .NET. Quite crippling to a programmer's mind, very hard to learn object-oriented programming this way. Also causes many threading problems. OOP is what you'll need to learn, no shortcuts in C#. – Hans Passant Nov 19 '16 at 00:53
  • @PratikGaikwad: You need an instance to call an instance method - as Hans mentioned, VB creates a default form instance named the same as the form. This is one of the worst 'features' of VB - it truly encourages a lack of understanding. – Dave Doknjas Nov 19 '16 at 05:20
  • @DaveDoknjas, yup got it. – Pratik Gaikwad Nov 19 '16 at 05:24

1 Answers1

3

The VB.Net compiler provices a Default Instance for some things. C# does not.

This is done in VB.Net at least partly for compatibility with older VB6-era code, and at least partly not done in C# because C# does not allow naming collisions between types and variables.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794