C
C#14mo ago
mihax

❔ Matrix dynamic allocation

If a have a matrix which I allocated with new and later I need to delete what the matrix contain and allocate again with new. How I delete the matrix contents and allocate a new one?
40 Replies
Jimmacle
Jimmacle14mo ago
do you have an example of what you're trying to do? c# is a mangaged language, you generally don't have to worry about deleting memory
mihax
mihax14mo ago
yes but I need to delete all the information in the matrix to create a new one of different size and with other information
mtreit
mtreit14mo ago
Just assign the new matrix to the existing variable.
mihax
mihax14mo ago
if I write new again the other gets deleted?
Jimmacle
Jimmacle14mo ago
yes
Patrick
Patrick14mo ago
you need to post some code and give context on what you're doing.
mtreit
mtreit14mo ago
The existing matrix will no longer be referenced and the garbage collector will free that memory.
Jimmacle
Jimmacle14mo ago
assuming it isn't referenced anywhere else, the garbage collector will eventually delete the memory damn i lost the race
mtreit
mtreit14mo ago
🥷
mihax
mihax14mo ago
Oh I understand thanks guys Something is not working
Patrick
Patrick14mo ago
almost like you should post some code and give some extra context.
mihax
mihax14mo ago
ok one second I dont know exactly what code to post that's the problem
Patrick
Patrick14mo ago
> Something is not working explain what "something" is first then 🙂
mihax
mihax14mo ago
private void ReadData() { StreamReader sr = new StreamReader("bila.in"); string[] s; char[] sep = { ' ' }; string line = sr.ReadLine(); s = line.Split(sep, StringSplitOptions.RemoveEmptyEntries); n = int.Parse(s[0]); m = int.Parse(s[1]); H = new int[n + 1, m + 1]; c = new int[n + 1, m + 1]; int i = 0; while ((line = sr.ReadLine()) != null) { s = line.Split(sep, StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j < m; ++j) H[i, j] = int.Parse(s[j]); i++; if (i == n) break; } } so here when I first start the program I read the information from bila.in file
Patrick
Patrick14mo ago
variable names can have more than one letter, y'know? also you should be disposing of your streamreader.
mihax
mihax14mo ago
yes I know sorry for that is from school what u mean
Patrick
Patrick14mo ago
im still waiting on what's wrong?
mihax
mihax14mo ago
private void CreateGUI() { btn = new Button[n + 1, m + 1]; tbl = new TableLayoutPanel() { RowCount = n, ColumnCount = m, Dock = DockStyle.Fill }; int W = panel.Width; int H1 = panel.Height; L = Math.Min(H1, W) / Math.Max(n, m); for (int j = 0; j < m; ++j) tbl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, L)); for (int i = 0; i < n; ++i) { tbl.RowStyles.Add(new RowStyle(SizeType.Percent, L)); for (int j = 0; j < m; ++j) { btn[i, j] = new Button() { Dock = DockStyle.Fill, BackColor = Color.Yellow, Text = H[i, j].ToString(), ForeColor = Color.Black, Font = new Font("Arial", 14), Margin = new Padding(0), FlatStyle = FlatStyle.Flat }; tbl.Controls.Add(btn[i, j], j, i); } } panel.Controls.Add(tbl); }
Patrick
Patrick14mo ago
so, WinForms?
mihax
mihax14mo ago
then here I crate the matrix of buttons and display it on tablelayoutpanel yea and I want to have a button to choose another file and change the matrix but I choose the file and is not changing
Patrick
Patrick14mo ago
well... you harcoded it so.... yes?
StreamReader sr = new StreamReader("bila.in");
StreamReader sr = new StreamReader("bila.in");
mihax
mihax14mo ago
what u mean
Patrick
Patrick14mo ago
you've written code to always look at the same file. bila.in
mihax
mihax14mo ago
no i have more
Patrick
Patrick14mo ago
you don't.
mihax
mihax14mo ago
one sec private void btnChange_Click(object sender, EventArgs e) { openFileDialog = new OpenFileDialog(); openFileDialog.Title = "Choose input"; openFileDialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString(); openFileDialog.Filter = "Text Files (*.in;*.txt)|*.in;*.txt|" + "All Files (*.*)|*.*"; ReadDataChange(); CreateGUI(); tbl.Update(); } private void ReadDataChange() { if (openFileDialog.ShowDialog() == DialogResult.OK) { pathFileName = openFileDialog.FileName; StreamReader sr = new StreamReader(pathFileName); string[] s; char[] sep = { ' ' }; string line = sr.ReadLine(); s = line.Split(sep, StringSplitOptions.RemoveEmptyEntries); n = int.Parse(s[0]); m = int.Parse(s[1]); H = new int[n + 1, m + 1]; c = new int[n + 1, m + 1]; int i = 0; while ((line = sr.ReadLine()) != null) { s = line.Split(sep, StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j < m; ++j) H[i, j] = int.Parse(s[j]); i++; if (i == n) break; } } }
Patrick
Patrick14mo ago
ok?
Jimmacle
Jimmacle14mo ago
needs more $code
MODiX
MODiX14mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
mihax
mihax14mo ago
mihax
mihax14mo ago
this is all the code
Patrick
Patrick14mo ago
well you don't reset the UI for one, presumably.
mihax
mihax14mo ago
and what I need to do to reset it
Patrick
Patrick14mo ago
remove the old table, and add the new one from the panel. it's hard to say - your variables are all over the place. i dont know what is a control and what isn't. panel.Controls.Add(tbl); you're never removing the table. you're just continually adding tables as controls.
mihax
mihax14mo ago
ohhh so I need to remove the old then add the new one?
Patrick
Patrick14mo ago
as i say, im guessing. but yes.
mihax
mihax14mo ago
and how I remove the old?
Patrick
Patrick14mo ago
panel.Controls.Remove(tbl);
panel.Controls.Remove(tbl);
probably.
mihax
mihax14mo ago
nicee is working thanks for helping
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.