How can I programmatically get a list of variables from NiceLabel?

The following piece of code shows how you can programmatically get a list of variables from a label (.lbl) file. For this sample to work you will have to add reference to “niceengine5wr.dll” that is usually installed in the “c:\Program Files\Common Files\EuroPlus Shared\NiceEngine 5” folder. You will also have to compile your application as 32-bit (x86) application even if you want to run it on a 64-bit system, because NiceEngine and NiceLabel are 32-bit applications.

// Open NiceEngine
NiceEngine5WR.NiceApp engine = new NiceEngine5WR.NiceApp();

// Open label
NiceEngine5WR.NiceLabel label = engine.LabelOpenEx(“file.lbl”);

// List of variables where basic information about label variable will be stored.
// For this sample name, length and prompt order will be stored.
IList<Variable> variables = new List<Variable>();

if (label != null)
{
  try
  {    
    // Get a list of variables
    NiceEngine5WR.IVariableList varList = label.Variables;
    try
    {
      for (int i = 1; i <= varList.Count; i++)
      {
        NiceEngine5WR.IVar varIntf = varList.Item(i);
        if (varIntf != null)
        {
          try
          {
            // Use only prompt variables that require prompt.
            // Using other types of variables usually does not make sense.
            if (varIntf.VarType == 1 && varIntf.DefType != 2)
            {
              Variable v = new Variable();
              v.Name = varIntf.Name;
              v.Length = varIntf.Length;
              v.PromptOrder = varIntf.PromptOrder;
              variables.Add(v);
            }
          }
          finally
          {
            // Make sure that interfaces are correctly released otherwise you may get errors. 
            varIntf.Free();
            varIntf = null;
          }
        }
        else
        {
          // Show error
        }
      }
    }
    finally
    {
      varList.Free();
      varList = null;
    }
  }
  finally
  {
      // Close label by releasing the interface.
      label.Free();
      label = null;
  }
}

Variable class definition looks like this:

public class Variable
{                
  public string Name
  {
    get;
    set;
  }

  public string Value
  {
    get;
    set;
  }

  public int Length
  {
    get;
    set;
  }

  public int PromptOrder
  {
    get;
    set;
  }
}
Posted in NiceLabel, SDK Tagged with: , , , ,