Porting to B4A

sterlingy

Active Member
Licensed User
Longtime User
Howdy All,

I'm trying to port some code from AcrionScript3. The code uses the "New" operator. I believe it is similar to "New" in VB6. Any ideas on how to achieve the same thing with B4A?

Below is part of the code I am porting. The function cloneVector is suppose to clone the very object that is associated with the class that this function is in.

B4X:
   public class Vector2D {
      private var _x:Number;
      private var _y:Number;
      
      /**
       * Constructor
       */
      public function Vector2D(x:Number = 0, y:Number = 0) {
         _x = x;
         _y = y;
      }
      
      /**
       * Creates an exact copy of this Vector2D
       * @return Vector2D A copy of this Vector2D
       */
      public function cloneVector():Vector2D {
         return new Vector2D(x, y);
      }

Any thoughts would be appreciated.
 

qsrtech

Active Member
Licensed User
Longtime User
I think you would just dim a new variable of the vector class, initialize it and return it. something like:

B4X:
'this is somewhere in your project
dim ClonedVector2D as Vector2D 'this will be your clone
ClonedVector2D=IWantToCloneVector2D.cloneVector

'this is in your Vector2D class
public sub cloneVector as Vector2D
    dim NewVector2D as Vector2D
    NewVector2D.Initialize(me.x,me.y) 'probably don't need "me"
    return NewVector2D
end sub
 
Last edited:
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
That is exactly what I did. But when I compiled, I got an error.

The method was called with this:

B4X:
   myVector.Initialize(3,4)
   
   thisVect = myVector.cloneVector
   Log("newVect: " & thisVect)

B4X:
Parsing code.                           0.01
Compiling code.                         0.02
Compiling layouts code.                 0.01
Generating R file.                      0.17
Compiling generated Java code.          Error
B4A line: 35
thisVect = myVector.cloneVector
javac 1.7.0_17
src\b4a\example\main.java:243: error: inconvertible types
mostCurrent._thisvect = (b4a.example.vector2d)(mostCurrent._myvector._clonevector());
                                              ^
  required: vector2d
  found:    String
1 error
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…