Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a problem with my code and says: Compiler Error Message: CS1513: } expected
for (int i = 2001; i<= 2017; i++)
{
for (int j = 1; j <= 12; j++)
{
thursday = LastThursdayOfTheMonth(j, i);
if (LStock.Any(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2)))
{
line = LStock.First(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2));
int index = LStock.IndexOf(line) - 1;
LFridays.Add(LStock.ElementAt(index));
}
else
{
while (LStock.Any(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2)) != true)
{
if (LStock.Any(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2)))
{
line = LStock.First(element => element.date == thursday.ToString(("dd-MMM-yyyy"), dtf2));
LFridays.Add(line);
}
else
{
thursday.AddDays(1);
}
}
}
else if (thursday.ToString("dd-MMM-yyyy",dtf2).Contains("25-dic"))
{
friday = LastFridayOfTheMonth(j, i);
bool find = false;
while (find != true )
{
if (LStock.Any(element => element.date == friday.ToString("dd-MMM-yyyy", dtf2)))
{
find = true;
line = LStock.First(element => element.date == friday.ToString("dd-MMM-yyyy", dtf2));
LFridays.Add(line);
}
else
{
friday = friday.AddDays(1);
}
}
}
}
}
I supposed that I have a problem with {} but it's impossible to find where it is. Can someone find if i have less {} or is a different error?
Thank u so much
Are we really supposed to have a
if()
{
}
else
{
}
else if()
{
}
Shouldn't that be
if()
{
}
else if()
{
}
else
{
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
List<Object> rollerliste = (from row in roller.AsEnumerable() select (row["rolName"])).ToList();
List<Object> yetkiliste = (from row in roller.AsEnumerable() select (row["Visible"])).ToList();
for(int r = 0; r < rollerliste.Count(); r++)
{
for (int y = 0; y < yetkiliste.Count(); y++)
{
if(rollerliste[r].ToString() == "frmMasalar" && yetkiliste[y].ToString() == "true" && r == y)
{
cu.frmMasalar = 1;
}
else
{
cu.frmMasalar = 0;
}
}
}
Actually
if(rollerliste[r].ToString() == "frmMasalar" && yetkiliste[y].ToString() == "true" && r == y)
it seems to be checking for correct data but not working.
rollerliste
yetkiliste
frmMasalar
True
frmYonetim
True
I just want to make check rollerliste if column1 is true "button.Enable = true" or false
Please note that, within your loops, you are overwriting cu.frmMasalar over and over again. That alone might be the reason you're not getting what you want.
I'm not sure I totally understand what you want to do. But, check whether this might be simpler:
cu.frmMasalar = 0;
foreach(var row in roller.AsEnumerable()) {
if((string) row["rolName"] == "frmMasalar" && (bool) row["Visible"]) {
cu.frmMasalar = 1;
break;
}
}
There are also more condensed ways to do this, if the point is to find the one entry where rolName == "frmMasalar":
cu.frmMasalar = 0;
var matchingRow = roller.AsEnumerable()
.FirstOrDefault(r => (string) r["rolName"] == "frmMasalar"
&& (bool) row["Visible"]);
if(matchingRow != null)
cu.frmMasalar = 1;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have an Infint class with one List field. In the main program, I want to read numbers (character by character) and add them to this list.
The assignment itself doesn't matter in this moment, what I'm wondering is why I can't add values to the list of the Infint object from the if/else block (inside the first while loop). I have initialised the List itself in the class constructor. I added the line number1.Number.Add(1) for debugging reasons, since the code wasn't working and I've come to realize it seems to be working fine outside that if/else block. I can't seem to find the solution.
Here are the following implementations:
internal class Infint
{
public bool isNegative { get; set; }
public List<int> Number { get; set; }
public Infint()
{
isNegative = false;
Number = new List<int>();
}
}
static void Main(string[] args)
{
using (StreamReader sr = new StreamReader("infint.txt"))
{
int i;
char num;
for (int operation = 0; operation < 3; operation++)
{
Infint number1 = new Infint();
Infint number2 = new Infint();
number1.Number.Add(1); //works
i = sr.Read();
while (sr.Peek() >= 0)
{
number1.Number.Add(1); //works
if (i == 10 || i == 13)
break;
num = (char)i;
if (operation == 0)
{
number1.Number.Add(1); //doesn't work
if (i == 45)
number1.isNegative = true;
else
{
number1.Number.Add(1); //doesn't work
//number1.Number.Add((int)Char.GetNumericValue(num));
}
}else if(operation == 1)
{
if (i == 45)
number2.isNegative = true;
else
{
number2.Number.Add((int)Char.GetNumericValue(num));
}
}else if(operation == 2)
{
Console.WriteLine("counts of num1: " + number1.Number.Count);
for (var k = 0; k < number1.Number.Count; k++)
{
Console.Write(number1.Number[k]);
}
Console.WriteLine("counts of num2: " + number2.Number.Count);
for (var k = 0; k < number2.Number.Count; k++)
{
Console.Write(number2.Number[k]);
}
}
i = sr.Read();
}
if (i == -1)
{
break;
}
//i = sr.Read();
}
}
Console.Read();
}
Removing the various noise in your code (it's extremely low quality, if you care), what you're left with is:
using var sr = new StreamReader("infint.txt");
for (int operation = 0; operation < 3; operation++)
{
var i = sr.Read();
while (sr.Peek() >= 0)
{
if (i == 10 || i == 13) break;
if (operation == 0)
{
// oh noes
}
}
}
So looking at the code and assuming what you describe is correct (if vague), that means that your text file starts with a new line, possibly two (you don't even mention your OS, let alone the contents of the file). If that is indeed the case, then it should be strikingly obvious why operation can't be 0 around the branch you're expecting it to be.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
if (resourceinfo != null && resourceinfo.Products != null)
{
foreach (var product in resourceinfo.Products)
{
if (product.relatedEntities != null)
{
for (int i = 0; i < product.relatedEntities.Length; i++)
{
if (product.relatedEntities[i].reference.Equals("CONT1234"))
{
if (product.resources != null)
{
foreach (var item in product.resources)
{
if (item.resource != null && item.resource.resourceCharacteristics != null)
{
for (int j = 0; j < item.resource.resourceCharacteristics.Length; j++)
{
var ele = item.resource.resourceCharacteristics;
if (ele[j].name.ToLower().Contains(IMEI_VALUE_NAME))
{
imeiNo = respObj.resourceCharacteristics[0].value = ele[j].value;
break;
}
}
}
break;
}
}
}
}
}
break;
}
}
I want this to be in lambda expression or linq query format. When I try to bind into lambda expression whenever there is null or empty then an error shows up:
instance of an object is not defined
your question is not super clear, but the best query I could get is this:
var query =
(
from product in resourceinfo?.Products ?? Enumerable.Empty<Product>().Take(1)
from relatedEntity in product?.relatedEntities
where relatedEntity.reference.Equals("CONT1234")
from item in product?.resources.Take(1)
from ele in item?.resource?.resourceCharacteristics
where ele.name.ToLower().Contains(IMEI_VALUE_NAME)
select ele.value
).Take(1);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to break two for loop at the highlighted line. (after showing the MessageBox.Show("THE ITEM ID DOES NOT EXIST.!"); )
bool conditionitem = true;
for (int cun = 0; cun < ItemIdNumber.Length; cun++)
{
int Item_Id = Convert.ToInt32(ItemIdNumber[cun]);
for (int yyu = 0; yyu <= 1258038; yyu++)
{
int weer = c[yyu];
if (weer == Item_Id)
{
conditionitem = false;
itemseq = yyu;
}
}
if (conditionitem != false)
{
MessageBox.Show("THE ITEM ID DOES NOT EXIST.!");
break; //--> here i want two break for two times
}
}
By this break it only break the first loop.
Two options I can think of:
(1) Set a flag inside the second loop before you break out of it. Follow the inner iteration with a condition that breaks out of the first iteration if the flag is set.
bool flag = false;
foreach (item in Items)
{
foreach (item2 in Items2)
{
flag = true; // whenever you want to break
break;
}
if (flag) break;
}
(2) Use a goto statement.
foreach (item in Items)
{
foreach (item2 in Items2)
{
goto GetMeOutOfHere: // when you want to break out of both
}
}
GetMeOutOfHere:
// do what you want to do.
You can refactor the loop to be a method that finds the item:
SomeType SomeMethod(int itemId)
{
for (int cun = 0; cun < ItemIdNumber.Length; cun++)
{
int Item_Id = Convert.ToInt32(ItemIdNumber[cun]);
for (int yyu = 0; yyu <= 1258038; yyu++)
{
if (c[yyu] == itemId) return yyu;
}
}
return null;
}
Then just use that:
var item = SomeMethod(Item_Id);
if(item == null)
{
MessageBox.Show("THE ITEM ID DOES NOT EXIST.!");
}
else
{
// ...
}
This also avoids mixing UI logic and internal logic.
Put your nested loop into a function and return true/false whenever you want to break the loop?
bool Function()
{
for(int i = 0; i < 10; ++i)
{
for(int j = 0; j < 10; ++j)
{
if (error)
{
MessageBox.Show("THE ITEM ID DOES NOT EXIST.!");
return false;
}
}
}
return true;
}
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Following is my code,where I need to continue the loop in case of exception inside try block.
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
try
{
if (name != "")
{
name=name.ToString().Substring(12);
break;
}
}
catch{
continue;
}
}
Please tell me if i`m wrong at any place in my code.Please check for performance wise too.
Thanks in advance.
The continue is unneeded. It will automatically continue.
var name = doc.FirstOrDefault(x => !string.IsNullOrEmpty(x) && x.Length >= 12);
You don't need exception handling if you can avoid it:
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
if(name != null && name.Length >= 12)
{
name = name.Substring(12);
break;
}
}
Never use exceptions for something that is not exceptional. If you don't expect any of documents will have length less that 12, then you can use exceptions (but also not just for control flow):
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
if (name.Length < 12)
throw new FooException("Wrong document found!");
// do something with name
}
You do not need break or continue here.
try this.
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
try
{
if (!string.IsNotNullOrEmpty(name))
{
name=name.ToString().Substring(12);
}
}
catch{ }
}