Instance of Class and context

Hi,

I have a project that contains the following:

Form1------------------Form

AccountsForm-------Form

Search-----------------Form

AccUpdate-----------Class

I have created the following instances of the above

AccountsForm in Form1 and called it ObjAccountsForm
Search in the AccountsForm and called it ObjSearch
AccUpdate in Search and called it ObjAccUpdate

My question is

When I need to reference, forexample, the instance of the AccUpdate Class form within my project I am having to type the full context to its instance

Form1.objAccountsForm.objsearch.ObjAccUpdate.somepublic property in this Class

My project contains many more Class's, and I have had to draw a table to keep track of where each instance of a class was created.

The project works fine but I am wondering if I am missing something with regards to referenceing an instance of a Class that could make it easier for me to code and remember.

Ron




Answer this question

Instance of Class and context

  • Mark Wilkins

     

     

    It's ok to mark an answer and keep going. You received the answer to your original question and now you have another.

    There are a couple of ways to deal with your issue. One is to Declare Delegates another is to declared events with delegates.

    I do a little bit of that. But not much so I have an alternate way.

     

    In Class2, I declare and instantiate controls with a Friend WithEvents clause and declare eventhandlers for them and generally I do all of the processing that I need to in that class.

    When I have a need to call another class, since everything is made accessible via the common structure which defeat the hierarchy that OOP attempts to establish, I'll public put a property or method in that class and call the property or method from the event within the Class2.

    I just like to do this way and I prefer it to using delegates.

     

     

     

     



  • Mateusz Rajca

    Form1.objAccountsForm.objsearch.ObjAccUpdate.somepublic property in this Class

    There is a way to get around this.

    Institatiate the classes as public in a public Common module. All classes will have common acces to them.



  • cues7a

    "but failed"

    Not a lot of information here.



  • vijil

    I'm sorry, I haven't had my first cup of coffee yet.

    Goto the Solution Explorer | select your project | right button | Add New Item | module

    I alway call mine "Common.VB"



  • Hena

    ReeneC

    Yes I know,

    Duplicate class to start with.

    I would really appreciate it if you could show me how I would use your example but related to my simple issue

    To handle the class1 event in form1 when the instance of class1 is defined in a module.

    Many Many Thanks

    ron


  • karthik asok

    ReeneC,

    Many Thanks,

    Have created a test project with many classes, classes that reference other classes. This allows me to make a call to the classes public properties functions without having to remember or code the context to the same instance of that class.

    forexample, before I used a public module,

    if I had created an instance of class1 in Form1 and then created an instance of class2 in class1

    If I needed to reference a public property within the same instance of class2 that was created in Class1 form within my project, I would have to of type it's full context

    Form1.class1.class2.some public property

    now I can just type class2.some public property and it is the same instance of Class2

    Excellent!!

    Ron


  • r3dw1ng

     

    I know, I've read them and although the documentation is detailed, it's sure not intuitive. I have written delagates and I do use them - however, they aren't intuitive and I have internal resistance to them. When I have used them. they work really well and grant an unusal kind of flexibility in VB, like what is known as a "callback" in the cases where I have used them.

    I was responding to another question last last and I was thinking about this thread so I'm adapting that response to you.

    Let's consider a class with events and having a control with event handlers:

    Public Class clsButton

     

        Friend WithEvents ToolstripButton As New ToolStripButton()

     

     

    End Class

     

    The control has been instantiantiated as a friend withevents to give it so it will be able to make asyncronous responses. That's what events are. SO here is an event handler for the control:

     

    Public Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ToolstripButton.Click

            'Process Creation

            Dim item As ToolStripItem = CType(sender, ToolStripItem)

            FrmToolBar.TBCount.Invocations(cflag.ButtonAdded)

            CreateProcess(item.Tag)

        End Sub

     

    It calls a method within it's class.

     

     

    It is another corresponding with another class which is exactly what Form1 is.

     

    Notice it was declared with events so it can respond inside the class to events you will declare.

    Naturally it has events like the one shown below.

     

    What do we usually want to do when there is an event an one class that corresponds or interacts with another class . We may want to inform a class, and interchange data or both. You could use delegates and events. Because I'm resitant to delegates, of I'll just create a property or method in the class I'm going to inform.

     

     

    This is how I do it:

     

     

    Public Class Form1

        Protected Shared m_Foo As String

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

     

        End Sub

     

        Public Shared Property GetString() As String

            Get

                Return m_Foo

            End Get

            Set(ByVal value As String)

                SomeotherMethod()

                m_Foo = value

            End Set

        End Property

     

        Private Shared Sub SomeotherMethod()

            'Do additional processing here

     

        End Sub

     

    End Class

     

    clsButton is instantiated in the common.

     

    Public Class clsButton

     

        Friend WithEvents ToolstripButton As New ToolStripButton()

        Protected Shared CurrentString As String

        Protected Shared UpdatedingString As String

        Protected Shared ClickCount as Integer

        Public Function GetButton() as ToolstripButton

                 

              Return ToolstripButton

     

        End Function

       

        Public Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ToolstripButton.Click

            ClickCount += 1
            UpdatedingString = cstr(ClickCount)

            CurrentString = Form1.GetString()

            Form1.GetString() = UpdatedingString

     

        End Sub

     

    End Class

     

    Is this adequately illustrative  

     

     

     

     



  • Grayson Peddie

    Hi,

    Although the above sort of answers the original post, I have removed the answered check
    as I still have an issue with this and I need it to appear as unanswered for the moment.

    Yes this works fine as above, but I now have the following issue when raising events

    The instances of the classes are defined in the Public Module as follows:

    Public Module Common

    Public myclass1 As New Class1

    Public myclass2 As New Class2

    Public f2 As New Form2

    End Module

    I have now coded an Event within class1 that will be handled in Form1. The problem is how do I code the WithEvents now that I am
    creating the instances of the classes in a module

    Normaly I would create an instance of Class1 within Form1 using the WithEvents, forexample:

    Private withevents objClass1 as Class1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    objclass1 = New Class1

    End Sub

    Private Sub myeventhandler() Handles objclass1.myevent

    MsgBox("Myevent fired")

    End Sub

     


  • DavidR100

    ReneeC,

    Thanks for the reply

    Can you ellaborate on this, I have done a search for public Common module, but failed to find any info relating to my post.

    Thanks

    Ron


  • wum

    Ron, Here is a real life example:

    Option Explicit On

    Imports System.io

    Imports System.Net

    Imports System.Data

    Imports System.Data.OleDb

    Public Module Common

    Dim Settings As New My.MySettings

    Public adodb As New ADONET(GetConnectionString)

    Public iosub As New IOSUBS

    Public CategoriesTable As New DataTable

    Public CurrentTable As New DataTable

    Public Adapter As OleDb.OleDbDataAdapter

    Public Dataset As New DataSet

    Public con As OleDb.OleDbConnection = New OleDb.OleD

    End Module



  • alwisha

    I had to learn that the hard way Ron. You should see my first OOP project. Or maybe... on the other hand you shouldn't.

    I'm glad this has been meaningful for you.



  • dezrtluver

    Thank you for your reply and code,

    I have looked at this and even tried to copy its function in a test project but failed.

    There is two Public classes called clsButtons in your sample

    I would very much appreciate it if we could use code that will resolve the issue to my sample. That is handling an event raised in class1 within form1 but when class1 is instantiated in a public module and not in the Form using WithEvents, Private withevents objClass1 as Class1.

    This will allow me follow the process and gain an understanding of what is happening.

    Many thanks


  • mikelbelaus

    ReeneC,

    I have read and re-read your reply but unable to understand what you mean. I have looked at the MS sample for using Delegates, don't make much sense at the moment.

    Can you give me an example how one would apply your example above to the problem with handling the Event in my sample when declaring the instances of a Class in a public Module.

    Ron


  • rameshkl

    ReneeC,

    If I understand this right, If I have a class that is public module all instances of this class will all reference the same instance of that Class.

    I will create a small project to test my understanding of this.

    Many Thanks

    Ron


  • Instance of Class and context