ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Class programming - how to return chartobject from a class? (https://www.excelbanter.com/excel-programming/374862-class-programming-how-return-chartobject-class.html)

[email protected]

Class programming - how to return chartobject from a class?
 
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


RaceEend

Class programming - how to return chartobject from a class?
 
wrote:

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



--


NickHK

Class programming - how to return chartobject from a class?
 
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

wrote in message
ups.com...
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




[email protected]

Class programming - how to return chartobject from a class?
 

Eend:

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

NickHK:

Terrific, that did the trick. Many thanks



All times are GMT +1. The time now is 01:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com