Results 1 to 10 of 10

Thread: [RESOLVED] Problem with Property Let/Get in Class module

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [RESOLVED] Problem with Property Let/Get in Class module

    I have this in a Class

    Code:
    Private cls_argcm() As tagCM
    Private cls_arghashkRep() As Long
      '
      '
      '
    Public Property Get arghashkRep() As Long()
     arghashkRep = cls_arghashkRep()
    End Property
    Public Property Let arghashkRep(newValue() As Long)
     cls_arghashkRep() = newValue()
    End Property
    
    Public Property Get argcm() As tagCM()
     argcm = cls_argcm()
    End Property
    Public Property Let argcm(newValue() As tagCM)
     cls_argcm() = newValue()
    End Property
      '
      '
    This works:

    Code:
    Public Property Get arghashkRep() As Long()
     arghashkRep = cls_arghashkRep()
    End Property
    Public Property Let arghashkRep(newValue() As Long)
     cls_arghashkRep() = newValue()
    End Property
    This does not work:

    Code:
    Public Property Get argcm() As tagCM()
     argcm = cls_argcm()
    End Property
    Public Property Let argcm(newValue() As tagCM)
     cls_argcm() = newValue()
    End Property
    Because the data type is tagCM (at least that's what I think) I get following error:

    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 defined types

    What I don't understand is the error message says:

    Only public user defined types defined in public object modules can be used.......

    And yet tagCM is a public UDT in a .BAS module

    Code:
    Public Type tagCM 
      isqFrom As Integer
      isqTo As Integer 
      cmk As Integer
      cmf As Integer
    End Type


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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

    Re: Problem with Property Let/Get in Class module

    Code:
    Public Property Get argcm() As tagCM()
    tagCM is a UDT you have it coded as if it were an array

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Problem with Property Let/Get in Class module

    Quote Originally Posted by DataMiser View Post
    Code:
    Public Property Get argcm() As tagCM()
    tagCM is a UDT you have it coded as if it were an array
    That's not the problem as it gives same results with or without the () and it is defined as array in the Class


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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

    Re: Problem with Property Let/Get in Class module

    jmsrickland,

    Declare them as "Friend", not "Public". I've been criticized for saying this elsewhere, but I absolutely know that I'm right. They just need to read the MSDN help system. I'll explain the entire difference between "Friend" and "Public" if you like, but, with "Friend" you can pass UDT's around from class modules and forms, in and out of BAS modules, all no problem.

    With "Public" there are much tighter restrictions. Just as an FYI, "Friend" declaration is allowed only in Class or Form but that should become your preferred way of declaring "exposed" procedures in a Form or Class.

    Take Care,
    Elroy

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

    Re: Problem with Property Let/Get in Class module

    Regarding post #4, I said "no problem", but there is one "got-cha". When using your forms with "Friend" declarations, don't Late Bind them.

    What is late binding? It's when you use the Object (or Variant) type to declare a variable to hold a class. Just be sure to use your actual class (and form) names to create variables for them (i.e., early binding), and the "Friend" declaration will always be your friend.

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

    Re: Problem with Property Let/Get in Class module

    Quote Originally Posted by jmsrickland View Post
    ...What I don't understand is the error message says:

    Only public user defined types defined in public object modules can be used.......

    And yet tagCM is a public UDT in a .BAS module
    Elroy provided the answer to the problem in post #4

    Regarding the confusion above. UDTs cannot be returned or set in a PUBLIC method, outside of a bas module, within a typical project. As Elroy suggested, use Friend vs Public. Otherwise, the exception here is if the project is a DLL or OCX and the UDT was declared Public in a global class object. When the dll/ocx is compiled, then projects using that dll/ocx will be able to use the UDT; therefore, the class would be able to return the UDT. Hope I made myself somewhat clear.
    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}

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

    Re: Problem with Property Let/Get in Class module

    *nods to LaVolpe*

    Yep, unless you're creating some kind of DLL server or ActiveX (OCX) component, I always try to declare all my CLASS and FORM procedures as either Private or Friend (almost never Public). (There are a couple of exceptions having to do with late binding issues, but those are very technical, and not applicable here.) However, with "Friend" the calls aren't "exposed" outside of the VBP project (such as a DLL API or OCX methods and procedures). However, I doubt that many people are using VB6 to make DLLs or OCX controls, but certainly a few may be.

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

    Re: Problem with Property Let/Get in Class module

    Elroy, besides ocx creation (quite common still), other exceptions to the Friend vs Public rule in classes.

    1. Classes to be used with the Implements keyword. A friend method cannot be implemented,
    2. Without verifying it, think CallByName can't be used with Friend methods

    May be a few more exceptions. I just think people need to be aware of the circumstances when one or the other can't be used
    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}

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

    Re: Problem with Property Let/Get in Class module

    Ahhhh, good points, LaVolpe.

    I do occasionally use "Implements", and it makes sense that "Friend" can't be used with it because that's a case where there's potentially an interface with "the outside world", which is precisely when the "Friend" no longer works.

    Also, I EXTREMELY seldom use CallByName, but that makes sense too. It seems like that would almost have to involve late binding since the "object" being passed is defined in the MSDN as a Variant, and not specifically declared.

    And yes, there are certainly exceptions. But when creating a "run of the mill" class, I'd argue that "Friend" is always the way to go, and you get the big advantage of UDT's.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Problem with Property Let/Get in Class module

    OK, I changed over to Friend. It removed the error I was having. OK, so far. Thanks, Elroy, for the suggestion


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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