A new identity is derived which relates the gain and the field distribution (or confinement factor) in a dielectric waveguide with complex refractive indices. This identity is valid for any guided mode of waveguides with an arbitrary cross section. It provides a new check of the accuracy of mode solvers. Also, it can be used in a variational approach to predict the gain or loss of a guided mode based on knowledge of confinement factors. It is shown that a previous analysis that is often used, is not correct. In addition, approximate expressions for the gain in slab waveguides are presented.