Musketeer Musketeer - 1 month ago 7
C# Question

Why are my guids being made null when added to a list

For some reason when I added my entity to a list the guids are being made null ie empty and I do not no why I step through with the debugger and hit _asset.id it it full of a guid.

I have made sure that all the asset is filled as it should be but do not no why when i get to return _asset that it becomes null

public List<Asset> getAllSavingsForSlate()
{


List<tbl_OtherAssets> _otherAssets = new List<tbl_OtherAssets>();
Asset _asset = new Asset();
List<Asset> _assets = new List<Asset>();

_otherAssets = dal.GetAssetsByCaseId(caseid);


foreach (var otherassets in _otherAssets)
{

foreach (var otherassets in _otherAssets)
{

if (otherassets.assetTitle == new Guid(slateExportsConstants.savingsAssetType))
{

_asset.Id = otherassets.Asset_Id;
_asset.Description = otherassets.description;
_asset.CurrentValue = (int)Math.Round(Convert.ToDouble(otherassets.value));
_asset.OutstandingFinance = 0;
_asset.FinanceAccountHolder = (int) slateExportsConstants.Applicant.Applicant1;
_asset.FinanceMonthlyPayment = 0;
_asset.FinanceEndDate = null;
_asset.Home = false;
_asset.AssetType = (int)slateExportsConstants.AsssetTypes.Savings;
_asset.FixedPeriodEndDate = null;
_asset.ValuationSource = 0;
_asset.IsSharedOwnership = false;
_asset.HousingAssociation = null;
_asset.HousingAssociationShareOfPropertyPercent = 0;
_asset.BeneficialInterest = false;
_asset.JointOwner1 =null;
_asset.JointOwner2 = null;
_asset.JointOwner3 = null;
_asset.RentalIncome = 0;
_asset.AccountNumber = "Test12334";
_asset.PropertySecuredAgainstId = null;
_asset.FinanceCompany = dal.GetCreditorName(otherassets.nameOfLender);




}

_assets.Add(_asset);

}
return _assets;
}

Answer

You are adding an empty object, keey the Add(_asset); and create instance inside the loop.

 if (otherassets.assetTitle == new Guid(slateExportsConstants.savingsAssetType))
            {
                Asset _asset = new Asset();
                _asset.Id = otherassets.Asset_Id;
                _asset.Description = otherassets.description;
                _asset.CurrentValue = (int)Math.Round(Convert.ToDouble(otherassets.value));
               _asset.OutstandingFinance = 0;
                _asset.FinanceAccountHolder = (int) slateExportsConstants.Applicant.Applicant1;
                _asset.FinanceMonthlyPayment = 0;
                _asset.FinanceEndDate = null;
                _asset.Home = false;
                _asset.AssetType = (int)slateExportsConstants.AsssetTypes.Savings;
                _asset.FixedPeriodEndDate = null;
                _asset.ValuationSource = 0;
                _asset.IsSharedOwnership = false;
                _asset.HousingAssociation = null;
                _asset.HousingAssociationShareOfPropertyPercent = 0;
                _asset.BeneficialInterest = false;
                _asset.JointOwner1 =null;
                _asset.JointOwner2 = null;
                _asset.JointOwner3 = null;
                _asset.RentalIncome = 0;
                _asset.AccountNumber = "Test12334";
                _asset.PropertySecuredAgainstId = null;
                _asset.FinanceCompany = dal.GetCreditorName(otherassets.nameOfLender);
                _assets.Add(_asset);
            }