Home > Software design >  how to assign value in asp.net List variable?
how to assign value in asp.net List variable?

Time:02-12

I have list variable which is filled by data fetch from db in code behind. After fetching data in list , can i replace a list variable value? here is my list

public  class SummaryInformation
{
   
    public long Id { get { return parent.Id; } }

    public string TypeCode { get { return parent.TypeCode; } }
}

Code for fetching data and replacing list value

string sTypeCode = "C"
SummaryInformation account = Account.GetSummary(long.Parse(lb.CommandArgument), currentUser, lb.Attribute("Id"));
List<SummaryInformation> accounts = new List<SummaryInformation>();

Now once the list loaded here from db data then can i replace TypeCode db value with my value in list? like below

accounts.ElementAt(1) = sTypeCode ;

i am getting error when i tried to assign list value like this.

CodePudding user response:

You can do the following to override your DB TypeCode value to your sTypeCode:

string sTypeCode = "C"
SummaryInformation account = Account.GetSummary(long.Parse(lb.CommandArgument), currentUser, lb.Attribute("Id"));
List<SummaryInformation> accounts = new List<SummaryInformation>();
account.TypeCode=sTypeCode;
//Add to your list
accounts.Add(account);

You also need to use Automatic Properties to your getter and setter:

public string TypeCode {get;set;}
  • Related