Results 1 to 6 of 6

Thread: a little problem concerning Private\Public declarations in active dll [Resolved]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170

    a little problem concerning Private\Public declarations in active dll [Resolved]

    i have made a dll project but i hav faced a small problem
    that has to be fixed.
    the problem was that i need to access a private variable
    from outside.
    yes, i know that i declared that variable as private so that it cant be changed by a user or, but my point is that
    i want to let an instance of the object
    change the value for another instance , not another library
    or statement.
    there is a read-only property to get that value, if this would
    make a difference.
    a simplified exaple is lik this:

    ***make project1 with class module "class1":

    Private PrivVar

    Public Property Get PrivateVariable()
    PrivateVariable = PrivVar
    End Property

    ***now make project2 with "form1":

    Dim x1 as New class1
    Dim x2 as New class1

    now i want x1 , to change the value of PrivVar in x2.
    can i ????

    another thing,
    can i make something like the Copy Constructor for my dll
    like that in C++ ???
    i want that copy constructor to change all the properties of
    the copeid instance including the private values, something like

    x1 = x2

    can i ???

    please help me doing such a thing .... thanks for ur concern .
    Last edited by ZaidGS; Aug 22nd, 2003 at 04:55 PM.

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    if i correctly understand your question, what youre asking is a classic use of the 'Static' keyword. variables declared as Static are shared across all the instances of the objects instantiated from a class.
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  3. #3
    Lively Member
    Join Date
    Apr 2003
    Posts
    114
    STATIC may work for you. But if not, I'm not sure if this will work for you. Try adding a FRIEND property LET. I do not know of a way to access a variable declared as private outside of a class without using some type of memory hack.

    Ex:
    Friend Property Let PrivVar(valu as string)
    mPrivVar = valu
    end Property

    Then create a private variable of the same type as the current class in the declarations section.

    Dim x2 as Class1 'be sure not to use New Class 1

    Create a sub to receive the variable, from a project that is referencing the dll, that is of the same type as Class1

    public Sub SetX2(cX2 as Class1)
    set x2 = cX2
    End Sub

    Create a sub to set the private variable from within the class

    Private Sub SetPrivateVariable()
    'Call this when you need from inside this class
    x2.PrivVar = "Some value"
    end Sub

    Your project that is accessing the DLL code may look something like this:
    dim X1 as New Class1
    dim X2 as New Class2

    X1.SetX2 X2


    Now X1 can set the friend variable in X2 but users of your DLL cannot.

    Remember that Friends are anything within the project so if you add another class to the project, that class will have access also if a variable is declared. Hope that helps a little.
    I can do all things with VB.

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    I think you can solve your problem by adding a standard code module to your DLL project, and using a public variable in this module. Your read-only property will return the value contained in this variable. Also the value of this variable will be shared across objects, and it won't be available outside the DLL project.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170

    Thumbs up

    ok, thanks for ur help,
    my problem concerning private\public variables
    was solved by friend declaration.
    although i know the function of friend declaration,
    it seems that i didnt know how to use it well,
    in fact the idea of the function (( SetX2 )) in another class
    of the same project didnt pass me.
    i have made the Property Let function a friend
    so that the property is not read-only in the scope of the project,
    but read-only outside it.
    by the way, the static declaration is not really good for my
    case because the private variable is not the same amoung
    all the instances.

    but now there is the other part of my question
    concerning the copy constructor
    i have made a new function:

    Public Sub CopyClass(X1 as class1, X2 as class1)
    .... some code that copy all values including private variables...
    End Sub

    but is the idea of copy constructor found in the VB ???
    something like:
    X2 = X1
    not a sub like:
    CopyClass(X1, X2)
    ???

    do i hav a better option ???

  6. #6
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    VB doesn't actually have a constructor per se, as you find in C++ or Java. Every time a VB class is instantiated, the Class_Initialize event for the class fires up.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

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