Results 1 to 2 of 2

Thread: [Resolved] Object created in Sub is not valid after exit. ???

Threaded View

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    [Resolved] Object created in Sub is not valid after exit. ???

    I have a bunch of these and I'd rather send a single line to a sub than have a mile-long sub creating all of them.

    This doesn't work. Msgbox returns "True" (Is Nothing). ???
    .

    Code:
    Private Sub CreateIndicator(ByRef Indicator As cIndicator, ByRef Picturebox As VB.Picturebox, ByRef Label As VB.Label, ByRef LabelPosition As LABEL_POSITION, ByRef Tooltip As String, ByRef IndicatorCollection As Collection)
    
    Set Indicator = New cIndicator ' Is valid while in this sub. Then not.
    
    With Indicator
    
      Set .Picturebox = Picturebox
      Set .CompanionLabel = Label
      .LabelPosition = LabelPosition
      .IgnoreClicks = True
      .TooltipText = Tooltip
    
      .Backcolor = &H404040
      .OffColor = &H400000
      .OnColor = &HC00000
      .CompanionLabel.Fontsize = 8
    
      .Refresh
    
    End With
    
    IndicatorCollection.Add Indicator
    
    End Sub
    
    
    Private Sub CreateIndicators()
    Dim m_Indicator As cIndicator
    
    Set colIndicators = New Collection
    Set colSwitches = New Collection
    
    CreateIndicator mw_Indicator_DND, picIndicator_DnD, lblIndicator_DnD, idx_LabelPosition_Left, "Do Not Disturb.", colIndicators
    
    MsgBox mw_Indicator_DND Is Nothing ' Returns True.
    
    ' <snip>
    
    end sub
    
    Private Sub CreateIndicator(ByRef Indicator As cIndicator, ByRef Picturebox As VB.Picturebox, ByRef Label As VB.Label, ByRef LabelPosition As LABEL_POSITION, ByRef Tooltip As String, ByRef IndicatorCollection As Collection)
    
    Set Indicator = New cIndicator
    
    With Indicator
    
      Set .Picturebox = Picturebox
      Set .CompanionLabel = Label
      .LabelPosition = LabelPosition
      .IgnoreClicks = True
      .TooltipText = Tooltip
    
      .Backcolor = &H404040
      .OffColor = &H400000
      .OnColor = &HC00000
      .CompanionLabel.Fontsize = 8
    
      .Refresh
    
    End With
    
    IndicatorCollection.Add Indicator
    
    End Sub
    But this works fine.

    Code:
    Private Sub CreateIndicators()
    Dim m_Indicator As cIndicator
    
    Set colIndicators = New Collection
    Set colSwitches = New Collection
    
    Set mw_Indicator_DND = New cIndicator
    With mw_Indicator_DND
    
      Set .Picturebox = picIndicator_DnD
      Set .CompanionLabel = lblIndicator_DnD
      .LabelPosition = idx_LabelPosition_Left
      .TooltipText = "Do Not Disturb."
    
    End With
    colIndicators.Add mw_Indicator_DND
    
    <snip>
    
    For Each m_Indicator In colIndicators
    
      With m_Indicator
    
      .OffColor = &H400000
      .OnColor = &HC00000
      .Backcolor = &H404040
      .IgnoreClicks = True
      .CompanionLabel.Fontsize = 8
      .Refresh
    
      End With
    
    Next m_Indicator
    Last edited by cafeenman; Dec 2nd, 2025 at 12:20 PM.

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