|
List Info
Thread: C-Sharp (C#) Group: C# Classes
|
|
| C-Sharp (C#) Group: C# Classes |

|
2006-03-15 00:25:20 |
Am building simple class to keep some numerical data in it.
I then wish
to process each class and have a property which would
summarize the
data in each instance of the class. I do not with to loop
through each
object in the main program and wish to implement something
that will
work within the class to calculate say the sum of all data
for me, then
returned through a property.
Is there a way that I can iterate through each instance of a
class?
So if I have a class such as:
class myNumber
{
int _num;
static int totalNumbers;
public myNumber()
{
totalNumbers++;
}
~myNumber()
{
totalNumbers--;
}
public int numCount
{
get
{
return = totalNumbers;
}
}
public string num
{
get
{
return _num;
}
set
{
_num = value;
}
}
}
that's just an exmple - not sure if i got the syntax of
everything
right.
but i wish to provide a property such as 'instanceNumSum'
- which will
intrinsically iterate through each class instance and report
on the sum
of all numbers.
any ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharp googlegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribe googlegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---
|
|
| C-Sharp (C#) Group: Re: C# Classes |

|
2006-03-15 00:58:20 |
|
That would only work if the calling code implicitly calls the destructor for each object instance (if you are fine with that just declare an ArrayList in static scope, add the class to it via the constructor - ArrayList.Add
(this) - and remove it on the destructor, then you can loop that to get all the items). Out of curiosity why don't you want to loop through all items? This seems a perfect example where a Custom Collection class (or a collection using generics if you are on v2) would solve the problem perfectly.
Liam
On 3/15/06, pukiVruki <gmail.com">pukivruki gmail.com> wrote:
Am building simple class to keep some numerical data in it. I then wish to process each class and have a property which would summarize the data in each instance of the class. I do not with to loop through each
object in the main program and wish to implement something that will work within the class to calculate say the sum of all data for me, then returned through a property.
Is there a way that I can iterate through each instance of a class?
So if I have a class such as:
class myNumber { int _num; static int totalNumbers;
public myNumber() { totalNumbers++; }
~myNumber() { totalNumbers--;
}
public int numCount { get { return = totalNumbers; } } public string num { get { return _num; } set { _num = value; } } }
that's just an exmple - not sure if i got the syntax of everything right.
but i wish to provide a property such as 'instanceNumSum' - which will intrinsically iterate through each class instance and report on the sum
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C-Sharp (C#)" group. To post to this group, send email to C_Sharp googlegroups.com To unsubscribe from this group, send email to C_Sharp-unsubscribe googlegroups.com For more options, visit this group at http://groups.google.com/group/C_Sharp -~----------~----~----~----~------~----~------~--~---
|
| C-Sharp (C#) Group: Re: C# Classes |

|
2006-03-15 12:22:08 |
|
I was thinking a bit more about your problem and think I might have
something that would resolve the issue for you. I assuming that you are
concerned about performance when getting the sum value rather then
Collection adding performance. If this is the case then a combination
of a custom collection and some events should do the job nicely for you:
http://www.bigbold.com/snippets/posts/show/1702
Liam
On 3/15/06, Liam Leane <gmail.com">greapy gmail.com> wrote:
That would only work if the calling code
implicitly calls the destructor for each object instance (if you are
fine with that just declare an ArrayList in static scope, add the class
to it via the constructor - ArrayList.Add
(this) - and remove it on the destructor, then you can loop that to get
all the items). Out of curiosity why don't you want to loop through all
items? This seems a perfect example where a Custom Collection class (or
a collection using generics if you are on v2) would solve the problem
perfectly.
Liam
On 3/15/06,
pukiVruki <gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">pukivruki gmail.com> wrote:
Am building simple class to keep some numerical data in it. I then wish to process each class and have a property which would summarize the data in each instance of the class. I do not with to loop through each
object in the main program and wish to implement something that will work within the class to calculate say the sum of all data for me, then returned through a property.
Is there a way that I can iterate through each instance of a class?
So if I have a class such as:
class myNumber { int _num; static int totalNumbers;
public myNumber() { totalNumbers++; }
~myNumber() { totalNumbers--;
}
public int numCount { get { return = totalNumbers; } } public string num { get { return _num; } set { _num = value; } } }
that's just an exmple - not sure if i got the syntax of everything right.
but i wish to provide a property such as 'instanceNumSum' - which will intrinsically iterate through each class instance and report on the sum
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C-Sharp (C#)" group. To post to this group, send email to C_Sharp googlegroups.com To unsubscribe from this group, send email to C_Sharp-unsubscribe googlegroups.com For more options, visit this group at http://groups.google.com/group/C_Sharp -~----------~----~----~----~------~----~------~--~---
|
[1-3]
|
|
|
about | contact Other archives ( Real Estate discussion Medical topics )
|