CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Class variable from a different module

    I have an ActiveX DLL project with one Class module and several "normal" modules.

    I have this in the Class module :

    public ErrType as string
    public ErrNum as Long
    public ErrNative as Long
    public ErrDesc as string
    public ErrDescComplete as string
    public ErrSource as string
    public ErrLine as Long




    How do I access those variables/properties from the "normal" modules?
    I tried using the ClassModuleName.ErrNum, I tried Self., Me. ...

    Thanks for any info, Jenda
    ====================================
    If you get a VB/VBScript code from someone without "Option Explicit" on top ... kill him/her !
    If you ever meet anyone from Aptech, don't waste time looking
    at the code!



  2. #2
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: Class variable from a different module

    If u use this class u should be having an instance of that.
    So the Variables of that instance are for that particular object only.

    For example if u define an object in a "normal module" as a public variable in this class type - Public MyVar as MyClass - ,
    and then create a new instance by - Set MyVar = New MyClass , then
    the values given to the public variables of object, can be accessed
    within that particular "normal module"
    Srinika



    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  3. #3
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Re: Class variable from a different module

    Well .. I don't have a named instance.

    I did not create the object in any function in the "normal" module.

    What I did is: 1) I created the module in another program
    2) I called its method.
    3) This method called a function from the "normal" module.
    4) This function called other functions and then somewhere down there after many calls in several of the functions/subs in the module I need to access the properties of the object.

    I tried to store a reference to the object in a global variable, but then there is the problem that the variable is common to all instances.

    Jenda

    P.S.: I'm glad I've tried and tested this. The Bugtechs have next to everything in globals. And even though this particular object is unlikely to be used in several instances at a time ...


  4. #4
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: Class variable from a different module

    Can u please give me the names of the Modules and from which one, to which one is called and where r the Public Variables defined.
    It's unclear to me
    1. the "Other program" that u r talking about in ur 1. Whether this is a class or what ?
    2. What is the Method that u r talking about in ur 2 ?
    3. How do u call that Method?
    ...

    Give me the details like

    Class1:
    Public var1 as Integer
    Public var2 as String
    Public Method1(i as Integer) as String
    .....

    Module1:
    ...
    ...
    Dim c as new Class1
    c.Var1 = 5
    c.Var2 = "AnyStr"
    ....



    Or simply tell what is ur aim.!!!
    Regards
    Srinika

    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  5. #5
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Re: Class variable from a different module

    "Normal" Module

    public Function Foo(...)
    ...
    x = Bar(...)
    ...
    End Function

    private Function Bar(...)
    ...
    If SomethingFails(...) then
    Self.ErrNum = 50001
    Self.ErrDesc = "The Something failed"
    end if
    ...
    End Function




    Class Module

    public ErrNum as Long
    public ErrDesc as string

    public Sub SomeMethod(...)
    ...
    y = Foo(...)
    ...
    End Sub




    An ASP Page (I know I said I use the object from an EXE. I got confused.)

    Dim obj
    set obj = Server.CreateObject("The.Class")
    obj.SomeMethod(...)
    if obj.ErrNum > 0 then
    ....




    I do not know what to use instead of the Self. in function Bar.
    I tried to store a reference to the object into a variable defined in the "normal" module:

    ' In Normal Module
    public Self as The.Object


    ' In Class module in SomeMethod

    set Self = me



    but the "normal" module globals are shared by all instances.

    I have moved all the functions and stuff from the "normal" module into the Class one and that works all right. I had to do it anyway since it was written using a lot of global variables so I have to move the variables to the class anyway to allow multiple instances at the same time.

    I wish I had time to rewrite the thing from scratch. I'm sure it could be ten times quicker :-(

    Jenda


  6. #6
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: Class variable from a different module

    What is ur "Self" ?
    y r u (or rather how r u) using Foo(..) inside the class ? That function is external to the class and is in "Normal Module"

    Is this "Normal Module" a class or a standard exe ?

    Do u want to create an object having error details, when something fails?
    If so createObject() has to be used in the place where failing is caught and assign the properties of the YourError Object using its public variables (or properties).

    In ur program u r doing a circular reference!!!
    Foo --> Bar --> ErrorObj ---> Foo

    Also no idea about the 2 latter code portions (where those r placed....)
    I'm confused
    Srinika


    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  7. #7
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Re: Class variable from a different module

    The Self. is what I need to know how to write. I tried Self., Me., ClassName. I tried everything I could think of.

    The "Normal module" is just a file with .bas extension included in the same ActiveX project as the Class module.

    I don't want to create an object with the error details. I want to fill in properties of the object that called the Foo() function with the error details.

    No I do NOT have any circular reference.
    ASP page creates Obj, calls Obj.SomeMethod, the SomeMethod calls Foo(), Foo() calls Bar(), Bar sets some properties of Obj and returns, Foo returns, SomeMethod returns, ASP checks the properties of Obj to see if there was an error.

    For the code portions at the end of my last mail :
    First comes on top of "Normal" module, second comes into the SomeMethod function ABOVE the Foo() call.

    Jenda


  8. #8
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: Class variable from a different module

    I think I got hold of what u r saying
    u'll have to pass that to the functions u call
    pass as a parameter & refer the argument name

    public Function Foo(obj as Class1, ...)
    ...
    x = Bar(obj , ...)
    End Function

    private Function Bar(Self as Class1, ...)
    ...
    If SomethingFails(Self as Class1) then

    Self.ErrNum = 50001
    Self.ErrDesc = "The Something failed" end if
    ...
    End Function


    Class Module
    public ErrNum as Long
    public ErrDesc as string
    public Sub SomeMethod(obj as Class1, ....)
    ...
    y = Foo(obj, ...) ...
    End Sub

    ' in ur ASP Page
    ' **********************************************
    Dim obj set obj = Server.CreateObject("Class1")
    obj.SomeMethod(obj, ....)
    if obj.ErrNum > 0 then ....
    ' **********************************************





    Srinika


    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

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

Featured