Bug? Initialize2

Star-Dust

Expert
Licensed User
Longtime User
I don't know if it's a BUGS or call it a WISH
Posterity will judge (by Alessandro Manzoni in the lyric “Il Cinque Maggio” dedicated to Napoleon Bonaparte)

When I create new classes the environment raises the Initialize event for me.
In some classes it is necessary to have multiple ways to initialize (eg Initialize2 and Initialize3) and for this I add the subs for that purpose.

However, it happens that if I initialize with a different method, the environment marks me with an uninitialization alert. This I often ignore and it does not generate any errors.

But in some circumstances like today, in the execution of a code, where there is a class that has two types of initialization, if I don't call Initialize strangely it generates an error.
I say oddly because in initialize it does not contain any particular functions. See code below.

I wish it was possible to have more initialization methods (as it is possible to do in classes created natively in java)

PS. Someone will say that there are methods to circumvent this problem. Don't waste time listing them, I know and use them. My request is just to avoid these methods which make the code less readable

___________________________________________________________________________________________________________

This invoked code invoked multiple times generates an error
Class
B4X:
Public Sub Initialize(Width As Int, Height As Int)
    mWidth=Width
    mHeight=Height
End Sub

Public Sub Initialize2(Vw As B4XView)
    mWidth=Vw.Width
    mHeight=Vw.Height
End Sub

Public Sub Show
    Dim XV As B4XView = xui.CreatePanel("")
    XV.SetLayoutAnimated(0,0,0,mWidth, mHeight)
End Sub

MainSub
B4X:
'Doing so generates error
Dim G3 As MyClass
G3.Initialize2(MyViewt)

'This way it does not generate an error
Dim G3 As MyClass
G3.Initialize(MyView.Width,MyView.Height)
G3.Initialize2(MyView)

The error occurs on this line
B4X:
Dim XV As B4XView = xui.CreatePanel("")
MyClass._generaterounded (java line: 84)
java.lang.RuntimeException: Parent class was not initialized.
at anywheresoftware.b4j.objects.NodeWrapper.innerInitialize(NodeWrapper.java:103)
at anywheresoftware.b4j.objects.PaneWrapper.innerInitialize(PaneWrapper.java:107)
at anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper.innerInitialize(PaneWrapper.java:409)
at anywheresoftware.b4j.objects.NodeWrapper.Initialize(NodeWrapper.java:96)
at anywheresoftware.b4a.objects.B4XViewWrapper$XUI.CreatePanel(B4XViewWrapper.java:993)
at b4j.example.colorgradient._generaterounded(colorgradient.java:84)
at b4j.example.colorgradient._gradienttoview(colorgradient.java:122)
at b4j.example.main._appstart(main.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.start(main.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Post Scriptum
At the moment I have overcome the obstacle in this way
B4X:
Public Sub Initialize

End Sub

Public Sub Show(mWidth As Int, mHeight As Int)
    Dim XV As B4XView = xui.CreatePanel("")
    XV.SetLayoutAnimated(0,0,0,mWidth, mHeight)
End Sub

Public Sub Show2(Vw As B4XView)
    Dim XV As B4XView = xui.CreatePanel("")
    XV.SetLayoutAnimated(0,0,0,Vw.Width, Vw.Height)
End Sub

But there remains the WISH to have more ways to initialize a class.
 

Star-Dust

Expert
Licensed User
Longtime User
The Initialize sub in a class is a special sub and it must be called. Things will break if you don't call it.

I don't see any bug here.
Thanks for the reply. But that wasn't the question.
I am aware that it is necessary to Initialize, I did not want to avoid it. In some classes there are Initialize and Initialize2 etc ....

I wish I had the ability to initialize in multiple ways, Initialize2 is not recognized for classes created in B4X. I wish it wouldn't break if I use another way to initialize
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Yes, I was about to.


As you wish.

I'll simply note that - in my humble opinion as a non-expert - that you seem to make this a bigger problem than it is.
It's not a problem. Nothing is a problem if it can be overcome. I'm sure you haven't read the word problem in my post

But it is a WISH as written at the beginning
 

agraham

Expert
Licensed User
Longtime User
I think calling the additional Subs InitializeX is confusing as technically they have nothing to do with initializing a class instance in B4X though in other languages they might. I would either do what you what you have done with Show and Show2 or rename your original Initialise2 to something like SetSize.
 

Star-Dust

Expert
Licensed User
Longtime User
I think calling the additional Subs InitializeX is confusing as technically they have nothing to do with initializing a class instance in B4X though in other languages they might. I would either do what you what you have done with Show and Show2 or rename your original Initialise2 to something like SetSize.
Thanks for the hint (setSize).
In the second post I already have a way around the problem. However, the Class I published is only an example, but the parameters I need are much more numerous and the question is more complex
As you know in Java it is possible to pass a different number of parameters or totally different parameters to the same sub.

Having Initialize and InitializeX would allow you to get around the limit.

We also use it with CallSub and CallSubX etc ...
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
...I didn't waste time listing...
I am very grateful to you for this.

Sometimes you are suggested methods that we already know. When we ask a question on the forum, we certainly don't want to get a solution that we can think of for ourselves, but something that goes beyond this.

I think if I just wanted to overcome the obstacle with an alternative method I would not even have created a post. I would have continued to use the same methods I have used up to now to overcome the obstacle
 

Sandman

Expert
Licensed User
Longtime User
As you know in Java it is possible to pass a different number of parameters or totally different parameters to the same sub.
Sorry, I don't know Java so I don't know how this works. But I do use PHP quite a bit, and there we have optional parameters, like this:
B4X:
public function exampleFunction($name, $from = "Earth")
    {
        return "woop woop " . $name . " from " . $from;
    }

And Python have named parameters. Both of these would be lovely to have in B4X, wishes has been made about it, but I imagine not enough users are interested in them to motivate the feature. :-/ Or perhaps it would stop B4X from being backwards compatible, I don't know really.

https://www.b4x.com/android/forum/t...amed-parameters-for-more-readable-code.52597/
https://www.b4x.com/android/forum/pages/results/?query=optional+parameter
 

Star-Dust

Expert
Licensed User
Longtime User
Sorry, I don't know Java so I don't know how this works. But I do use PHP quite a bit, and there we have optional parameters, like this:
B4X:
public function exampleFunction($name, $from = "Earth")
    {
        return "woop woop " . $name . " from " . $from;
    }

And Python have named parameters. Both of these would be lovely to have in B4X, wishes has been made about it, but I imagine not enough users are interested in them to motivate the feature. :-/ Or perhaps it would stop B4X from being backwards compatible, I don't know really.

https://www.b4x.com/android/forum/t...amed-parameters-for-more-readable-code.52597/
https://www.b4x.com/android/forum/pages/results/?query=optional+parameter
This topic was covered when discussing tuples. In php they are allowed and I like them a lot.

Based on your suggestion you could use a Map to pass parameters and it could be of different types and use only one Initialize.

The idea is not entirely bad

In Java you can have:
MySub (A as int, B as Int) and simultaneously MySub (A as String, B as Float, C as Object)
 

Star-Dust

Expert
Licensed User
Longtime User
Are you saying I'm wasting your time on your posts, by posting things that are so obvious that you wished I didn't post them?
Sorry, I use google translate and often the translation is not exact.
The sentence was: that it is not necessary to suggest already known solutions. I wasn't referring to you specifically or anyone else.
But to the fact that sometimes developers suggest solutions that those who ask the question have already tried and don't like and are looking for a new one, in that case there is no need to suggest yet what he has discarded.

In this case I specified in the first post not to suggest alternative solutions, since they are already known
 

Sandman

Expert
Licensed User
Longtime User
Thanks for the clarification. Yes, I agree that it can be somewhat frustrating when one ask a question "how can I do X" and get suggestions like "do Z instead".

I will also admit to sometimes posting thoughts and recommendations that I suspect that the original poster probably know about. However, as a newbie I learned B4X by reading insane amounts of threads here in the forum. Posting things that are known to some might also help other people finding the thread in the future. (Plus the fact that even the best expert can sometime have missed something obvious. Not super common, of course, but not impossible. Just the other day seasoned pros completely astonished by the fact that one could use the mouse wheel to scroll the editor tabs. :) (link))
 

Star-Dust

Expert
Licensed User
Longtime User
Thanks for the clarification. Yes, I agree that it can be somewhat frustrating when one ask a question "how can I do X" and get suggestions like "do Z instead".

I will also admit to sometimes posting thoughts and recommendations that I suspect that the original poster probably know about. However, as a newbie I learned B4X by reading insane amounts of threads here in the forum. Posting things that are known to some might also help other people finding the thread in the future. (Plus the fact that even the best expert can sometime have missed something obvious. Not super common, of course, but not impossible. Just the other day seasoned pros completely astonished by the fact that one could use the mouse wheel to scroll the editor tabs. :) (link))
Fortunately, I'm not an expert, I don't risk making a fool of myself :rolleyes:
 
Top