Re: Can you pass a Property of a User Defined Object to a Sub ByRef and Update it?
- From: "Shadow Lynx" <shdwlynxjunk@xxxxxxxxx>
- Date: 11 Jan 2006 08:39:29 -0800
You weren't condescending and I appreciate your responses. As far as I
understand it, the point of making Public Properties in classes is to
enable the rest of the your code to access them. In real code, I make
many variables and dictionaries available as public properties within
classes (for example, a User class which has many individual properties
as well as multi-value properties like E-Mail Address that live in a
Dictionary.)
I just realized why you've been bringing up the whole "may as well use
a global variable" thing. My example is fundementally flawed. In my
real code, the functions are actually methods within the classes, like
so:
Dim i
Dim x: Set x = New MyUserType
x.MyProperty = "Thingy"
x.MyDictionary.Add "First", "Neato"
--> x.LoadFromLDAP()
Response.Write(x.MyProperty & "<br>")
For Each i In x.MyDictionary.Keys
Response.Write(i & " = " & x.MyDictionary.Item(i) & "<br>")
Next
Set x = Nothing
Class MyUserType
Private varMyProperty, varMyDictionary
Public Property Let MyProperty(value)
varMyProperty = value
End Property
Public Property Get MyProperty
MyProperty = varMyProperty
End Property
Public Property Get MyDictionary
Set MyDictionary = varMyDictionary
End Property
Private Sub Class_Initialize()
Set varMyDictionary =
Server.CreateObject("Scripting.Dictionary")
End Sub
--> Sub LoadFromLDAP()
DoThing x.MyProperty
DoThing x.MyDictionary
End Sub
--> Sub DoThing(ByRef y)
If isObject(y) Then
y.Add "Second", "Spiffy"
Else
y = "Stuff"
Response.Write("y = " & y & "<br>")
End If
End Sub
End Class
Regardless of where the function is and how I use the class, etc., the
point of all this is that I do not understand why ByRef is not acting
like ByRef. (ByRef y) means that a POINTER to the original object (be
it a simple variable, object, whatever) is supposed to be created, such
that the original object is being modified, not a copy (ByVal). My
question is WHY is it NOT working this way? If I use a simple variable
(rather than a variable accessed via a class property) then ByRef works
as expected - you can see when in Debug that the original variable is
immediately updated.
The only solution I've come up with is lame: I must make two methods to
handle this situation. One for simple variables which is a function
and returns a value, the other a sub which directly accesses the
Dictionary object. I say this is lame because having a single method
that handles both situations is more elegant and SHOULD WORK.
Of course, in .Net (using Hashtable instead of Dictionary, etc.) it
DOES work for both objects and simple variable Properties.
.
- Follow-Ups:
- References:
- Can you pass a Property of a User Defined Object to a Sub ByRef and Update it?
- From: Shadow Lynx
- Re: Can you pass a Property of a User Defined Object to a Sub ByRef and Update it?
- From: mayayana
- Re: Can you pass a Property of a User Defined Object to a Sub ByRef and Update it?
- From: Shadow Lynx
- Re: Can you pass a Property of a User Defined Object to a Sub ByRef and Update it?
- From: mayayana
- Can you pass a Property of a User Defined Object to a Sub ByRef and Update it?
- Prev by Date: Re: Breaking out of a Select statement?
- Next by Date: Unix "strings" equivalent in VBScript
- Previous by thread: Re: Can you pass a Property of a User Defined Object to a Sub ByRef and Update it?
- Next by thread: Re: Can you pass a Property of a User Defined Object to a Sub ByRef and Update it?
- Index(es):