Calling a sub and pass in parent object

Last post 07-07-2009 9:09 AM by atconway. 3 replies.

Sort Posts:

  • Calling a sub and pass in parent object

    07-06-2009, 10:30 AM
    • Member
      30 point Member
    • ryanlcs
    • Member since 05-17-2007, 1:35 PM
    • Posts 238

    hi..

    I have a statement that will call a Sub.

    Charts(CurChartNo) = Server.CreateObject("ChartFX.WebServer")
    Call InitChart()


    Inside this sub,  I will assign values to the properties of the Charts object that is

    Public Sub InitChart()
            Charts(CurChartNo).RGB2Dbk = &HC0FFFF&
            Charts(CurChartNo).Chart3D = False
            Charts(CurChartNo).MultipleColors = True
            Charts(CurChartNo).Charttype = 1
            Charts(CurChartNo).Border = True
            Charts(CurChartNo).AllowResize = False
            Charts(CurChartNo).AllowDrag = False
            Charts(CurChartNo).SerLegBox = True
            Charts(CurChartNo).Scrollable = True
            Charts(CurChartNo).MarkerSize = 3
        End Sub

    But the Charts is not recognized in the Sub. This is current program under ASP, I am converting to ASP.Net.

    Please advice.

    Thank You.

  • Re: Calling a sub and pass in parent object

    07-06-2009, 4:51 PM
    Answer
    • Contributor
      5,664 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,186

    Based on the object type you are using and the code you have, you will need to add a reference parameter of type object like so:

     Public Sub InitChart(ByRef Charts As Object)

    ...then when calling your Sub() (with your code) you would call it like this:

    InitChart(Charts)

    The other way to do this is to turn your Sub() into a function that takes the chart number parameter, but returns you a new object from the function directly like so:

    Public Function InitChart(ByVal CurChartNo As Integer) As Object

    'Check this object instantiation to make sure it is the way you are currently creating the object

    Dim Charts As New Object
    Charts(CurChartNo) = Server.CreateObject("ChartFX.WebServer") 
     
            Charts(CurChartNo).RGB2Dbk = &HC0FFFF&  
            Charts(CurChartNo).Chart3D = False 
            Charts(CurChartNo).MultipleColors = True 
            Charts(CurChartNo).Charttype = 1  
            Charts(CurChartNo).Border = True 
            Charts(CurChartNo).AllowResize = False 
            Charts(CurChartNo).AllowDrag = False 
            Charts(CurChartNo).SerLegBox = True 
            Charts(CurChartNo).Scrollable = True 
            Charts(CurChartNo).MarkerSize = 3  

    Return Charts

        End Sub 

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: Calling a sub and pass in parent object

    07-07-2009, 2:20 AM
    • Member
      30 point Member
    • ryanlcs
    • Member since 05-17-2007, 1:35 PM
    • Posts 238

     Another way is to define the objects as Public.

    Thanks.

  • Re: Calling a sub and pass in parent object

    07-07-2009, 9:09 AM
    • Contributor
      5,664 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,186

    I do not recommend making your objects public; that is typically a beginners methodology to make everything public because they do not understand the importance of object scope and why keeping the scope of the object minimal in important.  The original inclination to pass the object to the Sub was a good idea.

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

Page 1 of 1 (4 items)