472,993 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

iterating through a collection containing different types of numericvalues and adding a specified number to the values.

i have an assignment to iterate through a collection containing
different types of numeric values (float, double, int, byte, short etc.)
and to add 1 to all of them. I tried with array and arraylist so far,
but i either get a cast exception or the value doesn't seem to get
updated at all. What should i be doing to make it work?

Thanks in advance

Anders
Dec 15 '05 #1
6 1776
Anders Würtz wrote:
i have an assignment to iterate through a collection containing
different types of numeric values (float, double, int, byte, short etc.)
and to add 1 to all of them. I tried with array and arraylist so far,
but i either get a cast exception or the value doesn't seem to get
updated at all. What should i be doing to make it work?

Thanks in advance

Anders


try this:

ArrayList al = new ArrayList();
al.Add((int)5);
al.Add((double)7.89);
al.Add((short)12);

foreach (object o in al)
{
if (o is int)
o = ((int)o)++;
else if (o is double)
o = ((double)o)++
<...etc...>
}

Hope it helps
MuZZy
Dec 15 '05 #2

Anders Würtz wrote:
i have an assignment to iterate through a collection containing
different types of numeric values (float, double, int, byte, short etc.)
and to add 1 to all of them. I tried with array and arraylist so far,
but i either get a cast exception or the value doesn't seem to get
updated at all. What should i be doing to make it work?

Thanks in advance


There are lots of ways to do this, I'm not sure what they are teaching
you at present. If you *know* each one of them is a value type, you can
just convert them all to a given super-type (like double) and add to
them.
Otherwise, you could do something like this:

ArrayList a = new ArrayList();
int i = 1;
float f = 2.0f;
double d = 3.0;
byte b = 4;
string s = "hello";

// Add some data to the array.
a.Add( i );
a.Add( f );
a.Add( d );
a.Add( b );
a.Add( s );

// Print them out to show
foreach ( object o in a )
Console.Out.WriteLine("value: {0}", o );

// Increment those we can increment.
for ( int o=0; o<a.Count; ++o )
{
// Can't convert non-value types
if ( a[o].GetType().IsValueType )
{
// Make it a double
double d1 = (double)Convert.ChangeType( a[o], d.GetType() );
// Increment it
d1 += 1.0;
// Make it what it was.
object obj = Convert.ChangeType( d1, a[o].GetType() );
// And store it back in the array
a[o] = obj;
}
}

// Print them out to show
foreach ( object o in a )
Console.Out.WriteLine("value: {0}", o );

This isn't pretty and it isn't elegant. But it should get you started.

Matt

Dec 15 '05 #3
Anders Würtz wrote:
i have an assignment to iterate through a collection containing
different types of numeric values (float, double, int, byte, short etc.)
and to add 1 to all of them. I tried with array and arraylist so far,
but i either get a cast exception or the value doesn't seem to get
updated at all. What should i be doing to make it work?

Thanks in advance

Anders

private ArrayList data;

......

this.byteField = 2;
this.shortField = 2;
this.intField = 2;
this.longField = 2;
this.sbyteField = 2;
this.ushortField = 2;
this.uintField = 2;
this.ulongField = 2;
this.floatField = 2;
this.doubleField = 2;
this.decimalField = 2;

data = new ArrayList();
data.Add(this.byteField);
data.Add(this.shortField);
data.Add(this.intField);
data.Add(this.longField);
data.Add(this.sbyteField);
data.Add(this.ushortField);
data.Add(this.uintField);
data.Add(this.ulongField);
data.Add(this.floatField);
data.Add(this.doubleField);
data.Add(this.decimalField);
....

in a method...

....

foreach (object o in data)
{
if (o is byte)
o = ((byte)o)++;
else if (o is double)
o = ((short)o)++;
else if (o is int)
o = ((int)o)++;
else if (o is long)
o = ((long)o)++;
else if (o is sbyte)
o = ((sbyte)o)++;
else if (o is ushort)
o = ((ushort)o)++;
else if (o is uint)
o = ((uint)o)++;
else if (o is ulong)
o = ((ulong)o)++;
else if (o is float)
o = ((float)o)++;
else if (o is double)
o = ((double)o)++;
else if (o is decimal)
o = ((decimal)o)++;
}

compiler errors:

1. cannot assign to 'o' because it is read-only
2. The left-hand side of an assignment must be a variable,
property or indexer

how do i solve this so 1 is added to all the values?

thanks in advance

Anders

Dec 15 '05 #4

Anders Würtz wrote:
Anders Würtz wrote:
i have an assignment to iterate through a collection containing
different types of numeric values (float, double, int, byte, short etc.)
and to add 1 to all of them. I tried with array and arraylist so far,
but i either get a cast exception or the value doesn't seem to get
updated at all. What should i be doing to make it work?

Thanks in advance

Anders

private ArrayList data;

......

this.byteField = 2;
this.shortField = 2;
this.intField = 2;
this.longField = 2;
this.sbyteField = 2;
this.ushortField = 2;
this.uintField = 2;
this.ulongField = 2;
this.floatField = 2;
this.doubleField = 2;
this.decimalField = 2;

data = new ArrayList();
data.Add(this.byteField);
data.Add(this.shortField);
data.Add(this.intField);
data.Add(this.longField);
data.Add(this.sbyteField);
data.Add(this.ushortField);
data.Add(this.uintField);
data.Add(this.ulongField);
data.Add(this.floatField);
data.Add(this.doubleField);
data.Add(this.decimalField);
....

in a method...

....

foreach (object o in data)
{
if (o is byte)
o = ((byte)o)++;


Nope, can't do that. The object itself isn't modifiable.
You'd have to do something like this:

ArrayList data = new ArrayList();
data.Add(byteField);
data.Add(shortField);
data.Add(intField);

for (int i=0; i<data.Count; ++i )
{
object o = data[i];
if (o is byte)
{
byte b = (byte)o;
b++;
data[i] = b;
}
}
Matt

Dec 15 '05 #5
MuZZy wrote:
Anders Würtz wrote:
i have an assignment to iterate through a collection containing
different types of numeric values (float, double, int, byte, short etc.)
and to add 1 to all of them. I tried with array and arraylist so far,
but i either get a cast exception or the value doesn't seem to get
updated at all. What should i be doing to make it work?
Thanks in advance

Anders

try this:

ArrayList al = new ArrayList();
al.Add((int)5);
al.Add((double)7.89);
al.Add((short)12);

foreach (object o in al)
{
if (o is int)
o = ((int)o)++;
else if (o is double)
o = ((double)o)++
<...etc...>
}

Hope it helps
MuZZy


private ArrayList data;

......

this.byteField = 2;
this.shortField = 2;
this.intField = 2;
this.longField = 2;
this.sbyteField = 2;
this.ushortField = 2;
this.uintField = 2;
this.ulongField = 2;
this.floatField = 2;
this.doubleField = 2;
this.decimalField = 2;

data = new ArrayList();
data.Add(this.byteField);
data.Add(this.shortField);
data.Add(this.intField);
data.Add(this.longField);
data.Add(this.sbyteField);
data.Add(this.ushortField);
data.Add(this.uintField);
data.Add(this.ulongField);
data.Add(this.floatField);
data.Add(this.doubleField);
data.Add(this.decimalField);
....

in a method...

....

foreach (object o in data)
{
if (o is byte)
o = ((byte)o)++;
else if (o is double)
o = ((short)o)++;
else if (o is int)
o = ((int)o)++;
else if (o is long)
o = ((long)o)++;
else if (o is sbyte)
o = ((sbyte)o)++;
else if (o is ushort)
o = ((ushort)o)++;
else if (o is uint)
o = ((uint)o)++;
else if (o is ulong)
o = ((ulong)o)++;
else if (o is float)
o = ((float)o)++;
else if (o is double)
o = ((double)o)++;
else if (o is decimal)
o = ((decimal)o)++;
}

compiler errors:

1. cannot assign to 'o' because it is read-only
2. The left-hand side of an assignment must be a
variable, property or indexer

how do i solve this so 1 is added to all the values?

thanks in advance

Anders
Dec 15 '05 #6
foreach is a readonly implementation you cannot change the object.
Just use for loop it will work fine

Saurabh
"Matt" <ma********@sprynet.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Anders Würtz wrote:
Anders Würtz wrote:
i have an assignment to iterate through a collection containing
different types of numeric values (float, double, int, byte, short etc.)
and to add 1 to all of them. I tried with array and arraylist so far,
but i either get a cast exception or the value doesn't seem to get
updated at all. What should i be doing to make it work?

Thanks in advance

Anders

private ArrayList data;

......

this.byteField = 2;
this.shortField = 2;
this.intField = 2;
this.longField = 2;
this.sbyteField = 2;
this.ushortField = 2;
this.uintField = 2;
this.ulongField = 2;
this.floatField = 2;
this.doubleField = 2;
this.decimalField = 2;

data = new ArrayList();
data.Add(this.byteField);
data.Add(this.shortField);
data.Add(this.intField);
data.Add(this.longField);
data.Add(this.sbyteField);
data.Add(this.ushortField);
data.Add(this.uintField);
data.Add(this.ulongField);
data.Add(this.floatField);
data.Add(this.doubleField);
data.Add(this.decimalField);
....

in a method...

....

foreach (object o in data)
{
if (o is byte)
o = ((byte)o)++;


Nope, can't do that. The object itself isn't modifiable.
You'd have to do something like this:

ArrayList data = new ArrayList();
data.Add(byteField);
data.Add(shortField);
data.Add(intField);

for (int i=0; i<data.Count; ++i )
{
object o = data[i];
if (o is byte)
{
byte b = (byte)o;
b++;
data[i] = b;
}
}
Matt
Dec 27 '05 #7

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

Similar topics

4
by: Paxton | last post by:
At the risk of being told "If it ain't broke, don't fix it", my code works, but is ugly. Part of the admin site I'm working on at the moment includes a facility for users to enter Formulations...
0
by: Just D. | last post by:
There is an interesting article - http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=542&printer=t It shows how to serialize the ArrayList of identical objects. I did it a year...
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
5
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.