• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Simple?? VB.net question

xoggoth

More fingers than teeth
Thanks received
1,183
Got a useful answer last time, so worth a try.

I just want to stick all the common functions called from different forms in one place rather than duplicate code, and for some I need to pass forms by reference to those functions. But if I have something like:-

Public Sub EnableDoubleBuffering(ByRef frm As System.Windows.Forms.Form)

in a public module or class, any reference to frm gives 'not accessible in this context because it is protected'.

The solutions I have looked at on the net all seem to to be along the lines of declaring a new instance of, or inheriting from, a specific form class, however:-

a) frm does not necessarily always refer to the same form class
b) frm could refer to different instances of the same form class

I can think of a rather messy solution to a) but not sure how to handle b). Ta for any answers.
 
Bored so:-

as buffdaddy says you should probably be passing it by value unless you are tring to return a different form than the one passed in - which migh be unhelpful :wink

Anyway this is fine:-

Module Module1
Public Sub EnableDoubleBuffering(ByRef frm As System.Windows.Forms.Form)
frm.Text = "QQQQ"
End Sub

End Module

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Module1.EnableDoubleBuffering(Me)
Dim a As New Form2

'Has to be a type of Form because it will foul up due to narrowing if not.
Dim b As Form
b = a
a.AA(b)

End Sub
End Class

Public Class Form2

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Friend Sub AA(ByRef frm As System.Windows.Forms.Form)
Module1.EnableDoubleBuffering(Me)
Module1.EnableDoubleBuffering(frm)
End Sub
End Class

Whether it is sensible or any help is a different matter of course.
 
That works although same problem with my original code. I was thinking that the inaccessibility error was a general problem in not being able to access the form, in fact it was just a specific function of that form that is protected. Don't need to use it anyway (it was just an example I copied off an MS page) so problem sorted. Ta chaps!
 
Last edited: 09:19

Advertisers

Back
Top