My List is
public List<BaseHomePage.QueueListItem> QueueDataSource
{
get { return this._queueDataSource; }
set { this._queueDataSource = value; }
}
and i want to remove the object from the list,my code is
for (int i = 0; i < _queueDataSource.Count; i++)
{
object queue = _queueDataSource[i];
if (objQuery.BranchOutQueue)
{
this._queueDataSource.Remove(queue); //Here I want to getting erroe
}
Error : The best overload method match for system.collections.Generic.List<baseHomePage.QueueListItem>.Remove(BaseHome.QueueList) has some invalid arguments
Replace
object queue = _queueDataSource[i];
with
var queue = _queueDataSource[i];
or even
BaseHomePage.QueueListItem queue = _queueDataSource[i];
At the moment you're discarding the information that queue is a specific type of item, but you're only allowed to remove that specific type of item from the list.
Related
List<ExShippingBillDetails> ExShippingBillDetailsList = new List<ExShippingBillDetails>();
for( int i=1; i <BillsArray.Length; i++ )
{
List<ExShippingBillDetails> ExShippingBillList = new List<ExShippingBillDetails>();
ExShippingBillList = FinalModel( ShippingBillDetailSArray, _ExShippingBillDetails );
ExShippingBillDetailsList.Add( ExShippingBillList[0] );
ExShippingBillList = new List<ExShippingBillDetails>();
}
After adding the object to list, when i change the property value of object(ExShippingBillList), Value in the list also changes.I dont want to change the value from list.
The solution is simple. As the object is added to the new list is pass by reference any change you make outside will also affect the original list. I have tried your example and solved your problem by creating a new object while adding to the list at this line.
ExShippingBillDetailsList.Add( ExShippingBillList[0] );
This is how I avoided value change.
List<ExShippingBillDetails> ExShippingBillDetailsList = new List<ExShippingBillDetails>();
List<ExShippingBillDetails> ExShippingBillList = new List<ExShippingBillDetails>();
ExShippingBillList.Add(new ExShippingBillDetails() { number = 1 });
ExShippingBillList.Add(new ExShippingBillDetails() { number = 2 });
ExShippingBillList.Add(new ExShippingBillDetails() { number = 3 });
ExShippingBillDetailsList.Add(new ExShippingBillDetails(ExShippingBillList[0]));
ExShippingBillList[0].number = 5;
//Now changing property values doesn't affect ExShippingBillDetails list
class ExShippingBillDetails
{
public int number { get; set;}
public ExShippingBillDetails()
{
}
//you need to add this constructor to copy the values
public ExShippingBillDetails(ExShippingBillDetails n)
{
number = n.number;
}
}
This should solve the problem you are facing now. Good luck :)
I have a list whose size is not fixed. In each iteration, the number of elements in the list may get decreased, increased or remain same but with different values.
In each iteration, I receive the newer list in a setter as following:
public List<int> IconsColor
{
get { return iconsColorList; }
set
{
newIconsColorList = new List<int>(value);
if (newIconsColorList.Count == iconsColorList.Count && newIconsColorList.All(iconsColorList.Contains))
return;
//Else
nIconsChanged = true;
//??????????????????????????
//?????????- How do I update Old list with New values
//Something like iconsColorList = newIconsColorList;
//but above line makes the If-condition true since both the lists are same now
}
}
How do I modify the elements of the previous list (iconsColorList) with new values (present in newIconsColorList)? And if the number of elements in the new list is greater than to that of the older list then, add the new element to the older list also.
So you want to merge both lists (update and add new):
public List<int> IconsColor
{
set
{
for (int i = 0; i < Math.Min(iconsColorList.Count, value.Count); i++)
{
if (value[i] != iconsColorList[i])
{
iconsColorList[i] = value[i];
nIconsChanged = true;
}
}
if (value.Count > iconsColorList.Count)
{
// append new items to the end of the list
iconsColorList.AddRange(value.Skip(iconsColorList.Count));
nIconsChanged = true;
}
}
}
Side-note: i hope the lack of a getter was just because it wasn't relevant. A property without a getter is not really useful and smells like fish. In this case it would just return iconsColorList;.
This is my property class:
class Actions
{
public string[] Style { get; set; }
}
and this is my main method:
Actions action = new Actions();
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
foreach (var item in list)
{
for (int i = 0; i < action.Style.Length; i++)
{
action.Style[i] = item.ToString();
Console.WriteLine(action.Style[i]);
}
}
How do I fill the property with list items?
This gives me a exception:
"object reference not set to an instance of an object".
There is no need to add your items one by one, you could just use the ToArray() method of your list like so:
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
Actions action = new Actions {
Style = list.ToArray()
};
As has already been pointed out, Style is always null, given the code you have shared. #Eldeniz and #paul have shared different ways to fix that. Obviously, your sample code is just a sample fragment, so here are 2 other options you could consider if the previous two don't work for whatever reason (I'm just free-handing this, please excuse any typos).
1) You can have your Actions class always return a not-null object
class Actions
{
private string[] _style;
public string[] Style
{
get { return _style ?? new string[0]; }
set { _style = value; }
}
}
Note that this will allow you to always see the output of the style property as requested, assuming an empty array and null are, for your purposes, the same thing.
2) You can make your loop tolerant to null values
foreach (var item in list)
{
for (int i = 0; i < action?.Style.Length ?? 0; i++)
{
action.Style[i] = item.ToString();
Console.WriteLine(action.Style[i]);
}
}
Finally, just as a tip, if you have your debugger attached and you are stepping through your code, Visual Studio will help you pinpoint these sorts of errors pretty easily. Take the time to become friends with your debugger. If it gives you an error you don't understand, do a quick web search. Your future self will thank you.
You must create an instance of the Style property
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
Actions action = new Actions();
action.Style=new string[list.Count];
foreach (var item in list)
{
for (int i = 0; i < action.Style.Length; i++)
{
action.Style[i] = item.ToString();
Console.WriteLine(action.Style[i]);
}
}
Work on entity framework vs2010,
I want to store somewhere some set of objects obtained from the database. Because I dont want to call DB after which user request. And I do it this way:
public IEnumerable<Message> Messages {
get { return HttpContext.Session[SESSION_MESSAGES_NAME] as IEnumerable<Message>; }
set { HttpContext.Session[SESSION_MESSAGES_NAME] = value; }
}
objEntity = new CmnItemSpecificationDetail();
objEntity.ItemSpecificationDetailRecordID = hidItemSpecificationDetailRecordID.Value.ToString() == "" ? _ItemSpecificationDetail.Count + 1 : int.Parse(hidItemSpecificationDetailRecordID.Value.ToString());
objEntity.SpecificationID = drpSpecification.SelectedIndex == 0 ? -1 : int.Parse(drpSpecification.SelectedValue);
objEntity.SpecValue = Convert.ToDecimal(txtSpecValue.Text);
objEntity.UOMID = drpUOM.SelectedIndex == 0 ? -1 : int.Parse(drpUOM.SelectedValue);
objEntity.UOMID = 1;
objEntity.Action = Entity.ActionMode.Add;
objEntity.Action = Entity.ActionMode.Add;
objEntity.CreateBy = LogInUser;
objEntity.CreateOn = DateTime.Now;
objEntity.CreatePc = PCName;
Messages.Append(objEntity);//failed to add item
public static class exten
{
public static IEnumerable<T> Append<T>(this IEnumerable<T> source, params T[] items)
{
return source.Concat(items);
}
}
This list failed to fill up item,want to know why this can not add item on list,what is the problem,How to solve this problem.
If have any query please ask ,thanks in advanced.
You may want to modify your Getter a bit.
The way it currently works it doesn't handle situations where the Messages is null (hasn't been initialized).
Change this
get { return HttpContext.Session[SESSION_MESSAGES_NAME] as IEnumerable<Message>; }
To this.
get { var msg = HttpContext.Session[SESSION_MESSAGES_NAME] as IEnumerable<Message>;
if(msg == null) {
msg = new List<Message>();
HttpContext.Session[SESSION_MESSAGES_NAME] = msg;
}
return msg;
}
Your Append() extension method does NOT change the original sequence. It returns a new sequence with the specified items appended to it.
When you call Messages.Append(objEntity), you don't do anything with the return value, so you are throwing away the results of the Append().
To fix it, you will need to do something with the return value. Perhaps just do:
Messages = Messages.Append(objEntity);
I have this code:
if (pertinentDataExists) {
List<Entity> operatorList = new List<Operator>();
List<Entity> newOperatorList = criteria.newOperatorList;
foreach (Operator operator in operatorList)
{
operatorList.Add(operator);
}
queryCriteria.CreateCriteria("OperatorList").Add(Restrictions.In("Operator", operatorList));
}
and then later:
if (otherPertinentDataExists) {
Object[] objects = new Object[criteria.newRoleTypeList.Count];
for (int = 0; i < criteria.RoleTypeList.Count; i++) {
objects[i] = criteria.RoleTypeList[i].Id;
}
queryCriteria.CreateCriteria("OperatorList").Add(Restrictions.In("Role", objects));
}
These work separately just fine. However, if it is the case that both cases have criteria to add to the list, then I get:
NHibernate.QueryException Message : duplicate association path:
OperatorList
How do I add restrictions for Role if the criteria is already created?
There are two options. The first, if possible (being in the same method), create Criteria and reference them as a local variable:
var operatorListCriteria = queryCriteria.CreateCriteria("OperatorList");
if ( myCondition1 )
{
operatorListCriteria.Add(...
}
if ( myCondition2 )
{
operatorListCriteria.Add(...
}
The second, create criteria with "alias" and then, later, anywhere, you can find them with GetCriteriaByAlias(aliasPath):
var aliasPath = "OperatorListAlias";
var operatorListCriteria = queryCriteria.CreateCriteria("OperatorList", aliasPath);
...
// later find them by aliasPath
var subCriteria = queryCriteria.GetCriteriaByAlias(aliasPath);
NOTE: Also check the GetCriteriaByPath(associationPath)