Question Store the reference passed to a sub

Budius

Well-known member
Joined
Aug 6, 2010
Messages
137
Location
UK
Programming Experience
3-5
Hi guys,

I've searched a lot on both google and here but sorry, couldn't find.
Maybe I'm just asking the wrong question, so I'll try to explain it here to see if someone can help me:

what I'm trying to do is what in C would be as simple as int varptr = &var

I have a form that is just a communication watcher/logger. It's just a blank text box with a Pause/Play button and a copy to clipboard button and every new activity on my serial communication is logged. At the moment the form have a timer that once a second do textbox.text = serial1.log ...

but the problem is that I also have serial2; serial3; sql1 that I would like to keep an eye on them, and considering my work here I'm sure I would be re-using this form a lot if I could make it portable. Create one form for each is just stupid, as you'll all agree.

Then I would like to have an sub initialise(byref log as string) that would store this reference into an local variable within the form; so that every new instance of my form could be initialised with whatever log they'll be looking for and the timer just make textbox.text = LogPointer to the actual value of the log string.
So, as I said I really wanted a int varptr = &var

Any suggestions?
thanks
 
Then I would like to have an sub initialise(byref log as string) that would store this reference into an local variable within the form; so that every new instance of my form could be initialised with whatever log they'll be looking for and the timer just make textbox.text = LogPointer to the actual value of the log string.


Your answer is actually in your question. I believe that you will need to make a constructor for your form and pass in the LogPointer value that you need and store as a class level variable.

VB.NET:
Public Class LogForm
     
     Dim LogPointer As String

     'constructor for new form
     Public Sub New(Byval PassLogPointerValue As String)
          If Not String.IsNullOrEmpty(PassLogPointerValue) Then
               LogPointer = PassLogPointerValue
          End If
     End Sub

     'Rest of your code

End Class

and how to use it

VB.NET:
     'On another form, inside a sub or function (could be an event)
     Dim frmLogForm As New LogForm("Value to pass to form")
     frmLogForm.Show()
 
hi demausdauth,

thanks for the reply, but I'm afraid that the problem is a bit more complex:
The string I want the form to show is been modified once a second and the form should update it in it's timed interval event.

At the moment I have in this form a one second timer with:
VB.NET:
    Private Sub Update_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateTM.Tick
        TextBox.Text = Seria1l.Log
    End Sub

And to make this class generic and re-usable I would need that this Serial1.log to be a "pointer" or reference to a string that was passed to the constructor and locally stored.

Any more suggestions anyone??



===============EDIT===============

hi again,

I tested what you said, and it behaved as I originally foreseen, it passed the value of that string and that was all that it was shown.
But then, using example you posted HERE I managed to pass the whole Serial1 object to the constructor and show the .log property on the timer.

example to whoever might interest:
VB.NET:
Public Class frm_Watcher

    Dim myLog As SerialComm
    Public Sub New(ByVal Log As SerialComm)
        myLog = Log
        Call InitializeComponent()
    End Sub

    Private Sub Update_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateTM.Tick
        TextBox.Text = myLog.Log
    End Sub
-
-
-   more code
-
-
End Class
then to call it:
VB.NET:
    Private Sub SerialWatcherToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SerialWatcherToolStripMenuItem.Click
        Dim watcher As New frm_Watcher(SerialConn1)
        watcher.Show()
    End Sub

===============EDIT 2=============

does anyone have a suggestion on how (and if possible) to do it passing just a string?
 
Last edited:
String Class (System) see section "Immutability and the StringBuilder Class".
VB.Net doesn't have pointers (for manipulation of memory address), only reference types (for manipulation of object), but this is not relevant for String class that is immutable.
There is one feature that allow pointer like manipulation of a string, and that is ByRef parameters, this is the only case where a local method can reassign the callers String object.
 
thanks JohnH,
I read there and it does make sense.

I guess I could change the routine to StringBuilders, but now it's all set and done, I shall just keep it.

thanks for the replies guys.
 
Back
Top