C# : bool array is not updating during execution - c#

i have this sample code which basically reads some string data from csv file and updates in textboxes and some variables.
The csv file is of the format 10,0,20,0,0,30,0..etc with 20 values. The idea is, if any of the value in csv file is 0, the isCashAvailable variable should be set to false and vice versa. Below is my code.
int[] cash = new int[20];
bool[] isCashAvailable = new bool[20];
public void UpdateCash()
{
var cashCSV = File.ReadAllText("cash.csv");
List<string> cashVal = cashCSV.Split(',').ToList();
for (int i = 1; i <= 20; i++)
{
var control = this.Controls.Find("textBox" + i.ToString(), true).FirstOrDefault() as TextBox;
if (cashVal[i - 1] != "0")
{
isCashAvailable[i - 1] = true;
control.Text = cashVal[i - 1];
cash[i - 1] = int.Parse(cashVal[i - 1]);
}
else
{
isCashAvailable[i - 1] = false;
control.Text = cashVal[i - 1];
cash[i - 1] = int.Parse(cashVal[i - 1]);
}
}
}
Now the issue is, when I try to update the value of the isCashAvailable and cash in runtime, its not updating. Whereas if am debugging it by stepping over each iteration, its updating. Can't seem to find why this is happening.

Try this:
int[] cash = new int[20];
bool[] isCashAvailable = new bool[20];
var cashCSV = File.ReadAllText("cash.csv");
List<string> cashVal = cashCSV.Split(',').ToList();
// changing this allows you to use i+1 just once, instead of doing i-1 over and over
for (int i = 0; i < 20; i++)
{
// this way it will throw an error if TextBox is not found instead of
// not throwing it here and then throwing NullReference later
var control = this.Controls.Find("textBox" + (i+1).ToString(), true).First() as TextBox;
//if-else is not needed here
//TryParse to make sure it always works
int.TryParse(cashVal[i], out cash[i]);
isCashAvailable[i] = (cash[i] > 0);
control.Text = cashVal[i];
//it works
Debug.Print($"{i}: isCashAvailable = {isCashAvailable[i]}, cashVal = {cashVal[i]}");
}

Related

Why does my bubblesort code need - 2 after the structure.Length for it to work?

I am trying to understand why my productTable.Length has to be - 2 for my bubblesort code to work.
I created two int variables, Last_Position and i. I created one product variable called temp and one bool called swap which is set to false. I then set Last_Position to equal productTable.Length - 2.
This is where I fail to understand, from what I have read the .Length counts the amount of characters and returns the amount however since 1 counts as 0 in programming, you have to - 1 to have the cap be accurate (i.e 1000 = 999) which has remained true until this part.
For some reason - 1 will throw up an error when the program runs targeting this code: if (String.Compare(productTable[i].prodCode, productTable[i + 1].prodCode) > 0) and states "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'"
The code works when I set it to - 2 but I want to understand why that is.
struct product
{
public string prodCode;
public string description;
public double price;
public int quantity;
}
product[] productTable;
public void loadData()
{
string path = "C:\\Users\\5004303\\Documents\\productFile.csv";
int lineCount = File.ReadLines(path).Count();
productTable = new product[lineCount];
product currentProduct = new product();
try
{
StreamReader sr = new StreamReader(path);
string line;
int currentArrayLocation = 0;
while (!sr.EndOfStream)
{
line = sr.ReadLine();
string[] fields = line.Split(',');
currentProduct.prodCode = fields[0];
currentProduct.description = fields[1];
currentProduct.price = Convert.ToDouble(fields[2]);
currentProduct.quantity = Convert.ToInt32(fields[3]);
productTable[currentArrayLocation] = currentProduct;
currentArrayLocation++;
}
sr.Close();
}
catch (FileNotFoundException)
{
MessageBox.Show("An error occured. Could not find file 'productFile.csv'.");
}
}
public void listProducts()
{
int currentArrayLocation = 0;
for (currentArrayLocation = 0; currentArrayLocation < productTable.Length; currentArrayLocation++)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = productTable[currentArrayLocation].prodCode;
lvi.SubItems.Add(Convert.ToString(productTable[currentArrayLocation].description));
lvi.SubItems.Add(Convert.ToString(productTable[currentArrayLocation].price));
lvi.SubItems.Add(Convert.ToString(productTable[currentArrayLocation].quantity));
lvProducts.Items.Add(lvi);
}
}
public void bubbleSort()
{
int last_Postion, i;
product temp;
last_Postion = productTable.Length - 2;
Boolean swap = false;
do
{
swap = false;
for (i = 0; i <= last_Postion; i++)
{
if (String.Compare(productTable[i].prodCode, productTable[i + 1].prodCode) > 0)
{
temp = productTable[i];
productTable[i] = productTable[i + 1];
productTable[i + 1] = temp;
swap = true;
}
}
}
while (swap == true);
}
Short answer:
Change
productTable.Lenght - 2 to productTable.Lenght - 1
and
for (i = 0; i <= last_Postion; i++) to for (i = 0; i < last_Postion; i++)
Explanation:
productTable.Lenght gives you the lenght of the list so productTable.Lenght - 1 is the last position in the list (0 to productTable.Lenght - 1).
In your "bubble" for loop inside the while you test against i+1 so i should only go up to the last_position - 1.
In your code when i == last_position then i + 1 is beyond the last position in the list.
Note: I did not check your code for validity, even if you make these changes, there may be other bugs.
Note on style, C# coding guidelines usually specify camel case for variable names, it is better to use lastPosition instead of last_Position. There are other styling "errors" in your code, such as declaring variables at the top of the function, using types instead of var. It may be some of this "errors" are course requirements, but a short read of any coding conventions document (e.g. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions) would be beneficial to you. Most work places have their own coding guidelines or adopt a public one, but on all of them are pretty similar.

Fill an array with a loop

I want to get a shorter code than now but I don't know how.
What I do now is like the code below.
arrPictureBox[0] = picChair0;
arrPictureBox[1] = picChair1;
arrPictureBox[2] = picChair2;
arrPictureBox[3] = picChair3;
arrPictureBox[4] = picChair4;
arrPictureBox[5] = picChair5;
arrPictureBox[6] = picChair6;
arrPictureBox[7] = picChair7;
arrPictureBox[8] = picChair8;
arrPictureBox[9] = picChair9;
arrPictureBox[10] = picChair10;
arrPictureBox[11] = picChair11;
(pic) is a picturebox.
But I want less code but I don't know if it possible to do this with a loop (for loop).
for (int i = 0 ; i < arrPictureBox.Length; i++)
{
arrPictureBox[i] = picChair + i;
}
If picChairN is a local variable then there's nothing you can do to simplify it as much as you'd like. The best you can do is
arrPictureBox = new [] { picChair0, picChair1, picChair2, picChair3,
picChair4, picChair5, picChair6, picChair7,
picChair8, picChair9, picChair10, picChair11};
If picChairN is a class member (e.g. a field created by the designer) then you could use reflection, but considering you already have the array method typed out I don't see much benefit.
Let's predict you're on WinForms and the pictureBoxes already exist, then you can use the following:
for (int i = 0; i < arrPictureBox.Length; i++)
{
arrPictureBox[i] = this.Controls["picChair" + i];
}
Which actually does this:
get the first Control (a PictureBox for example) with the given name
add the found control to the array of pictureboxes
EDIT:
It might be useful to check for non existing pictureBoxes:
for (int i = 0 ; i < arrPictureBox.Length; i++)
{
var pb = this.Controls["picChair" + i] as PictureBox;
if (pb != null)
{
arrPictureBox[i] = pb;
}
}
You can use al List like below.
List<string> arrPictureBox = new List<string>();
for (int i = 0; i < 20; i++)
{
arrPictureBox.Add("picChair" + i);
}
var result = arrPictureBox.ToArray();
Hope it helps.
If all the picture boxes are on the same form and are the ONLY picture boxes on the form, you can loop through them with something like the following:
int x = 0;
foreach(Control c in this.Controls)
{
if(c is PictureBox)
{
arrPictureBox[x++] = c
}
}

Try...catch returning nothing but code is still breaking

UPDATE: So this code is collection a SQL Query into a DataSet prior to this method. This data set is then dropped into excel in the corresponding tab at a specific cell address(which is loaded from the form) but the code below is the exporting to excel method. I am getting the following error:
An unhandled exception of type 'System.AccessViolationException' occurred in SQUiRE (Sql QUery REtriever) v1.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I have been tracking this for a while and thought I fixed it, but my solution was a false positive. So I am using a try...catch block that is breaking but not returning anything. Let me know if you all see anything that I am missing. I usually break on this line (templateSheet = templateBook.Sheets[tabName];) and on the same tabName. The tab is not locked or restricted so It can be written to and works more than half of the time.
public void ExportToExcel(DataSet dataSet, Excel.Workbook templateBook, int i, int h, Excel.Application excelApp) //string filePath,
{
try
{
lock (this.GetType())
{
Excel.Worksheet templateSheet;
//check to see if the template is already open, if its not then open it,
//if it is then bind it to work with it
//if (!fileOpenTest)
//{ templateBook = excelApp.Workbooks.Open(filePath); }
//else
//{ templateBook = (Excel.Workbook)System.Runtime.InteropServices.Marshal.BindToMoniker(filePath); }
//Grabs the name of the tab to dump the data into from the "Query Dumps" Tab
string tabName = lstQueryDumpSheet.Items[i].ToString();
templateSheet = templateBook.Sheets[tabName];
// Copy DataTable
foreach (System.Data.DataTable dt in dataSet.Tables)
{
// Copy the DataTable to an object array
object[,] rawData = new object[dt.Rows.Count + 1, dt.Columns.Count];
// Copy the values to the object array
for (int col = 0; col < dt.Columns.Count; col++)
{
for (int row = 0; row < dt.Rows.Count; row++)
{ rawData[row, col] = dt.Rows[row].ItemArray[col]; }
}
// Calculate the final column letter
string finalColLetter = string.Empty;
string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int colCharsetLen = 26;
if (dt.Columns.Count > colCharsetLen)
{ finalColLetter = colCharset.Substring((dt.Columns.Count - 1) / colCharsetLen - 1, 1); }
finalColLetter += colCharset.Substring((dt.Columns.Count - 1) % colCharsetLen, 1);
/*Grabs the full cell address from the "Query Dump" sheet, splits on the '=' and
*pulls out only the cell address (i.e., "address=a3" becomes "a3")*/
string dumpCellString = lstQueryDumpText.Items[i].ToString();
string dumpCell = dumpCellString.Split('=').Last();
/*Refers to the range in which we are dumping the DataSet. The upper right hand cell is
*defined by 'dumpCell'and the bottom right cell is defined by the final column letter
*and the count of rows.*/
string firstRef = "";
string baseRow = "";
//Determines if the column is one letter or two and handles them accordingly
if (char.IsLetter(dumpCell, 1))
{
char[] createCellRef = dumpCell.ToCharArray();
firstRef = createCellRef[0].ToString() + createCellRef[1].ToString();
for (int z = 2; z < createCellRef.Count(); z++)
{ baseRow = baseRow + createCellRef[z].ToString(); }
}
else
{
char[] createCellRef = dumpCell.ToCharArray();
firstRef = createCellRef[0].ToString();
for (int z = 1; z < createCellRef.Count(); z++)
{ baseRow = baseRow + createCellRef[z].ToString(); }
}
int baseRowInt = Convert.ToInt32(baseRow);
int startingCol = ColumnLetterToColumnIndex(firstRef);
int endingCol = ColumnLetterToColumnIndex(finalColLetter);
int finalCol = startingCol + endingCol;
string endCol = ColumnIndexToColumnLetter(finalCol - 1);
int endRow = (baseRowInt + (dt.Rows.Count - 1));
string cellCheck = endCol + endRow;
string excelRange;
if (dumpCell.ToUpper() == cellCheck.ToUpper())
{ excelRange = string.Format(dumpCell + ":" + dumpCell); }
else
{ excelRange = string.Format(dumpCell + ":{0}{1}", endCol, endRow); }
//Dumps the cells into the range on Excel as defined above
templateSheet.get_Range(excelRange, Type.Missing).Value2 = rawData;
/*Check to see if all the SQL queries have been run from
if (i == lstSqlAddress.Items.Count - 1)
{
//Turn Auto Calc back on
excelApp.Calculation = Excel.XlCalculation.xlCalculationAutomatic;
/*Run through the value save sheet array then grab the address from the corresponding list
*place in the address array. If the address reads "whole sheet" then save the whole page,
*else set the addresses range and value save that.
for (int y = 0; y < lstSaveSheet.Items.Count; y++)
{
MessageBox.Show("Save Sheet: " + lstSaveSheet.Items[y] + "\n" + "Save Address: " + lstSaveRange.Items[y]);
}*/
//run the macro to hide the unused columns
excelApp.Run("ReportMakerExecute");
//save excel file as hospital name and move onto the next
SaveTemplateAs(templateBook, h);
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}

How to allocate more memory for a variable

I have created a c# program that takes a scan input, and then takes the information from the input. What I have been noticing is that for larger strings, my program for some reason splits the string into two parts (not in half), which screws up the way I get my information. My string has hexadecimal values in it as well.
For example, when I scan a code into my console, it reads the string
[)>065JUN1234567892300167Q205GT21L123 ABC06P123456787Q100PL7Q10PKQ1006P98356877Q100PL7Q5PKQ2006P235265437Q200PL7Q40PKQ5
but it splits that string into:
[)>065JUN1234567892300167Q205GT21L123 ABC06P123456787Q100PL7Q10PKQ1006P98356877Q100"
and
PL7Q5PKQ2006P235265437Q200PL7Q40PKQ5
Any idea how to fix this, or allocate more memory to my variable which reads the console for the input scan?
Here is my code, its kind of long.
static void Main(string[] args)
{
Console.WriteLine("Please enter the date and lane number");
Console.WriteLine("like so: ddmmyylanenumber.");
string lanenum = Console.ReadLine();
Console.WriteLine("When done scanning, please type");
Console.WriteLine("\"finish\" into the console.");
string scanInput;
do
{
Console.Write("Scan now:");
scanInput = Console.ReadLine();
//The number before "JUN" identifies the type of label it is.
int posOfJUN = scanInput.IndexOf("JUN");
//Finding the type of label
string typeOfLabel = scanInput.Substring(posOfJUN - 1, 1);
string label;
if (typeOfLabel == "5")
{
label = "mixed";
}
else if (typeOfLabel == "1")
{
label = "individual";
}
else
{
label = null;
}
switch (label)
{
case "individual":
partNumber = scanInput.Substring(8, 8);
int posOfQ1 = scanInput.IndexOf("Q");
int posOf1JUN = scanInput.IndexOf("1JUN");
//Quantity of the pack
total = scanInput.Substring(posOfQ1 + 1, posOf1JUN - posOfQ1 - 1 - 1);
//number of packs is 1 because individual
numOfPacks = "1";
dunsNumber = scanInput.Substring(posOf1JUN + 4, 9);
//used to find the duns and serial number
posOf20L = scanInput.IndexOf("20L");
posOf21L = scanInput.IndexOf("21L");
//Setting the serial number
if (posOf21L == -1 || posOf20L < posOf21L)
{
serialNumber = scanInput.Substring(posOf1JUN + 4, posOf20L - posOf1JUN - 4 - 1);
}
else if (posOf20L == -1 || posOf21L < posOf20L)
{
serialNumber = scanInput.Substring(posOf1JUN + 4, posOf21L - posOf1JUN - 4 - 2);
}
else
{
serialNumber = null; //else clause if serial number can't be created
Console.WriteLine(new ArgumentException("Error obtaining Serial Number"));
}
partObject part2 = new partObject(partNumber, total, numOfPacks);
newPacks = int.Parse(numOfPacks);
total = total.ToString();
newtotal = int.Parse(total);
part2.callSQL(partNumber, newtotal, newPacks, dunsNumber, serialNumber, lanenum);
break;
case "mixed":
posOfJUN = scanInput.IndexOf("JUN");
dunsNumber = scanInput.Substring(posOfJUN + 3, 9);
int posOf7Q = scanInput.IndexOf("7Q");
posOf20L = scanInput.IndexOf("20L");
posOf21L = scanInput.IndexOf("21L");
//Finding serial number
serialNumber = scanInput.Substring(posOfJUN + 3, posOf7Q - posOfJUN - 3);
//The following lines are to find how many different parts are in the mixed load.
posOfPK = scanInput.IndexOf("PK");
int PKTemp;
int parts = 1;
//Each time a "PK" is seen, it means there is another part so the count increments.
while (scanInput.IndexOf("PK", posOfPK + 1) != -1)
{
PKTemp = scanInput.IndexOf("PK", posOfPK + 1);
posOfPK = PKTemp;
parts++;
}
//Creating an array of size "parts"
int posOf06 = scanInput.IndexOf("06");
int temp06 = scanInput.IndexOf("06", posOf06 + 2);
posOf06 = temp06;
int indexOfP = scanInput.IndexOf("P", posOf06 + 1);
partNumber = scanInput.Substring(indexOfP + 1, 8);
posOfPK = scanInput.IndexOf("PK", indexOfP);
posOfPL = scanInput.IndexOf("PL", indexOfP);
posOf7Q1 = scanInput.IndexOf("7Q", indexOfP);
partObject[] arrayOfParts = new partObject[parts];
for (int i = 0; i < parts; i++)
{
//Finds the different values, creates an object and puts it into the array of parts
partNumber = scanInput.Substring(indexOfP + 1, 8);
total = scanInput.Substring(posOf7Q1 + 2, posOfPL - posOf7Q1 - 2);
numOfPacks = scanInput.Substring(posOfPL + 5, posOfPK - posOfPL - 5);
arrayOfParts[i] = new partObject(partNumber, total, numOfPacks);
//resetting the variables for the next iteration, so a new object can be created with the next part
posOf06 = scanInput.IndexOf("06", temp06 + 1);
indexOfP = scanInput.IndexOf("P", posOf06 + 1);
temp06 = posOf06;
posOfPK = scanInput.IndexOf("PK", indexOfP);
posOfPL = scanInput.IndexOf("PL", indexOfP);
posOf7Q1 = scanInput.IndexOf("7Q", indexOfP);
//putting each object into SQL
newtotal = int.Parse(total);
newPacks = int.Parse(numOfPacks);
serialNumber = serialNumber + "P" + (i + 1);
arrayOfParts[i].callSQL(partNumber, newtotal, newPacks, dunsNumber, serialNumber, lanenum);
}
break;
default:
break;
}
}
} while (scanInput != "finish");
}
The full string is never there right from the start of the code.
If you debug your program I think you will find that the string is all there.
I'm not sure why you think the string is split... a System.String can only hold one string. At some point are you getting a string[] ?
Place a breakpoint after this line scanInput = Console.ReadLine(); and in Visual Studio hover over scanInput, and it will show you the whole string.... or try just printing out the string to show that it is all there.
EDIT: If you're using hexadecimal, try looking up the hex values which cause the anomaly on the ascii table. Perhaps the hex value is resulting in a carriage return or newline.

Remove Rows from TableLayoutPanel [duplicate]

I'm currently trying to fill a TableLayoutPanel through a method which goes as follows:
private int _rowCount;
public void InitPaths()
{
int c = 1;
int a = 1;
while (a < _PathRows.Length - 1)
{
var label = new Label();
//
// Label - Format.
//
label.Dock = DockStyle.Fill;
label.AutoSize = false;
label.Text = _pfadZeilen[a];
label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
label.Size = new System.Drawing.Size(22, 13);
label.BackColor = System.Drawing.Color.Transparent;
TableLayoutP.Controls.Add(label, 3, c);
//Checkboxen Einfügen
var cbox = new CheckBox();
//
//Checkbox Format.
cbox.Anchor = System.Windows.Forms.AnchorStyles.None;
cbox.AutoSize = true;
cbox.CheckAlign = System.Drawing.ContentAlignment.MiddleCenter;
cbox.Name = "checkBoxPfad" + a;
cbox.Size = new System.Drawing.Size(15, 14);
cbox.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cbox.UseVisualStyleBackColor = true;
TableLayoutP.Controls.Add(cbox, 0, c);
a++;
c++;
}
this._rowCount = BibTable.GetRowHeights().Length; // which seems to be Holding the value only within the method
}
and then delete all rows on Action, through the following Method:
public void RemoveRows()
{
for (int row = _rowCount; row >= 0; row--)
{
BibTable.RowStyles.RemoveAt(row);
BibTable.RowCount--;
}
}
Now the Problem is, if I try to do anything with the TableLayoutP outside of the method where all rows are initialized, it will tell me:
Object reference not set to the instance of an object.
What can I do? Is there a way to get a method inside a method (I'm realising just how stupid that sounds while typing it) or any other way to deal with this Situation?
You are ittering through GetRowHeights(), returning the height of each row. But you are deleting from the RowStyles collection which is not directly related to the first collection. I assume that GetRowHeights() returns much more rows, than RowStyles has.
Why not:
BibTable.RowCount = 0;
BibTable.RowStyles.Clear();
You are ittering through GetRowHeights(), returning the height of each row. But you are deleting from the RowStyles collection which is not directly related to the first collection. I assume that GetRowHeights() returns much more rows, than RowStyles has.
Why not:
BibTable.RowCount = 0;
BibTable.RowStyles.Clear();

Categories