C
C#2y 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
Jimmacle2y 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
mihaxOP2y 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
mtreit2y ago
Just assign the new matrix to the existing variable.
mihax
mihaxOP2y ago
if I write new again the other gets deleted?
Jimmacle
Jimmacle2y ago
yes
Patrick
Patrick2y ago
you need to post some code and give context on what you're doing.
mtreit
mtreit2y ago
The existing matrix will no longer be referenced and the garbage collector will free that memory.
Jimmacle
Jimmacle2y ago
assuming it isn't referenced anywhere else, the garbage collector will eventually delete the memory damn i lost the race
mtreit
mtreit2y ago
🥷
mihax
mihaxOP2y ago
Oh I understand thanks guys Something is not working
Patrick
Patrick2y ago
almost like you should post some code and give some extra context.
mihax
mihaxOP2y ago
ok one second I dont know exactly what code to post that's the problem
Patrick
Patrick2y ago
> Something is not working explain what "something" is first then 🙂
mihax
mihaxOP2y 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
Patrick2y ago
variable names can have more than one letter, y'know? also you should be disposing of your streamreader.
mihax
mihaxOP2y ago
yes I know sorry for that is from school what u mean
Patrick
Patrick2y ago
im still waiting on what's wrong?
mihax
mihaxOP2y 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
Patrick2y ago
so, WinForms?
mihax
mihaxOP2y 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
Patrick2y ago
well... you harcoded it so.... yes?
StreamReader sr = new StreamReader("bila.in");
StreamReader sr = new StreamReader("bila.in");
mihax
mihaxOP2y ago
what u mean
Patrick
Patrick2y ago
you've written code to always look at the same file. bila.in
mihax
mihaxOP2y ago
no i have more
Patrick
Patrick2y ago
you don't.
mihax
mihaxOP2y 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
Patrick2y ago
ok?
Jimmacle
Jimmacle2y ago
needs more $code
MODiX
MODiX2y 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
mihaxOP2y ago
mihax
mihaxOP2y ago
this is all the code
Patrick
Patrick2y ago
well you don't reset the UI for one, presumably.
mihax
mihaxOP2y ago
and what I need to do to reset it
Patrick
Patrick2y 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
mihaxOP2y ago
ohhh so I need to remove the old then add the new one?
Patrick
Patrick2y ago
as i say, im guessing. but yes.
mihax
mihaxOP2y ago
and how I remove the old?
Patrick
Patrick2y ago
panel.Controls.Remove(tbl);
panel.Controls.Remove(tbl);
probably.
mihax
mihaxOP2y ago
nicee is working thanks for helping
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server