Results 1 to 5 of 5

Thread: Changing variable in different class

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Changing variable in different class

    Looking for the best way to update a variable in a different class. Below is a rough outline of the situation.

    I am wondering if in fnSendEmailTo if I can access the Property in CB? If not, how would you do something like this?

    Code:
    file: CB.vb
    ----------------------------
    Imports Project.Functions
    
    Public Class CB
     Private _CurrentEmailsSentCount As Integer = 0
     Private _ID As Integer = 23
    
        Public Property CurrentEmailSentCount() As Integer
            Get
                Return _CurrentEmailsSentCount
            End Get
            Set(value As Integer)
                _CurrentEmailsSentCount = value
            End Set
        End Property
    
     Public Function fnStart() As String
    	CurrentEmailSentCount = fnGetJobPostingID_EmailsSentCount(_ID)
    	Return sRetVal
     End Function
    End Class
    
    file: Functions.vb
    ----------------------------
    Public Class Functions
    
     Public Shared Function fnSendEmailTo(...various parameters..) As String
       '... code
       If iEmailSent = 1 Then
        'update the _CurrentEmailsSentCount variable here from class CB
        CurrentEmailSentCount += 1
       End If
       '... code
     End Function
    End Class
    Last edited by lleemon; Mar 8th, 2012 at 11:54 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Changing variable in different class

    you could make the property shared:

    vb Code:
    1. Public Class CB
    2.     Private Shared _CurrentEmailsSentCount As Integer = 0
    3.     Private _ID As Integer = 23
    4.  
    5.     Public Shared Property CurrentEmailSentCount() As Integer
    6.         Get
    7.             Return _CurrentEmailsSentCount
    8.         End Get
    9.         Set(ByVal value As Integer)
    10.             _CurrentEmailsSentCount = value
    11.         End Set
    12.     End Property
    13.  
    14.     Public Function fnStart() As String
    15.         CurrentEmailSentCount = fnGetJobPostingID_EmailsSentCount(_ID)
    16.         Return sRetVal
    17.     End Function
    18.  
    19. End Class

    vb Code:
    1. Public Class Functions
    2.     Public Shared Function fnSendEmailTo(...various parameters..) As String
    3.         Dim iEmailSent As Integer = 1
    4.         '... code
    5.         If iEmailSent = 1 Then
    6.             'update the _CurrentEmailsSentCount variable here from class CB
    7.             CB.CurrentEmailSentCount += 1
    8.         End If
    9.         '... code
    10.     End Function
    11.  
    12. End Class

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Changing variable in different class

    why make it shared? It's already public... and exposed through the property... if you want to update it, then you need a reference to the instance that you want to update...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Changing variable in different class

    Making it shared could have some negative consequences. After all, if you did that, you wouldn't even be ABLE to have different instances of that class. At least, they wouldn't be able to work the way they currently do. On the other hand, with a name like CurrentEmailSentCount, perhaps this is a class where there will never be more than one instance anyways, in which case a Shared item would make sense.
    My usual boring signature: Nothing

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Changing variable in different class

    Quote Originally Posted by Shaggy Hiker View Post
    On the other hand, with a name like CurrentEmailSentCount, perhaps this is a class where there will never be more than one instance anyways, in which case a Shared item would make sense.
    that was my guess too. i couldn't answer sooner... i was busy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width