Repository navigation
With
Home › User Defined Types › With
Statement block to allow implicit access to fields in a user defined type variable
With user_defined_var
statements
End WithThe With...End With block allows the omission of the name of a variable of a user-defined Type when referring to its fields. The fields may then be accessed with just a single period (.) before them, e.g. if the Type contains an field element called "element", then it could be accessed within the With block as ".element".
It can be used as a shorthand to save typing and avoid cluttering the source.
With can also be used with dereferenced pointers as the second example shows, with temporary instances (with explicit or implicit constructor) as the third example, and even with temporary types (without any constructor), because destruction of such temporary instances/types is deferred to the end of the With scope.
But returns byref of functions from temporary instances/types taken as arguments are not supported (and no error message).
With blocks may be nested. In this case, only the innermost With block is active, and any outer ones are ignored until the inner one is closed again. See the third example for an illustration of this.
Internally, the address of the variable is taken at the start of the With block, and then is used to calculate any element accesses within the block.
Note that this means that Goto should not be used to jump into a With block, otherwise the address will not have been set, and the results of trying to access it will be undefined.
Note for With block used inside member procedure:
To access duplicated symbols defined as global outside the Type, add two dots as prefix: "..SomeSymbol" (inside a With...End With block).
- Not available in the -lang qb dialect unless referenced with the alias
__With. In the -lang qb and -lang fblite dialects, variables declared inside aWith..End Withblock have a function-wide scope as in QB.
In the -lang fb and -lang deprecated dialects, variables declared inside aWith..End Withblock are visible only inside the block, and can't be accessed outside it.
- New to FreeBASIC
Type rect_type
x As Single
y As Single
End Type
Dim the_rectangle As rect_type
Dim As Integer temp, t
With the_rectangle
temp = .x
.x = 234 * t + 48 + .y
.y = 321 * t + 2
End WithType rect_type
x As Single
y As Single
End Type
Dim the_rectangle As rect_type Ptr
the_rectangle = CAllocate( 5 * Len( rect_type ) )
Dim As Integer loopvar, temp, t
For loopvar = 0 To 4
With the_rectangle[loopvar] '' dereferenced pointer
temp = .x
.x = 234 * t + 48 + .y
.y = 321 * t + 2
End With
NextType rect_type
x As Single
y As Single
Declare Constructor()
Declare Constructor(ByVal x0 As Single, ByVal y0 As Single)
End Type
Constructor rect_type()
End Constructor
Constructor rect_type(ByVal x0 As Single, ByVal y0 As Single)
This.x = x0
This.y = y0
End Constructor
Dim the_rectangle As rect_type
With rect_type(1, 2) '' temporary instance created here held up to 'End With'
the_rectangle.x = .x + .y '' 1 + 2 = 3
the_rectangle.y = .x - .y '' 1 - 2 = -1
End WithType rect_type
x As Single
y As Single
End Type
Dim As rect_type rect1, rect2
'' Nested With blocks
With rect1
.x = 1
.y = 2
With rect2
.x = 3
.y = 4
End With
End With
Print rect1.x, rect1.y '' 1, 2
Print rect2.x, rect2.y '' 3, 4Category: User Defined Types
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Time Zones
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing