Results 1 to 17 of 17

Thread: Passing parameters between sub

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    71

    Passing parameters between sub

    Hi All, I have below code sub's and want to have values of colors (RGB) passed from iPartColor () to iColor(). However, I have values of Rcolor, Gcolor, and Bcolor = 0.

    How can I get values of Rcolor, Gcolor, and Bcolor inside iColor() same as values of R, G, B inside iPartColor().

    Any help is much appreciated.

    '//-------------------------------------

    Code:
    Option Explicit
    
    Private Sub iPartColor(ByVal R As Long, ByVal G As Long, ByVal B As Long)
    
    ' //// Define object selection
    Dim oSel As Selection
    Set oSel = CATIA.ActiveDocument.Selection
        
    oSel.Clear '//// Clear selection before any selection
        
    '//// Search and save all surfaces in active document in oSelection collection
    oSel.Search "Topology.Face.Color = *, All"
    
    Dim DefaultVisProperties '//// As VisProperty
    Set DefaultVisProperties = CATIA.ActiveDocument.Selection.visProperties
    
    '//// Get PartBody's default colors in RGB array.
    DefaultVisProperties.GetRealColor R, G, B
    
    MsgBox "Default part's RGB color array = " & "Red color: " & R & " ; Green color: " & G & " ; Blue color: " & B (this code works fine ....ie. 0, 128, 255)
    
    '//// Clear content of "oSel" 
    oSel.Clear
    
    End Sub
    
    '//---------------------------------
    
    Private Sub myColor()
    
    Dim Rcolor As Long, Gcolor As Long, Bcolor As Long
    
    Call iPartColor(Rcolor, Gcolor, Bcolor)
    
    MsgBox Rcolor '// result = 0 *****
    MsgBox Gcolor '// result = 0 *****
    MsgBox Bcolor '// result = 0 *****
    
    End Sub

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Passing parameters between sub

    Define iPartColor parameters BYREF not BYVAL
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Passing parameters between sub

    hahaha, yes, this is a fun one.

    Yes, NeedHelpVB, a Function will return a single value. However, a Sub (or a Function, for that matter) can actually return as many values as you like.

    By default, VB6 passes arguments (variables) in a way that's called "by reference" (or ByRef if you explicitly wish to specify this in the Function/Sub definition). When declared in this ByRef (default) way, if you change the values, those changes will be reflected back to whatever code called your Sub (or Function).

    However, optionally, you can pass variables as "by value" (or ByVal when defining). When you do this, a copy of the variable is made explicitly for the called Sub (or Function). Therefore, if that Sub (or Function) changes the values, those changes are not reflected back to the calling code.

    It's really that simple. We could get into discussions about whether the variable's address or the variable's actual data is pushed onto the memory stack, but those discussions are not at all needed to make use of this stuff.

    And your code didn't work the way you wanted because there are ByVal keywords on the variable definitions. As LaVolpe states, either change this keyword to ByRef, or just delete it (and let it default to ByRef).

    All The Best,
    Elroy

    EDIT1: Also, if you want other variables passed back to you, just create some "spare" variables in the procedure that's going to make the call, and pass them to the Sub (or Function). Yet another way is to make a UDT, but that's a bit higher-level.
    Last edited by Elroy; Apr 17th, 2017 at 06:05 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Passing parameters between sub

    Hmmmm....if iParColor is called from myColor() (which is called from somewhere else, and Rcolor, Gcolor, and BColor as dimmed in myColor(), they equal zero. Hence, when iPartColor is called, wouldn't zeroes be passed anyway?????

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Passing parameters between sub

    Quote Originally Posted by SamOscarBrown View Post
    Hmmmm....Hence, when iPartColor is called, wouldn't zeroes be passed anyway?????
    Don't think so in this case, it appears R,G,B are being set in the call: DefaultVisProperties.GetRealColor R, G, B. In other words R,G,B are being passed to iPartColor to be filled in, assigned and not as input into the iPartColor function.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Passing parameters between sub

    @NeedHelpVB: I'm curious as to which language you are actually using. You've got a few threads over in .NET, and here in classic. All the replies to this thread are true for either one, and the code shown would work in either one, but .NET uses ByVal as a default, whereas classic VB uses ByRef as a default.

    The languages are very similar, but not the same. So far, it doesn't matter for the threads you've started, but it seems unlikely that you would be simultaneously learning VB6 and VB.NET, which is why I'm asking which language you are actually working on?
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    71

    Re: Passing parameters between sub

    I am learning both vb6 and VB .NET. vb6 is partially use at my work and I do not want to learn and use it, but I am forced to use it.
    VB .NET is what I start trying to learn it.

    From my first post, both ByVal or ByRef do not pass parameters from iPartColor to myColor. I tested it in VB . NET....I am stuck at this point.

    Any help is much appreciated.

  8. #8
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Passing parameters between sub

    explicitly using ByRef works in both vb6 and .NET


    Code:
    Private Sub iPartColor(ByRef R As Long, ByRef G As Long, ByRef B As Long)

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Passing parameters between sub

    Yes byRef is the way to go. If you tried byRef and could not get it to work then show us the code you used and maybe we can help.

    The code you posted uses byVal so in effect event though the variables have the same name they are completely different variables and will contain different values.
    In this case they will all be 0 in the one sub because they are local to that sub and are never assigned a value.

    If you use byRef even if the variable name is different the value will still be updated as by ref tells it to reference the memory location where the variable being passed is stored.

    Consider the following simple example of the three options in VB6
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim X As Integer
        X = 0
        MySub1 X
        Debug.Print "Default = " & X
        X = 0
        MySub2 X
        Debug.Print "byRef = " & X
        X = 0
        MySub3 X
        Debug.Print "byVal = " & X
    End Sub
    Private Sub MySub1(myVar As Integer)
        myVar = 27
    End Sub
    Private Sub MySub2(ByRef myVar As Integer)
        myVar = 27
    End Sub
    Private Sub MySub3(ByVal myVar As Integer)
        myVar = 27
    End Sub
    Sub1 sets x=27 since the default in VB6 is byRef
    Sub2 sets x=27 because it uses byRef
    sub3 does not change the value of x because it uses byVal so x=0

    VB.Net would give 0 on sub 1 because it defaults to byVal and of course would return 27 on sub 2 and 0 on sub3

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    71

    Re: Passing parameters between sub

    Thanks for all suggestions and help. I will review materials on "sub/function and passing parameters" again, then try to rewrite my code.
    I f I still have issues, I will let you know.

    Thanks again

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Passing parameters between sub

    I thought we had established that this is embedded VBA in the CATIA product and not VB6.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    71

    Re: Passing parameters between sub

    you are right....VBA embedded in CATIA...not actual VB6. May be that is the reason why passing parameters do not work.....

  13. #13
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Passing parameters between sub

    Well, if CATIA is not passing your parameters byref, in this specific case, you should still be able to get the colors by making the sub a function. The changes would look like this... blue/bold are changes. Assumption are that the individual R,G,B values are within the range of 0-255
    Code:
    Private Function iPartColor(ByVal R As Long, ByVal G As Long, ByVal B As Long) As Long
    Code:
    DefaultVisProperties.GetRealColor R, G, B
    iPartColor = RGB(R, G, B)
    Edited: ^^ If RGB() not recognized by that app, then try this:
    iPartColor = R Or G * &H100& Or B * &H10000
    Code:
    Dim lColor As Long ' add to your routine that calls iPartColor
    lColor = iPartColor(Rcolor, Gcolor, Bcolor)
    Rcolor = lColor And &HFF
    Gcolor = (lColor And &HFF00&) \ &H100&
    Bcolor = lColor \ &H10000
    Last edited by LaVolpe; Apr 19th, 2017 at 03:25 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    71

    Re: Passing parameters between sub

    Thanks LaVolpe and Others...I will try your suggestion and let all of you know soon.

    Have a great day.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    71

    Re: Passing parameters between sub

    I rewrote the code using ByRef for passing parameters and it works. However, passing parameters only work between sub's inside the same module.

    If I try the place the sub's with parameters in different modules, those sub do not even show up in debugging window to debug.

    See example below. If I put ByRef declaration inside the brackets, the sub DefaultPartColor does not show up in the debug window to debug. If I remove those parameters, then the sub DefaultPartColor shows up. I do not know why. By the way, I write this sub in VBA inside CATIA, not in VB6.

    Code:
    Option Explicit
    
    Sub DefaultPartColor(ByRef MyRcolor As Long, ByRef MyGcolor As Long, ByRef MyBcolor As Long)
    
    ' //// Define object selection
    Dim oSel As Selection
    Set oSel = CATIA.ActiveDocument.Selection
        
    oSel.Clear '//// Clear selection before any selection
        
    '//// Search and save all surfaces in active document in oSelection collection
    oSel.Search "Topology.Face.Color = *, All"
    
    Dim DefaultVisProperties '//// As VisProperty
    Set DefaultVisProperties = CATIA.ActiveDocument.Selection.visProperties
    
    '//// Get PartBody's default colors in RGB array.
    DefaultVisProperties.GetRealColor R, G, B
    
    MsgBox "Default part's RGB color array = " & "Red color: " & MyRcolor & " ; Green color: " & MyGcolor & " ; Blue color: " & MyBcolor
    
    End Sub
    Any idea or suggestions?
    Last edited by NeedHelpVB; Apr 20th, 2017 at 02:30 PM.

  16. #16
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Passing parameters between sub

    Maybe try putting a "Public" before your sub declaration? As follows...

    Code:
    
    Public Sub DefaultPartColor(ByRef MyRcolor As Long, ByRef MyGcolor As Long, ByRef MyBcolor As Long)
    
    
    EDIT1: Also, are these modules what would be called a "Module" (not a "Class Module" and not a "Form) in the CATIA VBA?

    EDIT2: Also, I'm just thinking, does CATIA show you "Objects" in the VBA's project window? Are you putting this code in one of those modules? If so, that's going to be an instantiated object, and not a regular (static) Module.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    71

    Re: Passing parameters between sub

    I have tried Public, Private and none of them works.

    These codes are written in plain code editor, not a "Class form or Form" in VBA.

    Yes, VBA in CATIA show Objects, which can be browsed, and those objects are also displayed with "intellisen"...(may be my spelling is incorrect, but I am sure you know what I mean).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width