Cary Bondoc
Cary Bondoc

Reputation: 2988

Creating form name and object name as parameters of a function

I'm trying to pass form name and object as parameters of my public sub because I have 2 forms and they are almost identical to one another.

For example I have richtextbox1 in Form1 and I also have richtextbox1 in Form2, I would like to create a public sub something like this.

Public Sub Sub1(ByVal frm_name As form, Byval obj_name As object)
    frm_name.obj_name.Lines.Length 'Get the length of lines of that richtextbox
End Sub

Then when I want to call it

'Form 1 Codes
Public Class Form1
   Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
       Sub1(me, richtextbox1)
   End Sub
End Class

'Form 2 Codes
Public Class Form2
   Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
       Sub1(me, richtextbox1)
   End Sub
End Class

It is not working the way I want it, is there anything I can do for it to work?

Upvotes: 0

Views: 72

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

Think about it. You are saying that you're passing the names of things but you're not. You're not passing the name of a form; you're passing the form itself. You're not passing the name of an object; you're passing the object itself. What is the actual point of this? It's to get the number of lines in a RichTextBox, right? So, write a method that takes a RichTextBox as an argument and then call it passing a RichTextBox.

Public Sub Sub1(rtb As RichTextBox)
    Dim lineCount = rtb.Lines.Length

    '...
End Sub

'Form 1 Codes
Public Class Form1
   Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
       Sub1(richtextbox1)
   End Sub
End Class

'Form 2 Codes
Public Class Form2
   Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
       Sub1(richtextbox1)
   End Sub
End Class

Upvotes: 2

Related Questions