Author Topic: using class with AdvancedHMI  (Read 2372 times)

MEDALI1TN

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
using class with AdvancedHMI
« on: December 24, 2018, 03:03:23 PM »
Dear all,
please i need to use a class in my program.
 
This one example that working good for me but without class (all in the main form):

Public Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        OpcDaCom1.Subscribe("adress_of_opc", 1, 0, AddressOf test)
    End Sub

Public Sub test(sender As Object, e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs)
        variable = e.Values(0)
    End Sub


But i need to add the "public sub test" in another class and i will call it in the main form.

Thx

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: using class with AdvancedHMI
« Reply #1 on: December 24, 2018, 03:28:58 PM »
Public Sub test(sender As Object, e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs)
        MyClassInstance.Test(e.Values(0))
End Sub

MEDALI1TN

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: using class with AdvancedHMI
« Reply #2 on: December 24, 2018, 03:56:59 PM »
thx Archie;

i must write it inside class or mainform
if inside class, how can i call it inside mainform or i dont need to do it ?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: using class with AdvancedHMI
« Reply #3 on: December 24, 2018, 08:50:12 PM »
Can you explain more specifically what you are attempting to do?

MEDALI1TN

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: using class with AdvancedHMI
« Reply #4 on: December 25, 2018, 07:41:34 AM »
In my MainForm i have this code :

Imports System.Math
Imports MySql.Data.MySqlClient
Imports System.Data.DataTable
Imports System.Data.SqlClient

Public Class MainForm
Public variable As String

Public Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        OpcDaCom1.Subscribe("adress_of_opc", 1, 0, AddressOf test)
    End Sub

Public Sub test(sender As Object, e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs)
        variable = e.Values(0)
    End Sub

End Class

And i want to use a MainForm + a other new class in my program that i have :

1 - the opc adress in the MainForm:

Public Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        OpcDaCom1.Subscribe("adress_of_opc", 1, 0, AddressOf test)
    End Sub

2 - call the sub test from the other new class

call the function "sub test"

3 - the new class that i need :

Public Sub test(sender As Object, e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs)
        variable = e.Values(0)
    End Sub


so my question is ; how can i call the function in the MainForm ?






Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: using class with AdvancedHMI
« Reply #5 on: December 25, 2018, 08:06:54 AM »
Public Class MainForm
    Private MyClassInstance As New SecondClass

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        OpcDaCom1.Subscribe("adress_of_opc", 1, 100, AddressOf MyClassInstance.Test)
    End Sub
End Class




Public Class SecondClass
    Public Sub Test(sender As Object, e As Drivers.Common.PlcComEventArgs)
        '* Do something with returned value
    End Sub
End Class

MEDALI1TN

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: using class with AdvancedHMI
« Reply #6 on: December 25, 2018, 04:51:56 PM »
sorry Archie but I did not get to work.
can you please see the pic

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: using class with AdvancedHMI
« Reply #7 on: December 25, 2018, 05:04:10 PM »
The load event fires only 1 time on startup, so you set your TextBox to the variable value, which is initially empty. Then after the subscriptions start, the variable is updated in the second class, but that value is not pushed into your TextBox

You will need to add an event to your second class and hook into that event on the main form.
« Last Edit: December 25, 2018, 05:09:14 PM by Archie »

MEDALI1TN

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: using class with AdvancedHMI
« Reply #8 on: December 26, 2018, 04:17:47 PM »
Thx Archie.
I think I have to learn some basics of the vb.net because i cant do it.

Phrog30

  • Guest
Re: using class with AdvancedHMI
« Reply #9 on: December 26, 2018, 08:07:13 PM »
Archie asked you if could explain specifically what you are trying to do.  Without showing code, explain in elementary terms what you are trying to do.  There might me a better, easier way.

MEDALI1TN

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: using class with AdvancedHMI
« Reply #10 on: December 27, 2018, 05:27:24 AM »
Thank you very much for your assistance, and I hope that my explanation is clear to you.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: using class with AdvancedHMI
« Reply #11 on: December 27, 2018, 06:35:04 AM »
Mainform
Code: [Select]
    Private MyClassInstance As New SecondClass
    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        OpcDaCom1.Subscribe("adress_of_opc", 1, 100, AddressOf MyClassInstance.Test)
        AddHandler MyClassInstance.DataUpdated, AddressOf DataUpdated
    End Sub

    Private Sub DataUpdated(sender As Object, e As EventArgs)
        TextBox1.text = MyClassInstance.Variable
    End Sub

Second class
Code: [Select]
    Public Event DataUpdated As EventHandler

    Private m_Variable As String
    Public ReadOnly Property Variable As String
        Get
            Return m_Variable
        End Get
    End Property

    Public Sub Test(sender As Object, e As Drivers.Common.PlcComEventArgs)
        m_Variable = e.Values(0)
        OnDataUpdated(EventArgs.Empty)
    End Sub


    Protected Overridable Sub OnDataUpdated(e As EventArgs)
        RaiseEvent (me,e)
    End Sub

MEDALI1TN

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: using class with AdvancedHMI
« Reply #12 on: December 27, 2018, 08:54:35 AM »
Thx
i have a question please

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: using class with AdvancedHMI
« Reply #13 on: December 27, 2018, 09:22:18 AM »
It is correct. It is not references the variable (field), it is referencing the property. As a rule of OOP, you do not make fields public.

MEDALI1TN

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: using class with AdvancedHMI
« Reply #14 on: December 27, 2018, 10:14:19 AM »
where I have to declare my variable ?
i speak about "variable" and not about "m_variable"