Multithread, Linq to Sql, ConcurrentDictonary Fails Remove - c#

I have a ConcurrentDictionary of Attributes for products. These attributes have the product ID and values for the names of the attribute and any options the attribute has. I have this ConcurrentDictionary because I have threads that are created to handle each attribute in the dictionary by attribute name.
if (knownAttribute.AttributeType.Value.Equals("Product Specification"))
{
Console.WriteLine("Started a thread for: " + knownAttribute.AttributeTypeId + ", " + knownAttribute.Value);
while (true)
{
/* if (AS400SpecificationAttributes.IsEmpty && knownSpecificationBag.IsEmpty && gatherRowsTasks.All(x => x.IsCompleted))
break;*/
AS400SpecificationAttribute AS400SpecificationAttributeWork = null;
AS400SpecificationAttributeWork = knownSpecificationBag.Keys.FirstOrDefault(x => x.AttributeName == knownAttribute.Value);
if (AS400SpecificationAttributeWork != null)
{
var product = ctx.Products.FirstOrDefault(x => x.ProductNumber == AS400SpecificationAttributeWork.ProductNumber);
if (product == null)
continue;
var productAttribute = new ProductAttribute();
productAttribute.Attribute = knownAttribute;
if (AS400SpecificationAttributeWork.AttributeValue != null)
{
var knownAttributeOption = ctx.AttributeOptions.FirstOrDefault(x => x.Attribute.Equals(knownAttribute) && x.Value.Equals(AS400SpecificationAttributeWork.AttributeValue));
if (knownAttributeOption == null)
{
knownAttributeOption = new AttributeOption();
knownAttributeOption.Value = AS400SpecificationAttributeWork.AttributeValue;
knownAttributeOption.Attribute = knownAttribute;
ctx.AttributeOptions.InsertOnSubmit(knownAttributeOption);
ctx.SubmitChanges();
}
productAttribute.AttributeOption = knownAttributeOption;
productAttribute.AttributeOptionId = knownAttributeOption.Id;
}
product.ProductAttributes.Add(productAttribute);
ctx.SubmitChanges();
string tmpstr = null;
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr))
Thread.Sleep(50);
}
else
{
if (tryCounter < 5)
{
tryCounter++;
Thread.Sleep(1000);
Console.WriteLine("Thread waiting for work: Product Specification:" + knownAttribute.Value);
continue;
}
else
{
int outVal;
threadTracker.TryRemove("Product Specification:" + knownAttribute.Value, out outVal);
Console.WriteLine("Closing Thread: Product Specification:" + knownAttribute.Value);
break;
}
}
Thread.Sleep(50);
}
It seems like the following Attribute element refuses to be removed.
I don't understand why. If i put it in a while(!dic.tryRemove(ele)) it will forever be stuck and never move from there.
There may be an error somewhere within the thread but I have no idea why.

This statement
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr))
will always return true or false. It won't block. That's the behavior of ConcurrentDictionary. It will return false if the key is not in the dictionary.
If you're looping while that method returns false and it's stuck, that means that the item isn't in the dictionary when the loop begins. Either it either was never in the dictionary or that another thread already removed it.
Is your intention to loop until the item is not in the dictionary?
You could try this:
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr)
&& !knownSpecificationBag.ContainsKey(AS400SpecificationAttributeWork))

Implement proper equals and gethashcode when using TryRemove
public override int GetHashCode()
{
return new { this.name, this.value, this.group, this.productNumber }.GetHashCode();
}
public bool Equals(AS400SpecificationAttribute other)
{
if (other == null)
return false;
return (this.ProductNumber.Equals(other.productNumber) && ((this.group != null && this.group.Equals(other.AttributeGroup)) || (this.group == null && other.AttributeGroup == null)) && ((this.name!= null && this.name.Equals(other.AttributeName)) || (this.name == null && other.AttributeName == null)) && ((this.value != null && this.value.ToUpper().Equals(other.AttributeValue.ToUpper())) || (this.value == null && other.AttributeValue == null)));
}

Related

If statement failing due to unrelated change to another IF statement

When the check against the emailaddress is added to the if statement, as per below, the if statement if (origin.Equals(true)).
bool origin = false;
Contact contact = item as Contact;
foreach (Item subItem in contactItems)
{
Contact subcontact = subItem as Contact;
if ((contact.DisplayName.Equals(subcontact.DisplayName) || (contact.DisplayName is null && subcontact.DisplayName is null)) && ((contact.CompanyName is null && subcontact.CompanyName is null) || (contact.CompanyName.Equals(subcontact.CompanyName)) && ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])))))
{
if (origin.Equals(true))
{
try
{
Console.WriteLine(contact.DisplayName + " " + subcontact.DisplayName);
Console.WriteLine(contact.EmailAddresses[EmailAddressKey.EmailAddress1]);
subcontact.Delete(DeleteMode.HardDelete);
}
catch
{
Console.WriteLine("Cannot delete" + " " + subcontact.DisplayName);
}
}
origin = true;
}
}
If I remove the if (origin.Equals(true)) or removed && ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])) from the other if statement, the contact goes through the try-catch block.
Can anyone see why?
if ((contact.DisplayName.Equals(subcontact.DisplayName) || (contact.DisplayName is null && subcontact.DisplayName is null)) && ((contact.CompanyName is null && subcontact.CompanyName is null) || (contact.CompanyName.Equals(subcontact.CompanyName)) && ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])))))
should be:
if ((contact.DisplayName.Equals(subcontact.DisplayName) || (contact.DisplayName is null && subcontact.DisplayName is null)
&& ((contact.CompanyName is null && subcontact.CompanyName is null) || (contact.CompanyName.Equals(subcontact.CompanyName))
&& ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])))
the problem as I can see, is in the amount of ( ) you use, which I believe is incorrect.
Try to clean up your if statements to prevent these types of mistakes from happening, since this one was pretty long, and a few of your parenthesis are unnecessary
EDIT:
I tried running some code and it works. I did make some mistakes in the sample of the IF statement I mentioned above.. Maybe try copying my code, and substituting your specific code into it. It should work this way.
public static void Main()
{
bool origin = false;
int[] t = {0,4,6,8,5,6,4,5};
foreach(int i in t){
if ((true || (true && true)) // if ((contact.DisplayName.Equals(subcontact.DisplayName) || (contact.DisplayName is null && subcontact.DisplayName is null))
&& ((true && true) || (true)) // && ((contact.CompanyName is null && subcontact.CompanyName is null) || (contact.CompanyName.Equals(subcontact.CompanyName)))
&& ((true && true) || (true))) // && ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])))))
{
if (origin.Equals(true))
{
try
{
Console.WriteLine("here");
}
catch
{
Console.WriteLine("Cannot delete" );
}
}
origin = true;
}
}
}
I suggest a little Helper:
private bool EqualOrBothNull( string fromContact, string fromSubcontact )
{
if ( fromContact == null && fromSubcontact == null ) return true;
if ( fromContact != null && fromContact.Equals(fromSubcontact) ) return true;
return false;
}
then you can use it like this:
if ( EqualOrBothNull( contact.DisplayName, subContact.DisplayName ) &&
EqualOrBothNull( contact.CompanyName, subContact.CompanyName ) &&
EqualOrBothNull( contact.EmailAddresses[EmailAddressKey.EmailAddress1],
subContact.EmailAddresses[EmailAddressKey.EmailAddress1])
)
{
// ...
}
This increases readability and will make it easier to find the bug if any.
Thanks for all the potential answers unfortunately they didn't fix the issue, the code is much easier to read though! In the end I separated the email address check to another IF statement which seems to be working

Getting rid of unnecessary loops

In my game I'm going to have a lot of interactions in which I'll need to see if a player has an item, and if he does and something else is true, then do an action. Described in the following code.
private void SetTinderInPit()
{
MouseState currentMouseState = Mouse.GetState();
if (player.NextToFirePit == true)
{
foreach (Item item in player.PlayerInventory.Items)
{
if (item.ItemName == "tinder")
{
foreach (Item pit in allItemsOnGround)
{
if (pit.ItemName == "firepit" &&
pit.ItemRectangle.Contains(MouseWorldPosition) &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
{
item.ItemName = "empty";
pit.ItemName = "firepitwithtinder";
pit.Texture = Content.Load<Texture2D>("firepitwithtinder");
}
}
}
}
oldMouseState = currentMouseState;
}
}
As you can see, this is ugly to look at and I think that there would be a better way to do this, but I'm not sure how. Since there will be a lot of these types of methods, I'm wondering what would be the best way to accomplish this?
Seems like you could get rid of (actually hide) the loops altogether by using some LINQ:
private void SetTinderInPit()
{
MouseState currentMouseState = Mouse.GetState();
if (player.NextToFirePit)
{
Item tinder = player.PlayerInventory.Items.FirstOrDefault(i => i.ItemName == "tinder");
if (tinder != null)
{
Item firepit = allItemsOnGround.FirstOrDefault(i => i.ItemName == "firepit" && i.ItemRectangle.Contains(MouseWorldPosition));
if (firepit != null &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
{
tinder.ItemName = "empty";
firepit.ItemName = "firepitwithtinder";
firepit.Texture = Content.Load<Texture2D>("firepitwithtinder");
}
}
oldMouseState = currentMouseState;
}
}
This has the added advantage of short-circuiting the loop when the item is found. It also makes it easy to check against things other than the name (like an "IsFlammable" or "CanContainFire" property) so you could use multiple items instead of just "tinder" and "firepit".
If you actually intended to remove all firepits and tinder, use:
private void SetTinderInPit()
{
MouseState currentMouseState = Mouse.GetState();
if (player.NextToFirePit)
{
foreach (Item tinder in player.PlayerInventory.Items.Where(i => i.ItemName == "tinder")
{
foreach (Item firepit in allItemsOnGround.Where(i => i.ItemName == "firepit"))
{
if (firepit.ItemRectangle.Contains(MouseWorldPosition) &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
{
tinder.ItemName = "empty";
firepit.ItemName = "firepitwithtinder";
firepit.Texture = Content.Load<Texture2D>("firepitwithtinder");
}
}
}
oldMouseState = currentMouseState;
}
}
Quick caveat; this code will remove all firepits with the first tinder, leaving the other tinders unscathed. I could unravel the loops to remove everything, but this function matches the provided one; and besides, I'm assuming thats not the intended behavior.
Note you do not need ToList anywhere because you are not modifying the collection during enumeration. You can always modify the items in the collection, proved with the following test:
class IntWrapper
{
public int value;
public IntWrapper(int value)
{
this.value = value;
}
}
class Program
{
static void Main(string[] args)
{
List<IntWrapper> test = new List<IntWrapper>() { new IntWrapper(1), new IntWrapper(2), new IntWrapper(3), new IntWrapper(4), new IntWrapper(5) };
foreach (IntWrapper i in test.Where(i => i.value == 1))
{
i.value = 0;
}
foreach (IntWrapper i in test)
{
Console.WriteLine(i.value);
}
Console.ReadLine();
}
}
The only real change I would make to your existing code would be to move the check for mouse state early on, to avoid checking this multiple times in your loops. In addition I would use Linq to shorten the conditions (by removing the 'if' statements):
MouseState currentMouseState = Mouse.GetState();
// I would get all the conditional checks out of the way up front first
if (player.NextToFirePit &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
{
foreach (var tinderItem in player.PlayerInventory.Items
.Where(item => item.ItemName == "tinder"))
{
foreach (var firePit in allItemsOnGround
.Where(item => item.ItemName == "firepit" &&
item.ItemRectangle.Contains(MouseWorldPosition)))
{
tinderItem.ItemName = "empty";
firePit.ItemName = "firepitwithtinder";
firePit.Texture = Content.Load<Texture2D>("firepitwithtinder");
}
}
}
oldMouseState = currentMouseState;
An alternate idea, since you were looking for a way to get rid of the 'ugly' code, would be to move some of this functionality to the player object.
I would probably use LINQ more.
Note that this is written in Notepad, not visual studio so is more of a pseudo-code.
private void SetTinderInPit()
{
var currentMouseState = Mouse.GetState();
if (!player.NextToFirePit) return;
player.PlayerInventory.Items.Where(item => item.ItemName == "tinder").ToList().ForEach(item =>
{
allItemsOnGround.Where(x => x.ItemName == "firepit" &&
x.ItemRectangle.Contains(MouseWorldPosition) &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
.ToList().ForEach(pit =>
{
item.ItemName = "empty";
pit.ItemName = "firepitwithtinder";
pit.Texture = Content.Load<Texture2D>("firepitwithtinder");
});
});
oldMouseState = currentMouseState;
}

how to iterate collection and change value

I know this is a dumb question because you cannot modify the loop collection while in a loop, but I do need to change it. I know I must not change the referenced objects, but I have no idea how to do this.
var orders = _orderService.GetOrders(o => !o.Deleted &&
o.OrderStatus != OrderStatus.Cancelled &&
o.OrderStatus != OrderStatus.Complete);
foreach (var order in orders)
{
if (order.PaymentStatus == PaymentStatus.Paid)
{
if (order.ShippingStatus == ShippingStatus.ShippingNotRequired || order.ShippingStatus == ShippingStatus.Delivered)
{
var tempOrder = _orderService.GetOrderById(order.Id);
SetOrderStatus(tempOrder , OrderStatus.Complete, true);
}
}
}
I always get an error.
UPDATED: I changed to this
var orders = _orderService.GetOrders(o => !o.Deleted &&
o.OrderStatus != OrderStatus.Cancelled && o.OrderStatus != OrderStatus.CompletE);
List<int> orderIndex = new List<int>();
orders.ToList().ForEach(x => orderIndex.Add(x.Id));
foreach(var index in orderIndex)
{
var order = _orderService.GetOrderById(index);
if (order.PaymentStatus == PaymentStatus.Paid)
{
if (order.ShippingStatus == ShippingStatus.ShippingNotRequired || order.ShippingStatus == ShippingStatus.Delivered)
{
SetOrderStatus(order, OrderStatus.Complete, true);
}
}
}
try
int count = orders.Count; // the length of the collect : may need a different method for different collection types.
for(int i = 0; i < count; i++)
{
var current = orders[i];
// do stuff with current.
}
use for loop instead of foreach loop
for(int i=0; i<orders.Count; i++)
{
if (orders[i].PaymentStatus == PaymentStatus.Paid)
{
if (orders[i].ShippingStatus == ShippingStatus.ShippingNotRequired || orders[i].ShippingStatus == ShippingStatus.Delivered)
{
var tempOrder = _orderService.GetOrderById(orders[i].Id);
SetOrderStatus(tempOrder , OrderStatus.Complete, true);
}
}
}

Nullable Property throwing NullReferenceException on .HasValue

This line of (C#) code
if (!currentLap.S1.HasValue)
is giving me
System.NullReferenceException: Object reference not set to an instance of an object.
provided I'm sure that currentLap variable is instantiated (because it's being used a few lines before and it is a local variable) and it has following property:
private double? _s1;
[DefaultValue(null)]
[JsonConverter(typeof(ShortDoubleConverter))]
public double? S1
{
get { return _s1; }
set { _s1 = value; }
}
how can it possibly throw NullReferenceException? Can it be something to do with optimization on Release mode?
Thanks,
Stevo
EDIT:
here is full method code.
public void Update(DriverData driverData)
{
LapInfo currentLap = this.CurrentLap;
if (currentLap != null &&
this.LastDriverData != null &&
driverData.TotalLaps != this.LastDriverData.TotalLaps &&
driverData.InPits &&
driverData.Speed < 10 &&
!this.LastDriverData.InPits)
{
currentLap.Escaped = true;
}
this.LastDriverData = driverData;
if ((currentLap == null || currentLap.Lap != driverData.LapNumber) &&
!this.Laps.TryGetValue(driverData.LapNumber, out currentLap))
{
currentLap = new LapInfo() { Lap = driverData.LapNumber, Parent = this, Class = driverData.Class };
this.Laps.Add(driverData.LapNumber, currentLap);
int lapsCount = 0, completedDriverLaps = 0, cleanLaps = 0;
this.TotalLaps = driverData.TotalLaps;
//if it's not the first lap
if (driverData.TotalLaps > 0)
{
//previous lap
if (this.CurrentLap == null || !this.CurrentLap.Escaped)
{
this.CompletedLaps++;
if (this.CurrentLap == null || !this.CurrentLap.MaxIncident.HasValue)
this.CleanLaps++;
}
}
foreach (DriverLapsInfo laps in this.Parent.LapsByVehicle.Values)
{
lapsCount += laps.TotalLaps;
completedDriverLaps += laps.CompletedLaps;
cleanLaps += laps.CleanLaps;
}
this.Parent.Parent.SetLapsCount(driverData, lapsCount, driverData.Class, completedDriverLaps, cleanLaps);
}
this.CurrentLap = currentLap;
//add incidents
if (driverData.Incidents != null)
{
foreach (IncidentScore incident in driverData.Incidents)
{
this.CurrentLap.MaxIncident = Math.Max(this.CurrentLap.MaxIncident ?? 0, incident.Strength);
this.CurrentLap.Incidents++;
this.Incidents++;
}
}
LapInfo previousLap = null;
if ((this.PreviousLap == null || this.PreviousLap.Lap != driverData.TotalLaps) &&
this.Laps.TryGetValue(driverData.TotalLaps, out previousLap))
{
this.PreviousLap = previousLap;
if (!this.PreviousLap.Date.HasValue)
{
this.PreviousLap.Date = DateTime.UtcNow;
}
}
if (currentLap.Position == 0)
currentLap.Position = driverData.Position;
if (driverData.CurrentS1 > 0)
{
**if (!currentLap.S1.HasValue)**
{
this.UpdateBestS1(driverData.BestS1);
this.Parent.Parent.UpdateBestS1(driverData.BestS1, driverData.UniqueName);
currentLap.UpdateS1(driverData.CurrentS1, driverData);
//reset the best split set at the finish line
if (this.PreviousLap != null && this.PreviousLap.SplitBest < 0)
this.PreviousLap.SplitBest = 0;
}
if (driverData.CurrentS2.HasValue && driverData.CurrentS1.HasValue && !currentLap.S2.HasValue)
{
double s2 = driverData.CurrentS2.Value - driverData.CurrentS1.Value;
this.UpdateBestS2(s2);
this.Parent.Parent.UpdateBestS2(s2, driverData.UniqueName);
currentLap.UpdateS2(s2, driverData);
}
}
if (this.PreviousLap != null)
{
if (driverData.LastLap > 0)
{
if (!this.PreviousLap.S3.HasValue && driverData.LastS2.HasValue)
{
double s3 = driverData.LastLap.Value - driverData.LastS2.Value;
this.UpdateBestS3(s3);
this.Parent.Parent.UpdateBestS3(s3, driverData.UniqueName);
this.PreviousLap.UpdateS3(s3, driverData);
}
if (!this.PreviousLap.LapTime.HasValue)
{
double? bestLap = this.Parent.Parent.BestLap;
this.PreviousLap.UpdateLapTime(driverData, 0);
this.Parent.Parent.UpdateBestLap(this.PreviousLap, driverData.BestLap, driverData);
this.UpdateBestLap(driverData.BestLap, this.PreviousLap);
this.PreviousLap.UpdateLapTime(driverData, bestLap);
}
}
else
{
if (this.PreviousLap.SplitBest.HasValue)
this.PreviousLap.UpdateBestSplit();
if (this.PreviousLap.SplitSelf.HasValue)
this.PreviousLap.UpdateSelfSplit();
}
}
if (driverData.InPits)
{
switch (driverData.Sector)
{
case Sectors.Sector1:
if (previousLap != null)
previousLap.InPits = true;
break;
case Sectors.Sector3:
currentLap.InPits = true;
break;
}
}
//lap to speed
if (currentLap.TopSpeed < driverData.Speed)
{
driverData.TopSpeedLap = driverData.Speed;
currentLap.UpdateTopSpeed(driverData.Speed);
}
else
driverData.TopSpeedLap = currentLap.TopSpeed;
//overall top speed
if (this.TopSpeed < driverData.Speed)
{
driverData.TopSpeed = driverData.Speed;
this.TopSpeed = driverData.Speed;
this.Parent.Parent.UpdateTopSpeed(this.TopSpeed, driverData);
}
else
driverData.TopSpeed = this.TopSpeed;
}
There is no way on earth the code can make it to that line and currentLap beeing null.
Or am I going crazy? :)
.HasValue will not throw if the nullable reference is null, but a.b.HasValue will if a is null.
I suspect that currentLap == null. I know you say you're sure that currentLap is not null, but I think that's the most likely explanation. Can you post more code?
Update:
Thanks for posting your code.
This doesn't throw:
void Main() {
var f = new Foo();
Console.WriteLine (f.S1);
Console.WriteLine (f.S1.HasValue);
}
class Foo {
private double? _s1 = null;
public double? S1 {
get { return _s1; }
set { _s1 = value; }
}
}
Could you try to create a minimal reproduction? (minimal code that exhibits the issue)
Maybe have a look at the previous line of code :) - debugger often highlights the next line after the one where the NullReferenceException was actually thrown.

Is there a better method to structure this if statement

It just seems a mess to me, my mind tells me there has to be a better way.
I have 6 controls on a web page.
if (printer_make_1.Text != "" && printer_model_1.Text != "" && printer_make_2.Text != "" && printer_model_2.Text != "" && printer_make_3.Text != "" && printer_model_3.Text != "")
{
// Do something
}
What is the best/most efficient way to do this?
You can refactor into a method, if you want to improve the readability or use the same logic elsewhere:
public Boolean AllControlsHaveAValue() {
return (printer_make_1.Text != ""
&& printer_model_1.Text != ""
&& printer_make_2.Text != ""
&& printer_model_2.Text != ""
&& printer_make_3.Text != ""
&& printer_model_3.Text != "");
}
Then just ask:
if (AllControlsHaveAValue()) {
// do something
}
Restructuring starts with your data: avoid printer_make_1, printer_make_2, ...
class PrinterData
{
public string Make { get; set; }
public string Model { get; set; }
}
PrinterData[] printers = new PrinterData[3]; //or use a List<>
printers[0] = new PrinterData { Make = "PH", Model = "1A" };
...
if (printers.All(p => ! (p.Make == "" || p.Model == "")) )
...
if(new[] { printer_make_1, printer_model_1 ...}.All(l => l.Text != string.Empty)
{
//do something
}
You might want to split it up to be more readable:
var labels = new[] { printer_make_1, printer_model_1 ... };
if(labels.All(l => l.Text != string.Empty))
{
//do something
}
I normally put that test into a method and call that to make the if easier to read
private boolean AreAllPrinterFieldsFilled()
{
return (printer_make_1.Text != ""
&& printer_model_1.Text != ""
&& printer_make_2.Text != ""
&& printer_model_2.Text != ""
&& printer_make_3.Text != ""
&& printer_model_3.Text != "");
}
Then in the if:
if (AreAllPrinterFieldsFilled)
{
// Do something
}
There are lots of ways to accomplish this - none of them are elegant. Do what is most readable to you (and those who may come behind you).
I would probably take this approach:
string makeText = String.Concat(printer_make_1.Text, printer_make_2.Text, printer_make_3.Text);
string modelText = String.Concat(printer_model_1.Text, printer_model_2.Text, printer_model_3.Text);
if (makeText.Length != 0 && modelText.Length != 0)
{
// Do something
}
if (!Enumerable.Range(1, 3)
.Any(i => ((TextBox)FindControl("printer_make_" + i)).Text == "" ||
((TextBox)FindControl("printer_model_" + i)).Text == "") {...}
It allows you to later expand the number of printer makes and models, but isn't as strong typed.
private bool CheckAllEmpty(params TextBox[] textBoxes)
{
return textBoxes.All(tb => tb.Text != null);
}
private void Foo()
{
...
if (CheckAllEmpty(tb1, tb2, tb3))
{
mbox("tb1, tb2 and tb3 are all empty");
}
else
{
mbox("tb1, tb2 or tb3 isn't empty");
}
...
}

Categories

Resources