System.StackOverflowException when reading in a file c# - c#

I'm trying to read a file in to my wpf program but the System.StackOverflowException is occuring! Any help would be amazing, thank you!!
My Code:
private void BestSellerListtxt_TextChanged(object sender, TextChangedEventArgs e)
{
string[] TopFictionArray = File.ReadAllLines("TopFiction.txt");
string[] TopNonFictionArray = File.ReadAllLines("TopNonFiction.txt");
for (int i = 0; i < TopFictionArray.Length; i++)
{
BestSellerListtxt.Text = TopFictionArray[i];
}
BestSellerListtxt.Text = "";
for (int i = 0; i < TopNonFictionArray.Length; i++)
{
BestSellerListtxt.Text = TopNonFictionArray[i];
}
}

Try this and with the help of string.Join(Delimeter, array) you can concat arrays elements without using loops and easily can print
BestSellerListtxt.Text = string.Join("," , TopFictionArray);
BestSellerListtxt_TextChanged = strin.join("," , TopNonFictionArray);

Related

I get an error when I try to import files from a txt to a datagridview

as the Problem in the title says, I am having Problems importing Data from a txt file into a datagridview. I get the error Input Array is longer than the number of columns. Here is what I am using to save my Data from the datagridview into a txt file and what I want to use to Import the data into the datagridview:
private void OpenFile_Click(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(#"C:\Users\gv9816\source\repos\Liste\UserData.txt");
string[] values;
for (int i = 0; i < lines.Length; i++)
{
values = lines[i].ToString().Split('|');
string[] row = new string[values.Length];
for (int j = 0; j < values.Length; j++)
{
row[j] = values[j].Trim();
}
_table.Rows.Add(row);
}
}
private void SaveFile_Click_1(object sender, EventArgs e)
{
TextWriter writer = new StreamWriter(#"C:\Users\gv9816\source\repos\Liste\UserData.txt");
for (int i = 0; i < _table.Rows.Count - 1; i++)
{
for (int j = 0; j < _table.Columns.Count; j++)
{
writer.Write("\t" + DataGridViewCell.Rows[i].Cells[j].Value.ToString() + "\t");
}
writer.WriteLine("");
}
writer.Close();
MessageBox.Show("Data Exported");
}
Also, here is what I use to create the columns:
private void Form1_Load(object sender, EventArgs e)
{
_table.Columns.Add("First Name", typeof(string));
_table.Columns.Add("Last Name", typeof(string));
_table.Columns.Add("Age", typeof(int));
DataGridViewCell.DataSource = _table;
}
Here is the Output in my UserData.txt file
Hans | Müller | 12 |
Here is what the application Looks like
And this is what is generated into the UserData file
Any help would be appreciated :)
The last | character is the problem, If you can't change the data inside txt file. Just change this line,
values = lines[i].ToString().Split('|');
to this,
values = lines[i].ToString().Split('|').Where(x => !string.IsNullOrEmpty(x)).ToArray();

String array to int array conversion

I have a problem with converting string array (previously loaded from .txt file) to integer one. File has got 100 random numbers, they load without any problem, I just need to convert them to integers to make them sortable with 3 types of sorting. I've tried many thing that were said here, but none of them seem to work. All the time I'm getting an error saying that it can't be converted.
Here is my loading from the file code:
string[] path = File.ReadLines("C:\\Users\\M\\numb.txt").ToArray();
int[] numb= new int[path.Length];
for (int i = 0; i < path.Length; i++)
{
Console.WriteLine(path[i]);
}
And after some options to choose I'm using switch to pick one:
switch (a)
{
case 1:
Console.WriteLine("1. Bubble.");
//int[] tab = numb;
babel(path);
for (int z = 0; z < path.Length; z++)
{
Console.Write(path[z] + ", ");
}
break;
I've got bubble sorting method in my program too, don't think it is necessary to post it here.
If anyone can help me here, I'd be really grateful.
#Amy - I've tried this:
numb[i] = path[i].Convert.toInt32(); - it doesn't work.
What I want to achieve is to change every number in this array to int, I think it should be involved here:
{
Console.WriteLine(path[i]);
}
This conversion works.
#string[] path = File.ReadLines("C:\\Users\\M\\numb.txt").ToArray();
String[] path = {"1","2","3"};
int[] numb = Array.ConvertAll(path,int.Parse);
for (int i = 0; i < path.Length; i++)
{
Console.WriteLine(path[i]);
}
for (int i = 0; i < numb.Length; i++)
{
Console.WriteLine(numb[i]);
}
It is better to use TryParse instead of parse. Also it is easier to work with List, than with array.
using System;
using System.Collections.Generic;
namespace StringToInt
{
class Program
{
static void Main(string[] args)
{
String[] path = { "1", "2", "3", "a", "b7" };
List<int> numb = new List<int>();
foreach (string p in path)
{
if (int.TryParse(p, out int result))
{
numb.Add(result);
}
}
for (int i = 0; i < path.Length; i++)
{
Console.WriteLine(path[i]);
}
for (int i = 0; i < numb.Count; i++)
{
Console.WriteLine(numb[i]);
}
}
}
}
I can't imagine this wouldn't work:
string[] path = File.ReadAllLines("C:\\Users\\M\\numb.txt");
int[] numb = new int[path.Length];
for (int i = 0; i < path.Length; i++)
{
numb[i] = int.Parse(path[i]);
}
I think your issue is that you are using File.ReadLines, which reads each line into a single string. Strings have no such ToArray function.

How can i improve performance of my application?

I have three different function which i call based on the condition. When data is lesser my application performance is good. However, i have around 11000 rows of data my application taking 1-2 hour to complete the process.
I have some logic written in my functions which filters data and add into collection. Is there is any way i can improve the performance of my application using multi-threading or parallel processing. so that my function runs parallel and performance get improved.
I created a sample of my requirement below:
private void button1_Click(object sender, EventArgs e)
{
String[] AllData = new String[7];
AllData[0] = "1";
AllData[1] = "2";
AllData[2] = "3";
AllData[3] = "1";
AllData[4] = "2";
AllData[5] = "3";
AllData[6] = "1";
for (int i = 0; i < AllData.Length; i++)
{
CommonFunction(AllData[i]);
}
}
private void CommonFunction(String AllData)
{
if (AllData == "1")
{
Function1(1);
}
else if (AllData == "2")
{
Function2(2);
}
else
{
Function3(3);
}
}
private void Function1(int ID)
{
for (int i = 0; i < 10; i++)
{
}
}
private void Function2(int ID)
{
for (int i = 0; i < 10; i++)
{
}
}
private void Function3(int ID)
{
for (int i = 0; i < 10; i++)
{
}
}
Any help is highly appreciated.
Instead of sequential for loop, you can try to use Parallel.ForEach.
for (int i = 0; i < AllData.Length; i++)
{
CommonFunction(AllData[i].ToString());
}
can be converted to
Parallel.ForEach(AllData, someData =>
{
CommonFunction(someData);
});

Delete Duplicates From Text File Using Array

This is my code but I'm not sure what to put in a certain area (see below) as I keep getting an error there. I am basically loading up a text file and then deleting any values that are repeated and then outputting the updated copy of the text file.
The text file looks like,
5
5
3
2
2
3
My code is
{
public partial class Form1 : Form
{
//Global Variable
int[] Original;
public Form1()
{
InitializeComponent();
}
//Exit Application
private void mnuExit_Click_1(object sender, EventArgs e)
{
this.Close();
}
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//Code to Load the Numbers From a File
OpenFileDialog fd = new OpenFileDialog();
//Open the File Dialog and Check If A File Was Selected
if (fd.ShowDialog() == DialogResult.OK)
{
//Open File to Read
StreamReader sr = new StreamReader(fd.OpenFile());
int Records = int.Parse(sr.ReadLine());
//Assign Array Sizes
Original = new int[Records];
//Go Through Text File
for (int i = 0; i < Records; i++)
{
Original[i] = int.Parse(sr.ReadLine());
}
}
}
private void btnOutput_Click(object sender, EventArgs e)
{
//Store Original Array
string Output = "Original \n";
//Output Original Array
for (int i = 0; i < Original.Length; i++)
{
Output = Output + Original[i] + "\n";
}
//Create TempArray
int[] TempArray = new int[Original.Length];
//Set TempArray Equal to Original Array
for (int i = 0; i < Original.Length; i++)
{
TempArray[i] = Original[i];
}
//Current Index
int Counter = 0;
//Loop Through Entire Array
for (int i = 0; i < TempArray.Length; i++)
{
for (int j = i + 1; j < TempArray.Length; j++)
{
//Replace Duplicate Values With '-1'
if (TempArray[i] == TempArray[j])
{
TempArray[j] = -1;
Counter++;
}
}
}
//Set Size of Original Array
Original = new int[Original.Length - Counter];
//Counter = 0;
//Remove -1 Values
//error begins here
for (int i = 0; i < Original.Length; i++)
{
for (int j = i + 1; j < Original.Length; j++)
{
//Set Original Array Equal to TempArray For Values Not Equal To '-1'
if (j != -1)
{
Original[j] = TempArray[j];
//Counter++;
}
}
}
//error ends here
//Final Output -- The New Array
Output = Output + "Original Without Duplicates\n";
for (int i = 0; i < Original.Length; i++)
{
Output = Output + Original[i] + "\n";
}
lblOutput.Text = Output;
}
}
}
I understand your logic, but wer'e all lazy programmers. You could simply use LINQ in order to prevent duplication. Load the array as you did already and use the Distinct method somthing like this:
int[] newArray = Orginal.Distinct().ToArray();
Goodluck.

Access to method list in current file (VS AddIn)

I want to get list of methods in current file, when my tool is fired, but I don't have idea how that combobox is nested in the _applicationObject object. Can you help me?
I did it this way:
private void ListMethods()
{
CodeNamespace codenamespace = null;
for(int i = 1; i <= _applicationObject.ActiveWindow.ProjectItem.FileCodeModel.CodeElements.Count; i++)
{
if(_applicationObject.ActiveWindow.ProjectItem.FileCodeModel.CodeElements.Item(i).Kind == vsCMElement.vsCMElementNamespace)
{
codenamespace = (CodeNamespace)_applicationObject.ActiveWindow.ProjectItem.FileCodeModel.CodeElements.Item(i);
}
}
for(int i = 1; i <= codenamespace.Members.Count; i++)
{
if(codenamespace.Members.Item(i) is CodeClass)
{
ListMethodsForClass(codenamespace.Members.Item(i) as CodeClass);
}
}
}
private void ListMethodsForClass(CodeClass codeclass)
{
for (int i = 1; i <= codeclass.Members.Count; i++)
{
message(codeclass.Members.Item(i).Name);
}
}
I hope it will help in the future some of you :)

Categories