❔ 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
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
yes but I need to delete all the information in the matrix to create a new one of different size and with other information
Just assign the new matrix to the existing variable.
if I write new again the other gets deleted?
yes
you need to post some code and give context on what you're doing.
The existing matrix will no longer be referenced and the garbage collector will free that memory.
assuming it isn't referenced anywhere else, the garbage collector will eventually delete the memory
damn i lost the race
🥷
Oh I understand
thanks guys
Something is not working
almost like you should post some code and give some extra context.
ok one second
I dont know exactly what code to post that's the problem
> Something is not working
explain what "something" is first then 🙂
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 filevariable names can have more than one letter, y'know?
also you should be disposing of your streamreader.
yes I know sorry for that is from school
what u mean
im still waiting on what's wrong?
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);
}
so, WinForms?
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
well... you harcoded it so.... yes?
what u mean
you've written code to always look at the same file.
bila.in
no i have more
you don't.
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;
}
}
}
ok?
needs more $code
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/this is all the code
well you don't reset the UI for one, presumably.
and what I need to do to reset it
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.ohhh
so I need to remove the old then add the new one?
as i say, im guessing.
but yes.
and how I remove the old?
probably.
nicee
is working
thanks for helping
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.