Linking form issue

C Dull

Member
Joined
Jun 15, 2021
Messages
10
Programming Experience
Beginner
Any idea why the standard:

Form2.Show()

wouldn't be working for me? I'm trying to link a menu button to another form. Youtube and other tutorials made it seem that easy.

I named my 2nd form frmAbout, as I'm trying to link a separate "about" form to the menu of my main form.

C#:
private void menAbout_Click(object sender, EventArgs e)
        {
            frmAbout.Show()
        }

Error:
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Control.Show()' StraightlineDepreciationCalculator C:\Users\koval\source\repos\StraightlineDepreciationCalculator\StraightlineDepreciationCalculator\Straightline Depreciation Calculator.cs 207 Active

 
As the error says, you need to create an instance of your other form, and then call Show().

Assuming you were following the correct Hungarian notation, your code should have been:
C#:
FormAbout formabout = new FormAbout();
FormAbout.Show();

In proper Hungarian, only the basic types have short names or abbreviations. All other type names should be fully spelled out without abbreviations. Therefore, 'men' is no the correct Hungarian for a Menu, nor is 'frm' the correct abbreviation for Form.
 
As the error says, you need to create an instance of your other form, and then call Show().

Assuming you were following the correct Hungarian notation, your code should have been:
C#:
FormAbout formabout = new FormAbout();
FormAbout.Show();

In proper Hungarian, only the basic types have short names or abbreviations. All other type names should be fully spelled out without abbreviations. Therefore, 'men' is no the correct Hungarian for a Menu, nor is 'frm' the correct abbreviation for Form.

Hey Skydiver,

I appreciate the quick reply -- although I'm still getting the same error message on the .Show line. Not sure whats happening.
 
Sorry. Autocorrect was trying to help me. Last line should be:
C#:
formabout.Show();
 
Even Autocorrect didn't like Hungarian and tried to make it Pascal case like a normal human being would expect to read. :)
 
If you think you should be able to just call Show on the form class, rather than a variable that refers to an instance of the form class, then I can only assume that you have been reading/watching some VB tutorials. VB supports a feature called default instances where you can use the type name to refer to an instance provided by the system. It makes accessing form objects across your project easier but it also causes other problems, because people start to think of forms as being different to other types of objects when they aren't. Just as with any type, you need to create an instance of that type before you can do anything with it. OOP is based on the behaviour of real-world objects, with classes just being a description of what objects of that type have and do. In the real world, you can't play in the park with dog. You need to have A dog. The same goes for programming types and objects.
 
Last edited:
If you think you should be able to just call Show on the form class, rather than a variable that refers to an instance of the form class, then I can only assume that you have been reading/watching some VB tutorials. VB supports a feature called default instances where you can use the type name to refer to an instance provided by the system. It makes accessing form objects across your project easier but it also causes other problems, because people start to think of forms as being different to other types of objects when they aren't. Just as with any type, you need to create an instance of that type before you can do anything with it. OOP is based on the behaviour of real-world objects, with classes just being a description of what objects of that type have and do. In the real world, you can play in the park with dog. You need to have A dog. The same goes for programming types and objects.
Sorry. Autocorrect was trying to help me. Last line should be:
C#:
formabout.Show();
jmcilhinney -- haha, thanks for the analogy, that actually helped my understanding!

skydiver -- thanks again for taking the time.

Hate asking these dumb questions, I really do make an effort to source answers for myself before bugging you guys. Thank you.
 
I'm glad my analogy helped, even though it included a typo and I said:
you can play in the park with dog
when I meant the complete opposite:
you can't play in the park with dog
I always recommend to people to think about how an object would work in the real world to get an idea of how it would/should work in programming. There are obviously some differences but real-world object behaviour is the inspiration and basis of OOP, so there are far more similarities.
 
Back
Top Bottom