Here is a snippet of the code that is causing the error
if(items[0] != null)
{
equippedItem = items[0];
}
Here is a snippet of the items list:
public List<InventoryItem> items = new List<InventoryItem>();
Here is a snippet of the
public InventoryItem equippedItem;
I would change this:
if(items[0] != null)
{
equippedItem = items[0];
}
To this:
if(items.Any() && items[0] != null)
{
equippedItem = items[0];
}
This will check if the list is initialized and has items. If it does have items, you can get the first item from the list.
And if you really want to get the first item from that list at all times, maybe use something like this:
InventoryItem equippedItem = items.FirstOrDefault();
if(equippedItem != null)
{
// Add some cool code
}
The FirstOrDefault gets the first item from a list. If there isn't a first item, the result will be the default value of the type (int = 0, bool = false, InventoryItem = null). You could also use First(), but that would throw an exception if there isn't a first (index 0).
I would try to avoid indexes as much as possible, because it's very sensitive for errors.
I actually found specifically that if you do this it works:
if (items.Count != 0)
{
if(items[0] != null)
{
equippedItem = items[0];
}
}
Related
To change the Content-Control Text in the Document Body I'm doing this:
var elements = doc.MainDocumentPart.Document
.Descendants<SdtElement>()
.Where(s => s.SdtProperties.ChildElements.Count > 0 &&
s.SdtProperties.GetFirstChild<Tag>()?.Val == contentControlTag);
foreach (var element in elements)
{
if (element == null)
{
continue;
}
var elementText = element.Descendants<Text>();
if (elementText != null)
{
var elementTextValue = elementText.FirstOrDefault();
if (elementTextValue != null)
{
elementTextValue.Text = text;
}
elementText.Skip(1).ToList().ForEach(t => t.Remove());
}
}
Sadly this doesn't change the Header/Footer part.
I want to check each page's header/Footer part for a specific Content-Control-Tag and change its text similar to the code above. How can I achieve this?
The headers and footers aren't in the MainDocumentPart.Document, they are subparts in the HeaderParts and FooterParts collections off the MainDocumentPart. So you'll need to process each part separately. Fortunately, all of the parts share a common base class, OpenXmlPart, so you can put them together into a list and process them in a loop. Since you'll be dealing with the base class, you'll need to use RootElement property instead of Document as a starting point to get the Descendants.
So the end result would look something like this:
var partsToUpdate = new List<OpenXmlPart> { doc.MainDocumentPart }
.Concat(doc.MainDocumentPart.HeaderParts)
.Concat(doc.MainDocumentPart.FooterParts);
foreach (var part in partsToUpdate)
{
var elements = part.RootElement
.Descendants<SdtElement>()
.Where(s => s.SdtProperties.ChildElements.Count > 0 &&
s.SdtProperties.GetFirstChild<Tag>()?.Val == contentControlTag);
foreach (var element in elements)
{
if (element == null)
{
continue;
}
var elementText = element.Descendants<Text>();
if (elementText != null)
{
var elementTextValue = elementText.FirstOrDefault();
if (elementTextValue != null)
{
elementTextValue.Text = text;
}
elementText.Skip(1).ToList().ForEach(t => t.Remove());
}
}
}
I'm trying to iterate through my RadGridView rows, but when I have more than 20 or 30 items, the loop doesn't get all rows.
For example: using this code in a radgridview with 5 items, I can get all of them and do whatever I want, but when my grid has more than 20 items, it gets only 10 rows. Is this a bug or something like that? How can I solve it?
Here's my code:
private List<object> ReturnListFounds(string text)
{
List<object> a = new List<object>();
foreach (var item in myGrid.Items)
{
if (item == null)
continue;
GridViewRow row = myGrid.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;
if (row == null)
continue;
foreach (GridViewCell cell in row.Cells)
{
if (cell != null && cell.Value != null)
{
string str = cell.Value.ToString();
if (str.Equals(text, StringComparison.InvariantCultureIgnoreCase) || str.ToLower().Contains(text.ToLower()))
{
a.Add(row.Item);
break;
}
}
}
}
return a;
}
#Edit
I found out the problem. The thing is: the method "ItemContainerGenerator.ContainerFromItem(item) as GridViewRow" returns null if the item is outside of the view area. But I'm using this method in a grid containing 123 items and I can only get the row for the 20 first items.
I need to be able to get all of the items, not just the ones in the view area. I have already tried to set the virtualization false (EnableRowVirtualization = false; EnableColumnVirtualization = false;), but it didin't work as well.
Is there a way of getting all of the rows using this method?
Have you tried this?
var rows = StrategyGridView.ChildrenOfType<GridViewRow>();
It works fine for me. Hope it helps!
I tried a lot of things to make this work and I found one. It's not the best way of doing this, but it works. I anyone has anything better, just post here! Share with us!
private List<object> ReturnListFounds(string text)
{
List<object> result = new List<object>();
for (int l = 0; l <= Items.Count; l++)
{
var cell = new GridViewCellInfo(this.Items[l], this.Columns[0], this);
if (cell.Item != null)
{
var props = cell.Item.GetType().GetProperties();
foreach (var p in props)
{
if (p == null || cell.Item == null)
continue;
var t = p.GetValue(cell.Item);
if (t == null)
continue;
var str = t.ToString();
if (str.Equals(text, StringComparison.InvariantCultureIgnoreCase) || str.ToLower().Contains(text))
{
result.Add(cell.Item);
}
}
}
}
result = new List<object>(result.Distinct());
return result;
}
I know you can't modify a collection during a foreach, but I should be able to set variable values of the underlying iterator through it. For some reason the method below, every time it executes is giving be the "Collection was modified..." error:
private static IInstrument AdjustForSimpleInstrument(DateTime valueDate, IInstrument temp)
{
var instr = temp;
foreach (var component in instr.Components)
{
component.Schedule.ScheduleRows.RemoveAll(
sr =>
((sr.Payment != null) && (sr.Payment.PaymentDate != null) &&
(sr.Payment.PaymentDate.AdjustedDate.Date <= valueDate.Date)));
if (
!component.ScheduleInputs.ScheduleType.In(ComponentType.Floating, ComponentType.FloatingLeg,
ComponentType.Cap, ComponentType.Floor)) continue;
foreach (var row in component.Schedule.ScheduleRows)
{
var clearRate = false;
if (row.Payment.CompoundingPeriods != null)
{
if (row.Payment.CompoundingPeriods.Count > 0)
{
foreach (
var period in
row.Payment.CompoundingPeriods.Where(
period => ((FloatingRate)period.Rate).ResetDate.FixingDate > valueDate))
{
period.Rate.IndexRate = null;
clearRate = true;
}
}
}
else if (row.Payment.PaymentRate is FloatingRate)
{
if (((FloatingRate)row.Payment.PaymentRate).ResetDate.FixingDate > valueDate)
clearRate = true;
}
else if (row.Payment.PaymentRate is MultipleResetRate)
{
if (
((MultipleResetRate)row.Payment.PaymentRate).ChildRates.Any(
rate => rate.ResetDate.FixingDate > valueDate))
{
clearRate = true;
}
}
if (clearRate)
{
row.Payment.PaymentRate.IndexRate = null;
}
}
}
return temp;
}
Am I just missing something easy here? The loop that is causing the exception is the second, this one:
foreach (var row in component.Schedule.ScheduleRows)
I suspect this is not .NET-framework stuff, so I assume that row is connected to its collection. Modifying the contents of the row, might shift its place inside its collection, thus modifying the collection, which is not allowed during some foreach-operations.
The solution is simple: create a copy of the collection (by using LINQ).
foreach (var row in component.Schedule.ScheduleRows.ToList())
...
I'm trying to select only those rows which have Parent ID = 0
int predecessor = Parent;
StringBuilder valuePath = new StringBuilder();
valuePath.Append(Parent.ToString());
DataRow[] drPar;
while (true)
{
drPar = dt.Select("MenuID=" + predecessor);
if (drPar != null)
{
if (drPar[0]["ParentID"].ToString().Equals("0"))
break;
}
drPar[0]["ParentID"].ToString().Equals("0") is giving me
Out of Bound exception ..
Help Please !
DataTable.Select does not return null when there's no matching DataRow but an array with Length==0.
But apart from that, why are you using an "infinite" loop for only one statement?
So this should work:
drPar = dt.Select("MenuID=" + predecessor);
if (drPar.Length != 0)
{
if (drPar[0]["ParentID"].ToString().Equals("0"))
{
// ...
}
}
The array drPar must be empty to give this error as it is the only index you use in your code.
Try
if (drPar != null && drPar.Length > 0)
I want to write a check for some conditions without having to use try/catch and I want to avoid the possibilities of getting Index Out of Range errors
if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's at least one Object array that has values
{
if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty
{
// execute code here
}
}
So the problem I am facing is that in the second check I need to see whether I have one Item that is not empty. However, If I don't have Element[1], I get the Index Out of Range exception. The problem is that there could be 2 Elements and one(or both) of them may have empty Object arrays. The code will have to be executed only if one of thos Item strings is not empty.
Hopefully, I explained it well. How do I go about avoiding getting that exception under any condition?
Alright, you need some better null checking and some more cautious code here.
if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's at least one Object array that has values
{
if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty
{
// execute code here
}
}
is just unacceptable.
First, let's null check
if (array != null)
{
if (array.Element != null)
for simplicity, you could use &&
if (array != null && array.Element != null)
then, inside that if, we use a for loop (since you're stuck on arrays) and null check it
for (int i = 0; i < array.Element; ++i)
{
if (array.Element[i] != null && array.Element[i].Object != null)
{
then, since you have nested arrays, we loop again. This is called a nested loop, and it's generally bad practice, I'll show you why it works in a second.
for (int o = 0; o < array.Element[i].Object.length; ++o)
{
if (array.Element[i].Object[o] != null && !string.IsNullOrEmpty(array.Element[i].Object[o].Item))
{
Now, with all of that ugly nested loopyness, we've found out that your Item is not null.
On top of that, you have access to ALL of the potential values here, and can group them as you like. Here's how I would put the whole thing together for simplification.
List<string> arrayValues = new List<string>();
if (array != null && array.Element != null)
{
for (int i = 0; i < array.Element.length; ++i)
{
//bool found = false;
if (array.Element[i] != null && array.Element[i].Object != null)
{
for (int o = 0; o < array.Element[i].Object.length; ++o)
{
if (array.Element[i].Object[o] != null && !string.IsNullOrEmpty(array.Element[i].Object[o].Item))
{
arrayValues.Add(array.Element[i].Object[o].Item);
//if you want to drop out here, you put a boolean in the bottom loop and break and then break out of the bottom loop if true
//found = true;
//break;
}
}
}
//if (found)
// break;
}
}
if (arrayValues.Count > 0)
{
//do stuff with arrayValues
}
Could use a LINQ method ElementAtOrDefault(index)
so if the element is not found it will be null.
var currentElem = Elems.ElementAtOrDefault(i);
if(currentElem != null)
// do something
else
// do something
Could you do something like:
if(array.Element[0] != null || array.Element[1] != null){
//execute code
}
Your code is probably subject to refactor.
I assume it can work this way:
var obj = array.Element.FirstOrDefault(x => x.Object.Length > 0);
if (obj != null)
{
if (obj.Object[0].Item.Length != 0)
{
// do whatever is necessary
}
}
I think you can put your check in right before your first if Check.
if (array.Length > 1 && array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's at least one Object array that has values
{
if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty
{
// execute code here
}
}
This should just short-circuit out if your array doesn't have both Elements.
Place both tests together using the short-circuit && so that the second test doesn't occur if the first fails:
object element0 = array.Element[0].Object;
object element1 = array.Element[1].Object;
// Ensure at least one Object array has a non-empty value.
if ((element0.Length > 0 && !string.IsNullOrEmpty((string)element0.Object[0].Item))
|| (element1.Length > 0 && !string.IsNullOrEmpty((string)element1.Object[1].Item)))
{
// ...
}
I am presuming it is Object[1] causing the exception--you weren't clear on that. If it is Element[1] that causes the exception (or both), then you need to pre-test the length of the array:
if ((array.Element[0].Length != 0 && HasValue(array.Element[0].Object))
|| (array.Element[1].Length > 1 && HasValue(array.Element[1].Object)))
{
// ...
}
// <summary>
// Returns true if the specified string array contains a non-empty value at
// the specified index.
// </summary>
private bool HasValue(System.Array array, int index)
{
return array.Length > index &&
!string.IsNullOrEmpty((string)array.Object[index].Item);
}
for (long i = 0; i <= (output3.Length); i++)
{
output1.WriteByte(output3[i]); -----> index out of range exception correct it
output1.WriteByte(output3rx[i]);
}