1

i want to declare class globaly here is my example:

i want to use class clsIEError that looks exactly like this:

Option Explicit

Public Sub m(msg As String, Optional title As String = "Title:")

    'Attribute Value.VB_UserMemId = 0

    'this method will be used as defualt method
    'and here are attributes msg and title used to create some inteface
End Sub

and this is how it works example1:

Sub CATMain()

     Dim ie As clsIEError
     Set ie = New clsIEError

     ie "test", "title"

     Set ie = Nothing
End Sub

but my problem is that i want to have it globally example2:

Option Explicit

Public ie As clsIEError

Private Function Init()
    Set ie = New clsIEError
End Function

Sub CATMain()
    Call Init

'   and to use it same as in example 1    
    ie "test", "title" 

'   but i am able to use it only like:
'   ie.m "test", "title" 'works as expected 

    Set ie = Nothing
End Sub

why with public default method doesnt work?

tsolina
  • 141
  • 15
  • Why are `classes` created in the first place in general? – bonCodigo Jan 23 '13 at 14:22
  • I think is not important, but in this case i have some custom user interface that i want to use in different modules. – tsolina Jan 23 '13 at 14:54
  • Can you show us the code that you really want to use? – bonCodigo Jan 23 '13 at 14:55
  • instead of ' ie.m "test", "title" ' i want to use it as ' ie "test", "title" ', with public declaration, that's all – tsolina Jan 23 '13 at 15:00
  • i don't know about the catia VBA editor, but in case you're doing this in MS office or Catia is similar, you cannot provided the attributes simply in code - you unfortunately need to export the class, edit the text document with the Attribute line and then import it back (where the line is hidden in the editor) - see http://www.cpearson.com/excel/DefaultMember.aspx Though I agree that then example one should not work either... :-/ – Peter Albert Jan 23 '13 at 19:42
  • you are right it is not possible to provide attribute directly in vba editor, i know that as well :) i did followed this procedure from cpearson as well. – tsolina Jan 24 '13 at 12:23

2 Answers2

1

I can confirm that this doesn't work just as you describe. I get "Expected procedure, not variable" at run-time, but no compile errors. It must be a bug in the VB parser, but that's the only explanation I can come up with.

I always thought that the attribute had to match the element name. Where you have

Attribute Value.VB_UserMemId = 0

I thought you should have

Attribute m.VB_UserMemId = 0

But it seems to work (with the locally declared variable) either way. It's a terrible answer, but the answer is to explicitly call the method. Sorry.

Dick Kusleika
  • 32,673
  • 4
  • 52
  • 73
  • Hi Dick, thank you for your answer. Attribute element name will be automatically changed to pass to element name. If you change a name of element, or even type of it, vba editor will adjust it automaticaly. in fact, it doesnt have to work globally, but i would like to use this object at least on module/class module level. – tsolina Jan 24 '13 at 12:19
0

I am not sure that it will answer your question explicitly, but, as for me I wanted to create an instance of a class that would be accessible throughout my project. Basically I needed to create Static Class or Singleton. I found this post that was quite useful.

Community
  • 1
  • 1
DomLavoie
  • 81
  • 2
  • 4