Wish New for Objects and Types

Cadenzo

Active Member
Licensed User
Longtime User
My dream is something like a "New"- keyword in VB.net ;-)
Also every type-definition could create in the background a Sub like "New +Typename"

B4X:
Type MyObject (Name As String, Height As Int, Width As Int, Color As Int)

Sub Sammple
    Dim sb As StringBuilder : sb.Initialize
    Dim obj1 As MyObject = NewMyObject("sample", 100, 100, Colors.Green) 'same parameters like in type-definition
End Sub

Sub NewMyObject(name As String, height As Int, width As Int, color As Int) As MyObject
    Dim t As MyObject
    t.Name = name
    t.Height = height
    t.Width = width 
    t.Color = color
    Return t
End Sub

'same sample with New
Sub Sample
    Dim sb As New StringBuilder
    Dim obj1 As New MyObject("sample", 100, 100, Colors.Green) 'same parameters like in type-definition
End Sub
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
i think that the initialize is very similar to new and it avoids the static methods also it is helpful with the isInitialized method

also we already have the second request, if you hoover over the type it will ask if you want to create a sub as a creator.
 

DonManfred

Expert
Licensed User
Longtime User
Wow, this was new for me
It was added in B4A 9.8 in 2020 :D
 

agraham

Expert
Licensed User
Longtime User
Owing to the internal structure of B4X programs Dim is effectively the equivalent of New as it instantiates a new object of the specified type. Though not usual some objects do not need further initialisation.

A lot of B4X objects are thin wrappers round a native object and while Dim creates the B4X wrapper object instance Initialize usually creates the inner native object and connects it to the B4X event structure via the provided event name. Other necessary initialisation parameters are also often provided to Initialize.
 
Top