k-ujihara / NCDK

The Chemistry Development Kit ported to .NET
https://kazuyaujihara.github.io/NCDK/
GNU Lesser General Public License v2.1
35 stars 11 forks source link

Example Request for MolecularFormula Class #26

Closed HuanLab closed 3 years ago

HuanLab commented 3 years ago

Hi,

How can I generate a MolecularFormula object from a formula string (eg. C6H6). The default constructor seems to create an empty object.

Thanks,

Tao

k-ujihara commented 3 years ago

Use MolecularFormulaManipulator class like below. MolecularFormula class has only basic functionality.

using NCDK;
using NCDK.Tools.Manipulator;
using System;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            string formula = "C6H6";
            IMolecularFormula mf = MolecularFormulaManipulator.GetMolecularFormula(formula);
            double mass = MolecularFormulaManipulator.GetMass(mf, MolecularWeightTypes.MolWeight);

            Console.WriteLine($"{formula} mass = {mass}");
        }
    }
}

Above outputs below.

C6H6 mass = 78.11205990368275
HuanLab commented 3 years ago

Thank you so much, this solved my problem!