• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • 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

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Simple?? VB.net question

    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.
    bloggoth

    If everything isn't black and white, I say, 'Why the hell not?'
    John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

    #2
    not sure why you are passing the Form in by ref. It should work without the ref since the Form is a Reference Type
    http://msdn2.microsoft.com/en-us/lib...s2(VS.80).aspx

    i tried this quickly (in c# winforms), and it works

    Comment


      #3
      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

      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.

      Comment


        #4
        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 by xoggoth; 29 May 2007, 08:37.
        bloggoth

        If everything isn't black and white, I say, 'Why the hell not?'
        John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

        Comment

        Working...
        X