Results 1 to 20 of 20

Thread: Using public type as input parameter for function

  1. #1

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Using public type as input parameter for function

    I have a public type which contains the coordinates of a point, say X,Y and Z as:
    Code:
    Public Type Point
       Coord(1 to 3) as single
    End Type
    These points must be used as parameters in a function which rotates the point in three axis like in:

    Code:
    Public Sub RotatePoints(Points As Integer, ByVal InPoint As Point)
    
    For i = 1 To Points
        RotatedPoint(i).Coord(1) = InPoint(i).Coord(1)*sin(Alfa)
        RotatedPoint(i).Coord(1) = InPoint(i).Coord(2)*cos(alfa)
        RotatedPoint(i).Coord(1) = InPoint(i).Coord(3)*tan(alfa)
     Next i
    
    End Sub
    VB6 has a problem with this which I only partly understand:

    Only public user defined types defined in public object modules can be used as parameters or return types for public procedures of class modules or as fields of public user defined types

    I have also tried to make the routine a function like in:
    Code:
    Public Function RotatedPoints(Points As Integer, ByVal InPoint As Point) as Point
    but it has the same problem.

    I have an idea it confuses public types with class types and I do not know how to handle it.

    Thanks
    PK

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

    Re: Using public type as input parameter for function

    Going to assume that sub is in a class or form? If so, declare it as Friend vs. Public.

    There are other ways to skin that cat if needed.
    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
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Using public type as input parameter for function

    Where is your user type declared?

    is it in a class, or a module, or is it external to everything?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Using public type as input parameter for function

    LaVolpe,

    You really skinned that cat quickly. Thank you so much.

    PK

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

    Re: Using public type as input parameter for function

    You're welcome. Basically what this is saying is that UDT (user-defined types) cannot be
    - a parameter in a public method
    - can't be a return type from a public function
    The above restrictions are for non-bas modules, aka class modules (forms, class, uc, etc). You shouldn't get that error if the sub/function exists in a bas-module.

    The first part of that error allows for UDTs defined public in a class. But as a public declaration, they are only allowed in classes that have their instancing property set to global namespace; applicable to ActiveX projects.
    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

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Using public type as input parameter for function

    I understand it well now - many thanks.
    I graduated in 1969 and had a friend who had the 1967 model.
    PK

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

    Re: Using public type as input parameter for function

    My first car that I bought as a teenager after saving McDonalds paychecks for nearly a year was a 1969 Fastback 302 stock. It cost me $700 in 1977. 40 years later, I've put in a bit over $20K (including purchasing) into this one and it's still not done. May never get it done -- interior still needs restoring & over the last year, a little rust started to show. However, it drives just fine and that's the most important thing
    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}

  8. #8

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Using public type as input parameter for function

    As an extension of this, I am now trying to marry two arrays:

    This is my function:

    Code:
    Friend Function GetEndActions(Loadcase As Integer, w1 As Single, w2 As Single, a As Single, b As Single, L As Single, Axis As String) As Single()
    But this call does not work:

    Code:
                FEA() = GetEndActions(Rs!LoadType, w1, w2, a, b, MemLengthY, Rs!LoadAxis)
    Where FEA and the function have the same dimensions.

    Thanks
    PK

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

    Re: Using public type as input parameter for function

    FEA must be declared as single, and be a dynamic array, not a static array. Other than that, you should tell us what isn't working; what error you are getting.
    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}

  10. #10

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Using public type as input parameter for function

    Quote Originally Posted by LaVolpe View Post
    FEA must be declared as single, and be a dynamic array, not a static array. Other than that, you should tell us what isn't working; what error you are getting.
    That was the error thanks.
    I thought I could declare it as variant.

    Thanks
    PK

  11. #11

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Using public type as input parameter for function

    I am back with this problem, after LaVolpe's advice worked so well for long. Fortunately he also said there are other ways to skin this cat.

    I have this sub:

    Code:
    Friend Sub PlotAnyArrow(Firstpoint As Point3D, SecondPoint As Point3D, Optional Vectype As String, Optional VecIndex As Integer)
    'rotate points
    Firstpoint = RotatedPoint(Firstpoint)
    SecondPoint = RotatedPoint(SecondPoint)
    In gives me an error of type mismatch when it has to rotate FirstPoint in the first code line - and probably Second point as well when I get to it.

    The sending Sub has FirstPoint and Second Point defined as Point3D.

    The rotating function has this:
    Code:
    Friend Function RotatedPoint(InPoint As Point3D) As Point3D
    There are many things I do not understand here.
    Firstly, why does it give me the mismatch error, as I have FirstPoint defined as Point3D in all three routines
    Secondly, I really want to send four point to the first mentioned sub, but sometimes I only have two points. I have tried to sabotage the sub by sending the firstpoint and secondpoint twice, like in Friend Sub PlotAnyArrow (FirstPoint as Point3D,SecondPoint as point3D,FirstPoint as point3D,SecondPoint as point3D ...), when I have only two points, but it did not fall for that one. Can I do this?
    Thirdly, I have tried to use optional for the next two points, but it did not fall for that either.
    Fourthly, I have tried to send the points as objects, but that did not go well either.

    I have now run out of ideas.

    Thanks for your help.

    PK

  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Using public type as input parameter for function

    Show the Line where you declare/dim FirstPoint as you have it in your code. Don't "beautify" it.
    I have a suspicion.....
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  13. #13

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Using public type as input parameter for function

    Quote Originally Posted by Zvoni View Post
    Show the Line where you declare/dim FirstPoint as you have it in your code. Don't "beautify" it.
    I have a suspicion.....
    Here is the sending routine:

    Code:
    Public Sub PlotNodeRestraints()
    Dim i As Integer, j As Integer, MyFillColour As Long, MyForeColour As Long, MyArrows As Integer, Xtail As Single, Ytail As Single, Xhead As Single, Yhead As Single, HeadRadius As Single, PlotAngle As Single, CircleRad As Single, PlotPoint(1 To 2) As Point3D
    Dim S$, rs As Recordset, VectorValue(1 To 3) As Integer, AngleData() As Single, BeginAngle As Single, EndAngle As Single, ArrowAngle As Single, Dx As Single, Dy As Single, Dz As Single, PlotLength As Single, PlotVector As Vector3D
    Dim BeginPoint As Point3D, EndPoint As Point3D
    
    Exit Sub
    
    End Sub
    Thanks
    PK
    Last edited by Peekay; Sep 23rd, 2020 at 05:39 AM.

  14. #14
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Using public type as input parameter for function

    Hmm, OK.
    I don't see anything wrong here.
    My suspicion was you declare it with other variables in one line (which is true), but ommiting the VarType for all except the last.
    In that case only the last one would be of the declared type, the others would be Variants (It's the reason why i always declare all variables each in its own line)

    As for your TypeMismatch: Have you stepped through your code? It doesn't have to be your Rotate-Function that throws the error, but something else deeper inside that function.
    Check your variables in the Watch-/Immediate-Window
    IIRC, the debugger jumps back to the codeline which initially started the whole thing.

    As for your "Optional four points vs. two points" --> why not initialise a separate Point-Variable to some bogus-Data, which can never be achieved by your code, and you pass that one as third and fourth point?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  15. #15

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Using public type as input parameter for function

    Thank you Zvoni,

    I have solved the problem.
    I have to declare all the Point3D points as public variables in a module, and I may not use Dim in routines for Point3d points. I cannot work with points declared as public and those declared with dim in the same code line
    I did not know that.

    OK

  16. #16
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Using public type as input parameter for function

    You should be able to do this.
    But be aware that you don't have multiple definitions of the UDT.
    Maybe you have a public one in a module, but also (an old) declared the UDT in a form.

  17. #17

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Using public type as input parameter for function

    Quote Originally Posted by Arnoutdv View Post
    You should be able to do this.
    I agree that I should be able to mix the two types of declaration, but somehow there are conditions that I do not adhere to. Changing all these points to public UDT's has solved the problem for the interim.

    PK

  18. #18
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Using public type as input parameter for function

    What you can do:
    Declare variables in a module and use them.
    Declare variables in a sub an use them.
    Declare variables in a module and sub and use them.

    What you CAN'T do:
    Declare variables in one sub and use them in a completely different sub.
    It has to do with scoping. When you dim a variable in a sub, it is scoped to ONLY that sub.

    Variables defined in a module are different becaue they can be Public, Friend, or Private.
    Private is like Dim in a sub - it's only visible to that module.
    Public makes it publicly available to everything
    Friend is somewhere in between

    It sounds like you wanted to dim your variables all in PlotNodeRestraints and use them elsewhere... that doesn't work. That's not how this works. You'd need to define them at least as Friend or Public in a module to do that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  19. #19

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Using public type as input parameter for function

    Thanks techgnome,

    I know that mostly, but obviously I make errors and I can find all errors in someone else's code except my own. So, I just need some outside perspective to clear my brain.

    PK

  20. #20
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Using public type as input parameter for function

    In gives me an error of type mismatch when it has to rotate FirstPoint in the first code line - and probably Second point as well when I get to it.
    I really suspect something like this:

    Code:
    ' In a module
    Public Type tpPoint3D
      X As Double
      Y As Double
      Z As Double
    End Type
    
    Public Sub MyPoint3DRoutine(tPoint3D As tpPoint3D)
    '
    End Sub
    Code:
    ' In a Form
    Private Type tpPoint3D
      X As Double
      Y As Double
      Z As Double
    End Type
    
    Private Sub Command1_Click()
      Dim tPoint3D As tpPoint3D
    
      MyPoint3DRoutine tPoint3D  '<-- This will cause a type mismatch
    End Sub

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