ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] ½Ð°Ý VBA ¦p¦ó add Object ¨ì ArrayList "ArrayList.Add(Object)"

[µo°Ý] ½Ð°Ý VBA ¦p¦ó add Object ¨ì ArrayList "ArrayList.Add(Object)"

Type Test
    Name As String
    Num As Integer
    Bool As Boolean
End Type

Public ArrList As New ArrayList
   
Sub AddArrayList()
    Dim Test1 as Test
    Test1 .Name ="sophia"
    Test1.Num=3
    Test1.Bool=True
   
    ArrList .Add(Test1)    '''³oÃä·|¨q¥Xerror
    ''' SHOW:
    '''Compile erroe:  only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions.
EndSub
½Ð°Ý¦³¤°»ò¤èªk¥i¥H²K¥[¨Ï¥ÎªÌ¦Û©w«¬ºA¨ìArraylist
ÁÙ¬O¥u¯àName , Num , Bool ¤À§O¦s©ñ¦b¤£¦PªºArraylist

¦^´_ 1# solitary0720
¸Õ¸Õ¬Ý¥Îª«¥óÃþ§O¼Ò²Õ ¨ú¥N User-Defined Type

©w¸qª«¥óÃþ§O¼Ò²ÕTest
  1. Public Name As String
  2. Public Num As Integer
  3. Public Bool As Boolean
½Æ»s¥N½X

TOP

¦^´_ 2# stillfish00


    ½Ð°Ý ª«¥ó"Ãþ§O"¼Ò²Õ ¨ú¥N User-Defined Type
    ¬O«ü¦p¤U©w¸q¶Ü

    Class Test
        Public s_Name as String
        Public s_Num as Integer
        Public s_Bool as Boolen

        Public Property Let Name (str As String)
              s_Name = str
        End Property
        Public Property Get Name () As String
            Name = s_Name
        End Property
    End Class
   
Public ArrList As New ArrayList
Function Body() as string
    Dim Test_1 as Test
    Set Test_1 = New Test
    Test_1.Name = "sophia"

    ArrList.Add(Test_1)
   '''''ÁÙ¬O¦³°ÝÃD Show Run-time error "438"
   '''''Object doesn't support this property or method


End Function

ÁÂÁÂ~³Â·Ð±z¤F

TOP

¥»©«³Ì«á¥Ñ stillfish00 ©ó 2013-12-19 13:01 ½s¿è

¦^´_ 3# solitary0720
¬O«ü VBA project ´¡¤Jª«¥óÃþ§O¼Ò²Õ«á¡AClass1 §ï¦WºÙ¬° Test¡A¶ñ¤W
  1. Public Name As String
  2. Public Num As Integer
  3. Public Bool As Boolean
½Æ»s¥N½X
¦]¬°§Ú¤£ª¾¹DArrayList¦b³]©w¤Þ¥Î¶µ¥Ø­n¤Ä¿ï­þ­Ó
¬G§ï¥ÎCreateObject¥hlate-binding¡A
¤@¯ë¼Ò²Õ¤¤:
  1.    
  2. Public ArrList As Object  'New ArrayList
  3. Function Body() As String
  4.     Dim Test_1 As Test

  5.     Set ArrList = CreateObject("system.collections.arraylist")
  6.     Set Test_1 = New Test

  7.     Test_1.Name = "sophia"   
  8.     ArrList.Add Test_1

  9. End Function
½Æ»s¥N½X
¸Ü»¡¦^¨Ó¡A¤@¯ëVBA¤¤¤ñ¸û±`¨£ªº¤£¬O arraylist ¡A¦Ó¬O¥Î array ©Î dictionary¡C

TOP

¦^´_ 4# stillfish00


    ¥i¥H¤F!!!
    ÁÂÁÂ
    §Úµo²{  §Úªº°ÝÃD¬O()
    ArrList.Add (Test_1)
    ·|error

    ArrList.Add Test_1
    ´N¥i¥H¤F
    «D±`·PÁÂ

TOP

¦^´_ 4# stillfish00


   ¤£¦n·N«ä
   ¦A½Ð°Ý
   ªÅªºarray ©w¸q¤£¬ONothing¶Ü?

   Public ArrList(5) As address

Sub body()
    Dim Test1 As New address
    Dim Test2 As New address
   
    Test1.Name = "sophia"
    Test1.Num = 3
    Test1.Bool = True
        
    Test2.Name = "bill"
    Test2.Num = 5
    Test2.Bool = False
   
    Set ArrList(0) = Test1
    Set ArrList(1) = Test2

    If ArrList(3) = Nothing Then   '''³oÃä·|¨qerror  "Invalid use of object"
        Dim Test_Empty As New address
        Set ArrList(3) = Test_Empty
    End If

End Sub

¬O¹L§PÂ_ "" , Empty ³£¤£¦æ

TOP

¦^´_ 6# solitary0720
If ArrList(3) is Nothing Then

TOP

        ÀR«ä¦Û¦b : ¨C¤ÑµL©Ò¨Æ¨Æ¡A¬O¤H¥Íªº®ø¶OªÌ¡A¿n·¥¡B¦³¥Î¤~¬O¤H¥Íªº³Ð³yªÌ¡C
ªð¦^¦Cªí ¤W¤@¥DÃD