Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 2010 VBA - Adding a class instances to a collection 1

Status
Not open for further replies.

JasonEnsor

Programmer
Sep 14, 2010
193
0
0
GB
Hi Guys,

I am trying to write some code to UnitTest some of my procedures in VBA and was looking for some suggestions on how best to improve my concept.

Currently I have a UnitTest class that takes in a TestName,ExpectedResults,ActualResults and TestDescription, I am then adding these to a UnitTests Collection so that I can then output them to a text document.

The code is in a very rough state at the moment but what I have so far is
Code:
Global UnitTests As Collection

Public Sub testing()
    Dim clss As New Class1
    Set cls = New UnitTest
    cls.TestName = "IsValidWorkbook"
    cls.TestActualResult = IIf(clss.IsWorkbookValid(ThisWorkbook, "Sheet1"), "Pass", "Fail")
    cls.TestDescription = "1st"
    UnitTests.Add cls
    
    Set cls = New UnitTest
    cls.TestName = "IsValidWorkbook"
    cls.TestActualResult = IIf(clss.IsWorkbookValid(ThisWorkbook, "Sheet1"), "Pass", "Fail")
    cls.TestDescription = "2st"
    UnitTests.Add cls

    Call DisplayResults
End Sub

I am only using the Description field at the moment so the tests are duplicated apart from that value. Am I right in having to create a new instance of UnitTest each time I want to add a UnitTest, I thought I could add the UnitTest to the collection then update the values and add that to the collection. however that doesn't seem to work.

The code above works but it doesn't seem very clean to me.

Am I missing something?

Regards

J.


Regards

J.
 
>Am I right in having to create a new instance of UnitTest

Yep.

Consider that cls is just a pointer to the actual object. And you are just adding that pointer to your collection. if you don't create a new instance of the object each time then all the pointers in the collection will point to the same object.
 
Thanks for confirming that strongm, it is much appreciated :)

Regards

J.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top