Discussion:
Does DataRow::Item copy the data inserted or just the pointer?
(too old to reply)
AndersG
2012-01-13 13:38:14 UTC
Permalink
I am building a DataSet in memory for the purpose of reporting in
Crystal Reports and the question is simply:

while(!set.IsEOF())
{
//
Numvar = set.GetInt(0);
Stringvar = set.GetString(1);
r = t->NewRow();
r->default[0] = Numvar;
r->default[1] = gcnew String(Stringvar);

t->Rows->Add(r);
set.MoveNext();
}

Is the contents of Stringvar copied to the dataset or just a pointer?
Ie is it safe to deallocate the string var after the Add()?

Or am I going about this the wrong way?
Cholo Lennon
2012-01-16 15:33:43 UTC
Permalink
Post by AndersG
I am building a DataSet in memory for the purpose of reporting in
while(!set.IsEOF())
{
//
Numvar = set.GetInt(0);
Stringvar = set.GetString(1);
What's the type of Stringvar? Is a MFC string, std::string or Dot Net
String?
Post by AndersG
r = t->NewRow();
r->default[0] = Numvar;
r->default[1] = gcnew String(Stringvar);
If Stringvar is a dot net String you don't have to create another string
with gcnew, just assign it to r->default[1];
Post by AndersG
t->Rows->Add(r);
set.MoveNext();
}
Is the contents of Stringvar copied to the dataset or just a pointer?
Ie is it safe to deallocate the string var after the Add()?
Or am I going about this the wrong way?
If Stringvar is a Dot Net String you don't have to worry about its
lifetime, it's managed by GC. Dot net strings are managed by reference,
so technically you are coping a pointer, and it's ok.
--
Cholo Lennon
Bs.As.
ARG
AndersG
2012-01-18 12:53:25 UTC
Permalink
Post by Cholo Lennon
If Stringvar is a Dot Net String you don't have to worry about its
lifetime, it's managed by GC. Dot net strings are managed by reference,
so technically you are coping a pointer, and it's ok.
Sorry, Stringvar is a CString
Cholo Lennon
2012-01-19 03:54:50 UTC
Permalink
Post by AndersG
Post by Cholo Lennon
If Stringvar is a Dot Net String you don't have to worry about its
lifetime, it's managed by GC. Dot net strings are managed by reference,
so technically you are coping a pointer, and it's ok.
Sorry, Stringvar is a CString
Ok, so your code is perfectly valid:

r->default[1] = gcnew String(Stringvar);

Stringvar is copied into a dot net string which is referenced by
r->default[1]. r and default[1] are dot net references. The referenced
objects will be released later by GC when he decide it (technically when
no references to them exist)

Regards

--
Cholo Lennon
Bs.As.
ARG

Loading...