B4J Question Initialization questions

wl

Well-Known Member
Licensed User
Longtime User
Hi,

I'm not quite sure how initialization works.

I often use a method that returns some object. Under some conditions such a method could just return null

B4X:
Sub SomeMethod as SomeObject
  if (some condition) then
    Dim obj as SomeObject
    obj.initialize
    return obj
  else
    return null
  end if
end sub

I sometimes encounter problems if checking the return of this method by means of:

B4X:
  if SomeMethod = Null then ...

I then check on "IsInitialized" but I can't seem to understand what "initialization" does. Some kind of construction, but still ...

Also: if I create a custom class can I cann my "initialization" method any name ?

Tx
 

wl

Well-Known Member
Licensed User
Longtime User
What is SomeObject? Is this a class of yours?

You cannot change the name of the Initialize method.
Hi Erel,

Indeed, in fact I meant:

B4X:
Sub SomeMethod as SomeClass
  if (some condition) then
    Dim obj as SomeClass
     obj.initialize
     return obj
  else
    return null
  end if
end sub

When calling this method, should I check with "IsInitialized" or check on <> null ?

I noticed some classes have two initialization methods (Initialize and Initialize2) ? If the name is fix, how many Initialize methods can you have then ?

Tx
 
Last edited:
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I meant for example the List class which has 2 initialize methods (called initialize and initialize2, probably due to lack of method overloading) ?
Or is this because List is an internal class and that custom classes can't have more than the standard "initialize" ?


Also: I understand I need to check with <> NUll, but then when exactly would you use "IsInitialized" ?

Thanks
 
Upvote 0
Top