0

I had long time developing windows applications using VB.NET and one of thing that I was always use is:

Class (Test) and inside it there are some subs like the following:

VB.NET Code

 Friend Sub FLoadKeyLock(ByVal FRMNAME As Object)
        FRMNAME.Button3.Enabled = true;
        FRMNAME.Button4.Enabled = true;
        FRMNAME.Button1.Enabled = false;
        FRMNAME.Button2.Enabled = false;
        FRMNAME.Button5.Enabled = false;
        FRMNAME.Button6.Enabled = false;
        FRMNAME.Button7.Enabled = false;
        FRMNAME.Button8.Enabled = false;
        FRMNAME.Button9.Enabled = false;
        FRMNAME.Button10.Enabled = false;
 End Sub

Where FRMNAME refers to forms name that I send from different forms and I use (me) keyword to send current form name to apply that sub.

That was totally works fine in VB.NET but I cannot use it the same way in C#.NET.

C#.NET Code

 internal void FLoadKeyLock (Form FRMNAME)
    {
        FRMNAME.Button3.Enabled = true;
        FRMNAME.Button4.Enabled = true;
        FRMNAME.Button1.Enabled = false;
        FRMNAME.Button2.Enabled = false;
        FRMNAME.Button5.Enabled = false;
        FRMNAME.Button6.Enabled = false;
        FRMNAME.Button7.Enabled = false;
        FRMNAME.Button8.Enabled = false;
        FRMNAME.Button9.Enabled = false;
        FRMNAME.Button10.Enabled = false;
    }

The Error is: System.Windows.Forms.Form' does not contain a definition for 'Button3' and no extension method 'Button3' accepting a first argument of type

And same error for all used buttons.

So I have two questions:

1 - How to use such a function in C#.NET?

2 - Why it's not working the same way as VB.NET?

4
  • If you want to use dynamic, change the C# method signature to internal void FLoadKeyLock (dynamic FRMNAME). Obviously you compile-time type-safety. Commented Nov 12, 2015 at 12:31
  • 1
    Your method has no actual way of "knowing" what buttons are there in your form, unless you pass the actual type of the form (like MyForm, instead of the base Form class. If it was a Form, how can you ensure that your method will not receive a form instance that doesn't actually have any of those buttons? Commented Nov 12, 2015 at 12:39
  • That said, I suggest you research about proper design patterns. Commented Nov 12, 2015 at 12:40
  • @Maarten Brilliant, Solved my problem (Many thanks to you). Commented Nov 12, 2015 at 12:57

1 Answer 1

1

As @Maarten said. dynamic has solved my problem

internal void FLoadKeyLock (dynamic FRMNAME)
{
    FRMNAME.Button3.Enabled = true;
    FRMNAME.Button4.Enabled = true;
    FRMNAME.Button1.Enabled = false;
    FRMNAME.Button2.Enabled = false;
    FRMNAME.Button5.Enabled = false;
    FRMNAME.Button6.Enabled = false;
    FRMNAME.Button7.Enabled = false;
    FRMNAME.Button8.Enabled = false;
    FRMNAME.Button9.Enabled = false;
    FRMNAME.Button10.Enabled = false;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.