How to pass a variable by reference to a Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LeoVBNET
    New Member
    • Apr 2013
    • 29

    How to pass a variable by reference to a Form

    Hi. I make an instance of a form 'B' from Form 'A'. But I need to pass two variables by reference.
    I do what you can see below:

    Code:
    FORM 'B'
    Private Val1 As Uintenger
    Private Val2 As Boolean
    Public Sub New(ByRef _Val1 As Uinteger, ByRef _Val2 As Boolean)
    InitializeComponent()
      Val1 = _Val1
      Val2 = _Val2
    End Sub
    
    Private Sub Proceso()
      Val1 = 333
      Val2 = True
      Me.Close
    End Sub
    
    FORM 'A'
    Dim FormB As New 'B' (Val1, Val2)
    FormB.ShowDialog()
    The problem is that Val1 and Val2 keep their own values before calling Form 'B'

    I made an example passing an ArrayList and this modify its value when return, but it seems to be that primitives types (boolean, integer, string, etc) does not work by reference in this context.

    Other thing I tried is to pass this variables as Object type, but neither works.

    Thanks.
    Last edited by Rabbit; Jul 9 '13, 03:33 PM. Reason: Fixed code tags
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I haven't had much to do with creating instances of forms like this. But a thought occurs... have you tried using global (sorry, I suppose I mean "Public") variables, rather than passing them? This might be ugly, but if you can't get ByRef to work, at least it might provide a workaround.

    Comment

    Working...