473,396 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Proper class design

Hi,

I have a question about good class design ....

In my class I have some properties related to large amount
of data.
What is the best strategy, to include a reference to those
data (and making the object dependent on the client) or
copy the data into the object (independent object) ?

For example ....
class MyClass
{
int [] data; // reference to an int array .. no memory
alloc

(...)

// property Data
public Data
{
get
{
return data;
}
set
{
data = value;
}
}
}

and in the main method:

int [] large_data = new int[1000000];
MyClass myClass = new MyClass();
myClass.Data = large_data;

In this case the member "data" (via Property Data) refers to
"large_data" memory on the heap, like a C pointer.
At the other hand, if the "large_data" memory gets out of
scope, the object will no longer work, because of missing
data.

e.g.

MyClass myClass = new MyClass();
{
int [] large_data = new int[1000000];
myClass.Data = large_data;
}
// here, large_data is out of scope !!
int [] dd;
dd = myClass.Data; // data are missing ???

Some help would be nice.

Thanks,
Peter
Nov 16 '05 #1
3 1421
Hi,

Did you tried your example?

The memory will not be reclamed until no references are left.

MyClass myClass = new MyClass();
{
int [] large_data = new int[1000000];
myClass.Data = large_data;
}

as myClass is referencing the data it will not be reclamed.

which is the best way to create it , it does depend of how/who create the
data.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Peter" <An*****@work.nl> wrote in message
news:11***************@wgsvr01.wldelft.nl...
Hi,

I have a question about good class design ....

In my class I have some properties related to large amount
of data.
What is the best strategy, to include a reference to those
data (and making the object dependent on the client) or
copy the data into the object (independent object) ?

For example ....
class MyClass
{
int [] data; // reference to an int array .. no memory
alloc

(...)

// property Data
public Data
{
get
{
return data;
}
set
{
data = value;
}
}
}

and in the main method:

int [] large_data = new int[1000000];
MyClass myClass = new MyClass();
myClass.Data = large_data;

In this case the member "data" (via Property Data) refers to
"large_data" memory on the heap, like a C pointer.
At the other hand, if the "large_data" memory gets out of
scope, the object will no longer work, because of missing
data.

e.g.

MyClass myClass = new MyClass();
{
int [] large_data = new int[1000000];
myClass.Data = large_data;
}
// here, large_data is out of scope !!
int [] dd;
dd = myClass.Data; // data are missing ???

Some help would be nice.

Thanks,
Peter

Nov 16 '05 #2
In the first example, large_data does not get GC'ed unless myClass itself
goes out of scope as myClass is holding a reference to large_data.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Peter" <An*****@work.nl> wrote in message
news:11***************@wgsvr01.wldelft.nl...
Hi,

I have a question about good class design ....

In my class I have some properties related to large amount
of data.
What is the best strategy, to include a reference to those
data (and making the object dependent on the client) or
copy the data into the object (independent object) ?

For example ....
class MyClass
{
int [] data; // reference to an int array .. no memory
alloc

(...)

// property Data
public Data
{
get
{
return data;
}
set
{
data = value;
}
}
}

and in the main method:

int [] large_data = new int[1000000];
MyClass myClass = new MyClass();
myClass.Data = large_data;

In this case the member "data" (via Property Data) refers to
"large_data" memory on the heap, like a C pointer.
At the other hand, if the "large_data" memory gets out of
scope, the object will no longer work, because of missing
data.

e.g.

MyClass myClass = new MyClass();
{
int [] large_data = new int[1000000];
myClass.Data = large_data;
}
// here, large_data is out of scope !!
int [] dd;
dd = myClass.Data; // data are missing ???

Some help would be nice.

Thanks,
Peter

Nov 16 '05 #3
Hi,

I must say I did not try the given example, but it's clear,
..NET is really very smart.

Thanks,
Peter

"Sahil Malik" <co*****************@nospam.com> wrote in
message news:%2****************@TK2MSFTNGP15.phx.gbl...
In the first example, large_data does not get GC'ed unless myClass itself goes out of scope as myClass is holding a reference to large_data.
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Peter" <An*****@work.nl> wrote in message
news:11***************@wgsvr01.wldelft.nl...
Hi,

I have a question about good class design ....

In my class I have some properties related to large amount of data.
What is the best strategy, to include a reference to those data (and making the object dependent on the client) or
copy the data into the object (independent object) ?

For example ....
class MyClass
{
int [] data; // reference to an int array .. no memory alloc

(...)

// property Data
public Data
{
get
{
return data;
}
set
{
data = value;
}
}
}

and in the main method:

int [] large_data = new int[1000000];
MyClass myClass = new MyClass();
myClass.Data = large_data;

In this case the member "data" (via Property Data) refers to "large_data" memory on the heap, like a C pointer.
At the other hand, if the "large_data" memory gets out of scope, the object will no longer work, because of missing data.

e.g.

MyClass myClass = new MyClass();
{
int [] large_data = new int[1000000];
myClass.Data = large_data;
}
// here, large_data is out of scope !!
int [] dd;
dd = myClass.Data; // data are missing ???

Some help would be nice.

Thanks,
Peter



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would...
14
by: Viken Karaguesian | last post by:
Hello all, It stinks being a newbie! One thing that I'm not sure about with CSS is: where is the "proper" place to put font attibutes? This has me a little confused. Take the below style sheet...
0
by: BlueMonkMN | last post by:
I've been trying to think of the right way to design relationships between objects with different desired lifetimes that raise events. If an event source is a relatively permanent object and the...
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
1
by: Stefan Richter | last post by:
Hi, I have to write a little program, to enter and retrieve data from a database. The most important aspect is that the code is easy to understand, clear and organized, and easy to modifiy or...
2
by: =?Utf-8?B?QmVu?= | last post by:
I have a Customer table in the database that relates to a CustomerType table (I have several other table combinations in the database like Document and DocumentType). As far as I can tell, I...
2
by: John | last post by:
Hi there, I am writing a simple program that will connect to database. Database has 2 tables, let's call them father and child. This is one to many relationship. I would like to create...
1
by: mattmao | last post by:
I am brand new to C#.NET so here is my trial on this lab exercise: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace lab02exec { ...
8
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I have a couple of questions about the proper design of classes. I'll use a simple Customer class for my question. 1) Lets say that I have this Customer class like I said, and I want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.