Search code examples
web-servicesdynamics-gp

BalanceType in Dynamics GP Web Services, How to use?


I am trying to create a Customer in GP Web Services and I came across the BalanceType property on the Customer class and I do not know how to set its value. I was expecting it to be an integer of a value 0 or 1, however I receive a "cannot implicitly convert type 'int' to [...].BalanceType".

Here is the definition for it. I believe the problem is my lack of experience with C# and .NET in general and with enumeration types specifically.

public enum BalanceType : int {

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Open Item")]
    OpenItem = 0,

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Balance Forward")]
    BalanceForward = 1,
}

In my code I have a class with a property

public int balanceType

Later in a method I have the following where _customer is my parameter object passed in and customerObj is the web services class object.

customerObj.BalanceType = _customer.balanceType;

Your time and brainpower is greatly appreciated.


Solution

  • An enumeration type provides a convenient way to define named constants with a value. In this case, OpenItem = 0 and BalanceForward = 1.

    You set an Enum like this:

    customerObj.BalanceType = BalanceType.OpenItem;
    

    I would change the property in your code to also be of BalanceType like so:

    public BalanceType balanceType;
    

    That way you avoid any need for casting between integer and your enumeration type. You will be able to set it easily:

    customerObj.BalanceType = balanceType;
    

    In case you do need to cast to the enumeration type from an integer, see this related question.