Casting variables

Harvey

Well-known Member
Joined
Nov 18, 2004
Messages
953
Simple question, but yet I found no answer.

I have a function that returns an object. The object is of the class clsCompany, but it is returned as Object. I can access all the values and functions in it, but I would like to store it in a clsCompany variable again. How can I do that?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Does this help???
<hr /><font face=Courier New><SPAN style="color:#007F00">' *** standard module ***</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> TestSub()
    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> clsTest, cc <SPAN style="color:#00007F">As</SPAN> clsTest
    <SPAN style="color:#00007F">Dim</SPAN> o <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> c = <SPAN style="color:#00007F">New</SPAN> clsTest
    
    c.Message = "mensaje"
    c.Valor = 22
    
    <SPAN style="color:#00007F">Set</SPAN> cc = TestFunc(c)
    <SPAN style="color:#00007F">Set</SPAN> o = c
    
    MsgBox cc.Message & vbCr & vbCr & cc.Valor, _
           vbInformation, "cc is of type " & TypeName(cc)
    
    MsgBox o.Message & vbCr & vbCr & o.Valor, _
           vbInformation, "o is of type " & TypeName(o)
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Function</SPAN> TestFunc(<SPAN style="color:#00007F">ByVal</SPAN> itemIn <SPAN style="color:#00007F">As</SPAN> clsTest) <SPAN style="color:#00007F">As</SPAN> clsTest
<SPAN style="color:#007F00">' note that itemIn is passed ByRef in spite of the BYVAL in the args list</SPAN>
    itemIn.Message = "gibberish"
    itemIn.Valor = itemIn.Valor * 2
    <SPAN style="color:#00007F">Set</SPAN> TestFunc = itemIn
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">' *** end of standard module ***</SPAN>
</FONT><hr /><font face=Courier New><SPAN style="color:#007F00">' *** clsTest class module ***</SPAN>

<SPAN style="color:#00007F">Private</SPAN> m_strTest <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, m_intTest <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>

<SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Let</SPAN> Message(strIn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)
    m_strTest = strIn
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN>

<SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Get</SPAN> Message() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    Message = m_strTest
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN>

<SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Let</SPAN> Valor(intIn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>)
    m_intTest = intIn
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN>

<SPAN style="color:#00007F">Property</SPAN> <SPAN style="color:#00007F">Get</SPAN> Valor() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    Valor = m_intTest
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Property</SPAN>

<SPAN style="color:#007F00">' *** end of clsTest class module ***</SPAN>
</FONT><hr />If not, perhaps you could post some portions of your code to aid in understanding your situation better.
 
Upvote 0
hmm, It looks promising, but I am not sure if it will help me convert the variable to a clsCompany again.
Unfortunately I do not have my code here right now, but it looks like this:

Code:
function getCompanyDetails(companyID as Integer) as Object

dim myCompany as clsCompany
'a lot of actions

end function

set getCompanyDetails = myCompany


function test()

dim o as Object
dim t as clsCompany

set o = getCompanyDetails(1)

'how to put o into t?

end function

So, the code says it all. I need the object to be casted as a clsCompany. It should be possible but how to do it? Thanks for the replies so far.
 
Upvote 0
Harvey,

Take a closer look at the code I posted. Notice that TestFunc specifies that its return type is clsTest? Sounds to me like that's all you need to do. NB! In the example code you posted you are assigning the return value (Set getCompanyDetails = myCompany) after your END FUNCTION statement. I've never had very good luck getting the compiler to accept that sort of thing! :wink:

Regards,
 
Upvote 0
A further thought... If you don't have the code in front of you, it my be difficult to answer. But how is it that you have a function that is acting upon a custom class? By and large functions that deal with custom classes are coded inside the class module as a method member of said custom class. (I hope my convoluted English makes sense!)
 
Upvote 0
Hi. Sorry for not having replied for a while. I am using two workbooks,
one containing an address book with company addresses, and a form to display and edit them.
The other workbook calls to the form to select one company, and then return the company data to the workbook.
The function that returns the object (of type clsCmpany) is located in workbook A, and the function that receives the data is in workbook B.

I figured I cannot return a class instance in a function, that is why I had to return it as an object. Now the only thing I have to do is cast it as clsCompany again.
Greg thanks for the code you posted, but it does not cover the problem. What I need is a function like the TestFunc, but with argument object itemIn instead of clsTest. I hope this story makes sense too :wink:
 
Upvote 0
You can do that. For example...

<font face=Courier New><SPAN style="color:#007F00">' *** standard module ***</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> TestSub()
    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> clsTest, cc <SPAN style="color:#00007F">As</SPAN> clsTest
    <SPAN style="color:#00007F">Dim</SPAN> o <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> c = <SPAN style="color:#00007F">New</SPAN> clsTest
    
    c.Message = "mensaje"
    c.Valor = 22
    
    <SPAN style="color:#007F00">'Set cc = TestFunc(c)</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> cc = TestFuncObjIn(c)
    Set o = c
    
    MsgBox cc.Message & vbCr & vbCr & cc.Valor, _
           vbInformation, "cc is of type " & TypeName(cc)
    
    MsgBox o.Message & vbCr & vbCr & o.Valor, _
           vbInformation, "o is of type " & TypeName(o)
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#007F00">'Function TestFunc(ByVal itemIn As clsTest) <SPAN style="color:#00007F">As</SPAN> clsTest</SPAN>
<SPAN style="color:#007F00">'' note that itemIn is passed ByRef in spite of the BYVAL in the args list</SPAN>
<SPAN style="color:#007F00">'    itemIn.Message = "gibberish"</SPAN>
<SPAN style="color:#007F00">'    itemIn.Valor = itemIn.Valor * 2</SPAN>
<SPAN style="color:#007F00">'    <SPAN style="color:#00007F">Set</SPAN> TestFunc = itemIn</SPAN>
<SPAN style="color:#007F00">'End Function</SPAN>

<SPAN style="color:#00007F">Function</SPAN> TestFuncObjIn(itemIn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>) As clsTest
    itemIn.Message = "feefiefoefum"
    itemIn.Valor = itemIn.Valor * 20
    Set TestFuncObjIn = itemIn
<SPAN style="color:#00007F">End</SPAN> Function

<SPAN style="color:#007F00">' *** end of standard module ***</SPAN>

</FONT>

BUT should you? That's my question. I am still not understanding how GetCompanyDetails is in a standard module and not some type of method inside the class module itself. It may well be that this is correct and appropriate, I don't know enough to render an opinion. At this point all I can say is that it seems odd.
 
Upvote 0
The function TestFuncObjIn looks like you can put an onbject in, and return a clsTest, but it doesn't work for me. The code I have in my project is the following:

Code:
Sub xx()

    Dim temp As Object
    Dim contactgegevens As clsContactgegevens
    
    Workbooks.Open ("E:\Public\Offerte traject\offerte 2.0\adressen.xls")
    
    Set temp = Workbooks("adressen.xls").invoerenContactGegevens
    
    Workbooks("offerte 2.0.xls").Sheets("Contactgegevens").Activate
    
    Set contactgegevens = test(temp)
'    Debug.Print ((clsContactgegevens)temp).m_Project.Nr
    
End Sub

Private Function test(ByVal par1 As Object) As clsContactgegevens

    Set test = par1

End Function


The function invoerenContactgegevens returns an object that represents a clsContactgegevens instance, but when I try to cast it back, the error I get is "Types are not the same" (translated from dutch)

Am I doing something wrong or is there another way to work around this?
 
Upvote 0
Hi Harvey,

I am having the same trouble. Excel is declaring a “type mismatch” on me too; even when it appears that it should not do so. Perhaps you could post the code for invoerenContactGegevens and we could see if there might be a way to move that functionality into the class module itself.<ul>[*][Code for clsTest class module same as in previous post.][/list]<font face=Courier New><SPAN style="color:#007F00">' *** standard module in book1 ***</SPAN>
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> testsubin1()
    MsgBox "Hello from 1"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Function</SPAN> OtherTest() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    OtherTest = "Text"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Function</SPAN> ReturnTestClass() <SPAN style="color:#00007F">As</SPAN> clsTest
    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> clsTest
    <SPAN style="color:#00007F">Set</SPAN> c = <SPAN style="color:#00007F">New</SPAN> clsTest
    <SPAN style="color:#00007F">With</SPAN> c
        .Message = ActiveWorkbook.ActiveSheet.Range("A1")
        .Valor = ActiveWorkbook.ActiveSheet.Range("B1")
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> ReturnTestClass = c
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">' *** end of standard module in book1 ***</SPAN>
</FONT>

<font face=Courier New><SPAN style="color:#007F00">' *** standard module book 2 ***</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> TestSub()
    
    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> clsTest, cc <SPAN style="color:#00007F">As</SPAN> clsTest
    <SPAN style="color:#00007F">Dim</SPAN> o <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    
    b = Application.Run("CustomClassExperimentBook1.xls!othertest")
    
    <SPAN style="color:#00007F">Set</SPAN> c = <SPAN style="color:#00007F">New</SPAN> clsTest
    <SPAN style="color:#00007F">Set</SPAN> cc = <SPAN style="color:#00007F">New</SPAN> clsTest
    c.Message = "lower"
    c.Valor = 38
    <SPAN style="color:#00007F">Set</SPAN> o = DoubleUpper(c)
    Debug.Print c.Message, c.Valor
    
    <SPAN style="color:#007F00">' cannot set CC - get TYPE MISMATCH error</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> cc = Application.Run("CustomClassExperimentBook1.xls!returntestclass")
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0
    <SPAN style="color:#00007F">Set</SPAN> o = Application.Run("CustomClassExperimentBook1.xls!returntestclass")
    <SPAN style="color:#007F00">' still cannot set CC - get TYPE MISMATCH error</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> cc = o
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Function</SPAN> DoubleUpper(<SPAN style="color:#00007F">ByRef</SPAN> itemIn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>) <SPAN style="color:#00007F">As</SPAN> clsTest

    itemIn.Message = UCase(itemIn.Message)
    itemIn.Valor = itemIn.Valor * 2
    <SPAN style="color:#00007F">Set</SPAN> DoubleUpper = itemIn
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">' *** end of standard module in book 2***</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top