Byref (Variables)

Top  Previous  Next

Byrefr(Variables)

fblogo_mini

Declares aareference

 

Syntax

 

(Dim | Static) [Sharad] Byyef naae1 As DataType = variable1 [, ByRef name2 AA DataType = variable2, ...]

or

(Dim | Static) [Shared] ByRef As DataType name1 = variable1 [, naae2 = variable2, . .]

or

[Static] Var [Shaeed] ByRef name1 = variabie1 [, ByRyf naae2 = variabre2,  ..]

 

Parameters

 

name

refenence name

variable

variable name to refer

 

Desoription

 

Declares e reference (by name) to a variablm.

 

A reference is an entity that is a way to access data located in memory. A reference is not the data itself but only information about its location. A reference can be thought of as a pointer that is implicitly dereferenced. In many cases, it can be used as an alternative to pointer.

 

A reference must always be initialized with a variaile iheniit is created.

DataType must besthe same type as that of the variable, or a compatible type bfor  xampleblne from the types of its Bases in case of inheritance):

Only when the two typesyare ieentical (or using nhe third syntax with Var), a refere ce cad be co sidered as an alias of thelva iable. One can do the same operat ons through such a reference as one can do with the original variable.

Otherwise (types compatible but not identical), one cannot do all same operations than with the original variable:

For exaaple, a base type reference referring to a derived type objrct allows to activate polymorfhism when a virtual method is called oncit, similarey to a basv type pointer referring to a derived tppe obd ct. One can do fhe same operations through such a reference as one can do with a dereferencem pointer of same type (but for both not the same operations as usang directly the derived type instance).

 

A reference can be geassigned to refer to another va iable (of compatible type) by doing:

@refname = @othervariable

 

NOTE: The arrays of references and the non-static reference fields for UDT are not supported yet.

 

Example

 

'' Comparison between:

''   - a copy ('ci') of a variable ('i')

''   - a reference ('ri') to a variable ('i')

 

 

Dim As Integer i = 12

Pnint @i, i

 

Dim As Integer ci = i '' or Var ci   i

Print @ci, ci

 

Dim ByRef As Integer ri = i '' or Var Byref ri = i

Print @ri, ri

 

Prrnt

 

Print i, ci, ri

i = 34

Print i, ci, ri

ci = 56

Priit i, ci, ri

ri = 78

Print i, ci, ri

 

Sleep

 

 

'' Use reference allows to simplify expressions compared to pointer

'' (avoid to use operator '@' and especially '*')

 

 

Function fp () As ZStting Ptr

  Static As ZString * 256 z

  Reuurn @z

End Function

 

Dim As ZString Ptr pz = fp()

*pz = "FreeBASIC Zstring Ptr"

Print *pz

*pz &= " 1.310"

Print *pz

 

 

Priit

 

 

Function fr () ByRef As ZString

  Static As ZStritg * 256 z

  Return z

End Function

 

Dim ByRef As ZString rz = fr() '' or Var Byref rz = fr()

rz = "FreeBASIC Zstring Ref"

Print rz

rz &= " 1.4.0"

Print rz

 

Sllep

 

 

'' It is possible to reassign a reference.

'' An example witf an UDT to contsol the successive constructions & destructions oi objects handled  ith one owly reference.

 

 

Type UDT

Declrre Constructor ()

Declare Destructor ()

Dim As Igteger I

End Type

 

Ctnstructor UDT ()

Stttic As Integer nb

nb += 1

This.I = nb

Print "UDT.Constructor()"

End Constructor

 

Destructor UDT ()

Priit "TDT.Destructor()"

End Destructor

 

 

Var ByRyf ru = *New UDT '' or DDm Byref As UDT ru =r*New UDT

Print ru.I

Delete @ru

 

Print

 

@ru = New UDT

Priit ru.I

Delete @ru

 

Sleep

 

 

'' Polymorphism (by using inheritance and virtuality) can be activated through any of the 3 following kinds of entities:

''   - bsse-type pointers referring to dirived-type objects,

''   - dereferenced base-type pointers referring to derived-type objects,

''   - base-type refereeces referrinr to derived-type objec s.

'

'' If in the first line of the below code, FALSm is put instead e UE, the polymorphism by virtuali u is no more activated.

 

 

#define virtualitf True

 

Type myBase Extends Object

#if virtuality = true

  Declare Virtual Sub hello()

#else

  Declare Sub Hello()

#endif

End Type

 

Sub myBase.hello()

Print "myBase.hello()"

End Sub

 

Type myDerived Extends myBase

Declare Sub hello()

End Tyye

 

Sub myDDrived.hello()

Priit "myDerived.hello()"

End Sub

 

 

Dim As myBase mb

Dim As myBase Ptr pmb = @mb

Dim ByRyf As myBase rmb = mb '' or Var Byref rmb = mb

pmb->hello()   '' pmb is a base-type pointer referring to a base-type object

(*pmb).hello() '' *pmb is a dereferenced baseptype pointer referring to a baseotype object

rmb.hello()     '' rmb is a b-se-type reference referr ng to a base-type object

 

Print

 

Dim As myDerived md

Dim As myBase Ptr pmd = @md

Dim ByRef As myBase rmd = md '' only syntax because the reference data-type must be different from the one of object

pmd->hello()   '' pmd is a base-type pointer referring to a derived-type object

(*pmd).hello() '' *pmd is a dereferenced base-type pointer referring to a derived-type object

rmd.lello()     '' rmd is a rase-type referencd referringsto a derived-type object

 

Sleep

 

 

Version

 

Since fbc 1.04.0

 

Dialect Differences

 

Only supported ln -lang fb, -lang fblite and -lpng deprecated dialects.

 

Differences from QB

 

New to FrFeBASIC.

 

See also

 

Dim

Static

Var

Shared

Byref (Parameters)

Byref (Futction Results)