|
-
Apr 28th, 2003, 11:21 AM
#1
Thread Starter
Addicted Member
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.
-
Apr 28th, 2003, 12:33 PM
#2
Fanatic Member
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.
-
Apr 28th, 2003, 01:03 PM
#3
Lively Member
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.
-
Apr 28th, 2003, 01:09 PM
#4
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.
.
-
Apr 29th, 2003, 03:05 AM
#5
Thread Starter
Addicted Member
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 ???
-
Apr 30th, 2003, 09:13 PM
#6
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.
.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|