home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / COM / SafeArray / run.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  2.0 KB  |  66 lines

  1. // 
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. // This sample shows how to use SafeArrays in Java.
  5. //
  6. //
  7. import com.ms.com.*;
  8. import simpleobject.*;
  9.  
  10. public class run
  11. {
  12.     public static void main(String args[])
  13.     {
  14.  
  15.         // Instantiate a COM object (which is implemented in Visual Basic)
  16.         _SimpleObject obj = new SimpleObject();
  17.  
  18.  
  19.  
  20.         // Passing a SafeArray to COM object:
  21.         //
  22.         // Here we invoke the routine:
  23.         //
  24.         //    'Returns the sum of the elements of A
  25.         //    Public Function ComputeSum(A() As Long) As Long
  26.         //
  27.         // with a 5-element array containing the elements {0,1,2,3,4}
  28.         //
  29.         // Remember: VB "Long" == Java "int".
  30.         SafeArray sa = new SafeArray(Variant.VariantInt, 5);
  31.         int ia[] = {0,1,2,3,4};
  32.         sa.fromIntArray(ia);
  33.         System.out.println("ComputeSum() returned " + obj.ComputeSum(sa));
  34.         System.out.println();
  35.  
  36.  
  37.         // Receiving a SafeArray from a COM object:
  38.         //
  39.         // Here we invoke the routine:
  40.         //
  41.         //    'Returns an array of the squares of 0..10.
  42.         //    Public Function CreateSquares() As Variant
  43.         //
  44.         // (the returned Variant will contain a SafeArray of Long).
  45.         //
  46.         // Remember: VB "Long" == Java "int".
  47.         Variant v = obj.CreateSquares();
  48.         sa = v.toSafeArray();
  49.  
  50.         // We can extract elements one at a time...
  51.         System.out.print("CreateSquares() returned {"); 
  52.         for (int i = sa.getLBound(); i <= sa.getUBound(); i++) {
  53.             System.out.print(" " + sa.getInt(i));
  54.         }
  55.         System.out.println(" }");
  56.  
  57.         System.out.print("Converting results to Java array: {"); 
  58.         // ...or translate the entire SafeArray to a Java array.
  59.         ia = sa.toIntArray();
  60.         for (int i = 0; i < ia.length; i++) {
  61.             System.out.print(" " + ia[i]);
  62.         }
  63.         System.out.println(" }");
  64.     }
  65. }
  66.