Class programming - how to return chartobject from a class?

F

fredrik.kjell

Hi,

I have the following setup:

1. Class module "ChartHandler"
2. Module "Tester"

In ChartHandler, I have a function to return an instance of a
ChartObject. The chart is present in a worksheet in my workbook. I
successfully claim the chart with the following code:

Public Function claimChartObject(ByVal s As String) as ChartObject

Debug.Print ws.Name ' prints worksheet name correctly
Debug.Print ws.ChartObjects(s).Name ' prints the name correctly

claimChartObject = ws.ChartObjects(s)

End Function

The above function works as it should. However, when I try to get the
chartobject from my module, it fails. The code in my module is
something like this:

Dim coChart As ChartObject
Dim handler As ChartHandler ' Creates an instance of ChartHandler -
works

' Some initialization...
' ....

Set coChart = handler.claimChartObject("myChart") ' <-- this fails!

If I try to snatch the chartobject with the following line, it works:
Set coChart = Worksheets("chart").ChartObjects("myChart")

So my question is, how to I return the chartobject from the class?? I
tried changing "Public Function" to "Property Get" but without success.

Any help much appreciated!

Fredrik
 
R

RaceEend

Hi,

I have the following setup:

1. Class module "ChartHandler"
2. Module "Tester"

In ChartHandler, I have a function to return an instance of a
ChartObject. The chart is present in a worksheet in my workbook. I
successfully claim the chart with the following code:

Public Function claimChartObject(ByVal s As String) as ChartObject

Debug.Print ws.Name ' prints worksheet name correctly
Debug.Print ws.ChartObjects(s).Name ' prints the name correctly

claimChartObject = ws.ChartObjects(s)

End Function

The above function works as it should. However, when I try to get the
chartobject from my module, it fails. The code in my module is
something like this:

Dim coChart As ChartObject
Dim handler As ChartHandler ' Creates an instance of ChartHandler -
works

' Some initialization...
' ....

Set coChart = handler.claimChartObject("myChart") ' <-- this fails!

If I try to snatch the chartobject with the following line, it works:
Set coChart = Worksheets("chart").ChartObjects("myChart")

So my question is, how to I return the chartobject from the class?? I
tried changing "Public Function" to "Property Get" but without
success.

Any help much appreciated!

Fredrik

You must make an instance of the class
Dim handler As New ChartHandler

or
Dim Handler as ChartHandler
Set Handler = New ChartHandler



--
 
N

NickHK

Fredrik,
Because you are returning an object, you need the Set keyword.

Public Function claimChartObject(ByVal s As String) as ChartObject
Set claimChartObject = ws.ChartObjects(s)
End function

NickHK
 
F

fredrik.kjell

Eend:

Thanks, aldready done that but I excluded that part in my code-snippet

NickHK:

Terrific, that did the trick. Many thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top