Problems with a CLASS

Ok, here is my situation. I have created a class called “Calculations” in the main form of my project, I declare this class as  “Dim Calcs as New Calculations” and everything is fine and dandy, but In this same project I have another form where I also have to access the same class. So I do the same declaration to access the same class “Dim Calcs as New Calculations” For some reason other than I’m a rookie programmer wana-be , the values that I set while in a form other than the main form do not stay permanent, in other words, as soon as I exit that form, the value goes back to “0”. Can anyone help me on this one Thanks

 

William



Answer this question

Problems with a CLASS

  • Rahul Ohri

     

     

    Oh No Dman... you take it!

    How does one declare a singleton in VB2005

    {Help militate for Emoticons with hair!!!!}

     



  • PhilipLanier

     

     

    What I have done in the past in that in form1 I have declared a class as a member variable

    Public sio as new foo

    In form2 I'll reference

    form1.sio.AppropriateDataAndFunctions

    This references Form1's instantiation of the class

    What you are are doing does reference the same class but creates as new instantiation of the class with it's own new set of variables which are different in memory from that first instatiation of the class.

    There is another way to go about this. In the class if it's advisable and you want only one copy of a variable across all instantations is to declare that variable as a member variable with the shared qualifer

    Public Shared VariableGreen as Integer



  • Curt Carpenter

    actually VB is the easiest language to implement the singlton becasue of modules (the are static by default nature) But you can also use a class with shared members to implement a singleton but anyway here ya go:

    Module Module1

    Private MySingleton As MySingletonClass

    Public Function GetMySingleton() As MySingletonClass

    If IsNothing(MySingleton) Then

    MySingleton = New MySingletonClass

    Return MySingleton

    Else

    Return MySingleton

    End If

    End Function

    End Module

    Public Class MySingletonClass

    Private m_ID As Integer

    Public Property ID() as Integer

    Get

    Return m_ID

    End Get

    Set(ByVal value As Integer)

    m_ID = value

    End Set

    End Property

    End Class

    So from form1 button 1 i could do this

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

    GetMySingleton().ID = 1

    End Sub

     



  • Simon_E

    you can also include this function in a module but really what you are talking about is its another singlton issue...I'll let Renee take it from here

     

     



  • AndrewB

    Ok...

    I understood those things. I use modules for constants, enums, structure definitions and api call definitions.

    I really don't like to put actual variables in them. You're right there's only one copy of those variables and that's the way it is for shared variables in basic classes.

    Thank you for your time !

     

     



  • Problems with a CLASS