referring to self in a class function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    referring to self in a class function

    I'm not sure how to do this. I want to create a function inside my class that will return a boolean on the existence of some data within any structures created with the class.

    Code:
    Public Class MyObject
         Public Object1 as String
         Public Object2 as String
    End Class
    
    Public Class MyObjectList
         Public Objects() as MyObject
         Public Function Contains(Obj as String) as boolean
              For I = 0 to [ObjectCreatedFromClass].[number of items in the objectcreatedfromclass]
                     If [ObjectCreatedFromClass](I) = Obj then
                          Return True
                     End if
               Next I
               Return False
         End Function
    End Class
    What I don't know how to do is refer to the object that called the function to get values, including the number of items in the referring objects array.
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #2
    Ok - got it. It was just too easy...
    (glad you folks put up with me.)
    Here's how its done
    Code:
     Public Class MyObject
          Public Object1 as String
          Public Object2 as String
     End Class
      
     Public Class MyObjectList
          Public Objects() as MyObject
          Public Function Contains(Obj as String) as boolean
               For I = 0 to me.count
                      If me(I).Object1 = Obj then
                           Return True
                      End if
                Next I
                Return False
          End Function
     End Class
    Me refers to the current instance of the object.

    DOH!

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      With just a little shift is code you could make this a property instead of a function. Basically it would go into the get method of the property. Then you can use it as a bool instead of a function with a return

      C#
      Code:
      public bool IsOn
      {
         get { return btnMeaningfulName.Checked; }
         set {
                  btnMeaningfulName.Checked = value;
                  panelDashboard.Visible = value;
                  labelStatus = value == true ? "On" : "Off";
                  btnRunNow.Enabled = value;
      }

      Comment

      Working...