I am accepting a form from the user with a bunch of inputs and I am constructing a string from those inputs. I am trimming each input and 2 of the inputs (GID & Clone) are not required fields so I'm doing null checks before building the string to avoid null object errors.
I wrote this method and it works, but I was wondering if there is a less repetitive way of doing this.
private string ConstructSeedReference(SeedViewModel model)
{
string SeedRefId;
if (!String.IsNullOrEmpty(model.Seed.GID) && !String.IsNullOrEmpty(model.Seed.Clone))
{
SeedRefId = String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}",
model.Customer.CustomerCode.TrimEnd(), model.Seed.CollectionCodeId.TrimEnd(), model.Seed.SpeciesId.TrimEnd(),
model.Seed.Zone.TrimEnd(), model.Seed.Elevation.TrimEnd(), model.Seed.ColYear.TrimEnd(), model.Seed.OrchardId, model.Seed.GID.TrimEnd(), model.Seed.Clone.TrimEnd());
return SeedRefId;
}
else if (!String.IsNullOrEmpty(model.Seed.GID) && String.IsNullOrEmpty(model.Seed.Clone))
{
SeedRefId = String.Format("{0}{1}{2}{3}{4}{5}{6}{7}",
model.Customer.CustomerCode.TrimEnd(), model.Seed.CollectionCodeId.TrimEnd(), model.Seed.SpeciesId.TrimEnd(),
model.Seed.Zone.TrimEnd(), model.Seed.Elevation.TrimEnd(), model.Seed.ColYear.TrimEnd(), model.Seed.OrchardId, model.Seed.GID.TrimEnd());
return SeedRefId;
}
else if (String.IsNullOrEmpty(model.Seed.GID) && !String.IsNullOrEmpty(model.Seed.Clone))
{
SeedRefId = String.Format("{0}{1}{2}{3}{4}{5}{6}{7}",
model.Customer.CustomerCode.TrimEnd(), model.Seed.CollectionCodeId.TrimEnd(), model.Seed.SpeciesId.TrimEnd(),
model.Seed.Zone.TrimEnd(), model.Seed.Elevation.TrimEnd(), model.Seed.ColYear.TrimEnd(), model.Seed.OrchardId, model.Seed.Clone.TrimEnd());
return SeedRefId;
}
else
{
SeedRefId = String.Format("{0}{1}{2}{3}{4}{5}{6}",
model.Customer.CustomerCode.TrimEnd(), model.Seed.CollectionCodeId.TrimEnd(), model.Seed.SpeciesId.TrimEnd(),
model.Seed.Zone.TrimEnd(), model.Seed.Elevation.TrimEnd(), model.Seed.ColYear.TrimEnd(), model.Seed.OrchardId);
return SeedRefId;
}
}