property set byref

  • Thread starter Thread starter Cc
  • Start date Start date
C

Cc

hi,
is there a way to use byref on property set , because i would like to pass
the value into the variable byref ?
 
Cc,
As Fergus suggested, I too suspect you are confusing ByRef & ByVal
parameters with Reference Types & Value Types.

Remember there are two types of Parameters (ByRef & ByVal) and there are two
types of variables (Reference Types & Value Types).

So you can have:
ByRef - Reference Type
ByRef - Value Type
ByVal - Reference Type
ByVal - Value Type

Lets look at types of variables:
When you define a Class you are defining a Reference Type. Which means that
a variable of this Class holds a reference to the object, the object itself
exists on the heap. If I assign this variable to a second variable a copy of
this reference is made and I now have two references to the same object on
the heap. There is still only one object on the heap. If you define a
Structure you are defining a Value Type. The variable itself holds the value
of the structure. If I assign this variable to a second variable a copy of
the entire structure is made. I now have two copies of the same structure.

Value Types include:
Boolean, Byte, Short, Integer, Long, Char, Single, Double, Decimal, along
with anything defined with the Structure or Enum keyword.
Value Types all derive directly or indirectly from System.ValueType

Reference Types include:
Object, and anything defined with the Class, Interface or Delegate keyword
are reference types.
Reference Types all derive from System.Object excluding types that inherit
from System.ValueType

Interface is a reference type, even if defined in a Structure. The structure
itself is a value type, however if you assign the structure to a Interface
variable, it will be Boxed, boxing places the value on the heap in a new
object (effectively making it a reference type)

Lets look at types of parameters:
Now when you define a parameter to be ByVal a copy of the variable is
passed. Remember Reference types hold a reference to the object, so passing
a Reference Type ByVal causes a copy of this reference to be passed as a
parameter, the single copy of the object itself is still on the heap. The
variable & parameter both have references to this single object. Passing a
Value Type ByVal causes a complete copy of the value to be passed as a
parameter. Now passing a Reference Type ByRef, causes a reference to the
variable to be passed, the variable has a reference to the object. Passing a
Value Type ByVal also causes a reference to the variable to be passed, the
variable has a copy of the Value.

Hope this helps
Jay
 
Excellent reply !

--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
 
ok I will make an example
class1.vb :
class class1
private local_struct as system.valuetype



public function a1(byref arg_struct as system.valuetype)
if arg_struct.id =0
arg_struct.id =1
end if
end function

public functon B1(byref arg_struct as system.valuetype)
if arg_struct.id = 3
arg_struct.id = 2
end if
end function

end class


--------------------------
form1.vb
Structure Person

<VBFixedString(10)> Public ID As String

<VBFixedString(15)> Public Name As String

End Structure

private sub test()
dim testclass as new class1
dim m_person as person

m_person.id = 0
testclass.A1(m_person)

if m_person.id =1
'do something
else
m_person.id = 3
end if

testclass.B1(m_person)

msgbox(m_person.id)

end sub

----------------------------

in this example function A1& B1 are suppose to use same variable instance
that is m_person. but if I can pass by reference m_person (form1) to
local_struct (class1) that will give less coding (no need to fill in value
to the function parameter). i try to use property set for local_struct but
property set can only use byval.


I wonder why VB designer ignore such case

rgds
charles
 
Cc said:
ok I will make an example
[example]

Could you please give us a working example? I tried to complete/correct the
code but without success.

in this example function A1& B1 are suppose to use same variable
instance that is m_person. but if I can pass by reference m_person
(form1) to local_struct (class1) that will give less coding (no need
to fill in value to the function parameter).

Which value? which function?
i try to use property
set for local_struct but property set can only use byval.

???
 
Cc,
in this example function A1& B1 are suppose to use same variable instance
that is m_person.
Yes, we all know how to ByRef works with functions. What we want to know is
how you expect the ByRef to work with the Property Set.
but if I can pass by reference m_person (form1) to
local_struct (class1) that will give less coding (no need to fill in value
to the function parameter). i try to use property set for local_struct but
property set can only use byval.
Huh? Where is this property set & what does it look like. Is it part of
Class1, Form1, Person, or some other class?

Can you continue this example and include the Property Set with ByRef so we
can see what you are attempting to do.

Remember procedures that have side effects are generally not a good idea. A
function that modifies its parameter is questionable, as a function is
intended to return a value, but wait did I mention it also modifies one of
its inputs, if I needed to modify the parameters, I would make it a Sub. A
property that only modifies its parameter is questionable. I can see
"Parent" like properties when when you set a property modifies both the
parent & child to maintain the relationship.

I strongly suspect you want (need!) the Person Structure to be a Person
Class (you know, the difference between Reference Types & Value Types, not
ByVal Parameters & ByRef Parameters).

Hope this helps
Jay
 
Back
Top