View Full Version : Initiating a new instance of a class object
Geetha Gupta
02-01-2017, 11:29 AM
Hi all,
I have the following code in a workbook, different from the one in which the class module is defined, with Instancing property as PublicNotCreatable
Sub GetInfo()
Dim cPrMine As cprofit
Set cPrMine = GetNewClsProfit
.
.
.
end sub
Public Function GetNewClsProfit() As cprofit
'for making a new copy of the class cprofit,
Set GetNewClsProfit = New cprofit ' the macro gets stuck here
End Function
an error message says "Invalid use of new keyword"
can someone make out what problem is?
Thanks in advance,
Regards
Geetha
Paul_Hossler
02-01-2017, 06:01 PM
http://www.cpearson.com/excel/classes.aspx
The Instancing Property Of A Class
The Instancing property of a class controls where that class may be used. The default value is Private, which means that the class can be used only in the project in which the class is defined. You can set the instancing property to PublicNotCreatable, which allows a variable to be declared as that class type in projects that have a reference to the project containing the class. The second class may declare a variable of the class type, but cannot create an instance of the class with the New keyword. See the next section for more details.
Using Classes In Multiple Projects
If the Instancing property of the class is PublicNotCreatable a variable of that class type may be declared in other projects, but cannot be created in that project. You can use a function in the project containing the class to return a new instance to the caller. First, change the name of the project containing the class from the default value of VBProject to something meaningful like projSourceProject. Then, in the class that will use the class, set a reference to projSourceProject. Back in the project containing the class, create a procedure that will create and return a new instance of the class:
Public Function GetClass() As CEmployee
Set GetClass = New CEmployee
End Function
Then call this function in the project that will use the class:
Dim NewEmp As projSourceProject.CEmployee
Set NewEmp = projSourceProject.GetClass()
So maybe if you created a reference to the class-holding workbook it would work
mikerickson
02-01-2017, 09:32 PM
You don't need a function to do that.
You can do it in the calling routine, but you would have to set the initial properties in the ' ... section.
Sub getInfo()
Dim cPrMine As cprofit
Set cPrMine = New cProfit
'....
End With
If you want to return a new instance from a function, I would use code like this. (Lets pretend that cProfit has a .Name property)
Sub Test()
Dim myProfit as cProfit
Set myProfit = GetNewCProfit("Mine")
' ...
End Sub
Function GetNewCProfit(newName As String) As cProfit
Dim Result as cProfit
Set Result = New cProfit
Result.Name = newName
Set GetNewCProfit = Result
Set Result = Nothing
End Function
Hi all,
I have the following code in a workbook, different from the one in which the class module is defined, with Instancing property as PublicNotCreatable
I might not be understanding, but if you are saying that you have this code in the workbook that wishes to access/borrow (let us call this wb the Beneficiary) another workbook's (we'll call Provider) class module, then, Function GetNewCProfit(newName As String) needs to be in the Provider workbook (with the Class).
Then getInfo() needs to be in the Beneficiary wb, and how it calls GetNewCProfit() depends on whether you set a reference to Provider's library as Paul mentions, or use late-binding and Application.Run to set a reference.
Hope that helps,
Mark
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.