Article ID: 169764
Article Last Modified on 8/25/2005
import VarEnumeration;NOTE : the file name has to have the correct capitalization.
// VarEnumeration.java
//
// Written by Gregory W Singleton
// of Microsoft Technical Support, Developer Support
// Copyright (c) 1997 Microsoft Corporation. All rights reserved.
//
// This class implements Enumeration to support Conversion from
// IEnumVARIANT.
//
// The constructor takes a reference to an existing IEnumVARIANT.
import com.ms.com.*;
import stdole2.*;
import java.util.*;
public class VarEnumeration implements Enumeration
{
private IEnumVARIANT enum = null;
private Variant var = null;
public VarEnumeration(IEnumVARIANT iev)
{
enum = iev;
// Get first Element and store it
nextElement();
}
public Object nextElement()
{
// Temporary variable to hold outgoing VARAINT
Variant retval = var;
// Create Variant to hold return from Next
Variant tempVar = new Variant();
tempVar.putEmpty();
// Create Array of ints to hold number of returned items
int[] numItems = new int[1];
numItems[0] = 0;
// Get next Item
try
{
enum.Next(1, tempVar, numItems);
}
catch(Exception ex)
{
// enum.Next throws an exception when it cannot
// return the amount of items requested
numItems[0] = 0;
}
// Check to see if numItems == 0
if(numItems[0] == 0)
var = null; // If so, store null in var
else
var = tempVar; // If not, store it in var
// Return previous VARAINT
return retval;
}
public boolean hasMoreElements()
{
if(var == null)
return false;
else
return true;
}
}
The class VarEnumeration has two public member functions that are used to
implement the Enumeration interface. The first returns a Boolean if the
collection has more elements:
public boolean hasMoreElements()The second returns the next Object from the collection:
public Object nextElement()This object will always be of type com.ms.com.Variant or null.
...
import stdole2.IEnumVARIANT;
import com.ms.com.Variant;
import java.util.Enumeration;
import VarEnumeration;
class TestEnum
{
...
public void tryTest
{
VarEnumeration varEnum = new
VarEnumeration((IEnumVARIANT)m_autoObject.get_NewEnum());
While(varEnum.hasMoreElements())
{
Variant var = (Variant)varEnum.nextElement();
System.out.println("Variant Type: " + var.getvt());
}
}
...
}
Once the above code is executed the automation objects collection is
enumerated. A println call is made for each Variant returned showing its VT
type.
Keywords: kbhowto kbcode KB169764