473,804 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1825
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)1 2);

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.Wri teLine("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().IsVa lueType )
{
// 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.ChangeT ype( 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.Wri teLine("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.ushortFiel d = 2;
this.uintField = 2;
this.ulongField = 2;
this.floatField = 2;
this.doubleFiel d = 2;
this.decimalFie ld = 2;

data = new ArrayList();
data.Add(this.b yteField);
data.Add(this.s hortField);
data.Add(this.i ntField);
data.Add(this.l ongField);
data.Add(this.s byteField);
data.Add(this.u shortField);
data.Add(this.u intField);
data.Add(this.u longField);
data.Add(this.f loatField);
data.Add(this.d oubleField);
data.Add(this.d ecimalField);
....

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.ushortFiel d = 2;
this.uintField = 2;
this.ulongField = 2;
this.floatField = 2;
this.doubleFiel d = 2;
this.decimalFie ld = 2;

data = new ArrayList();
data.Add(this.b yteField);
data.Add(this.s hortField);
data.Add(this.i ntField);
data.Add(this.l ongField);
data.Add(this.s byteField);
data.Add(this.u shortField);
data.Add(this.u intField);
data.Add(this.u longField);
data.Add(this.f loatField);
data.Add(this.d oubleField);
data.Add(this.d ecimalField);
....

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(byteFi eld);
data.Add(shortF ield);
data.Add(intFie ld);

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)1 2);

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.ushortFiel d = 2;
this.uintField = 2;
this.ulongField = 2;
this.floatField = 2;
this.doubleFiel d = 2;
this.decimalFie ld = 2;

data = new ArrayList();
data.Add(this.b yteField);
data.Add(this.s hortField);
data.Add(this.i ntField);
data.Add(this.l ongField);
data.Add(this.s byteField);
data.Add(this.u shortField);
data.Add(this.u intField);
data.Add(this.u longField);
data.Add(this.f loatField);
data.Add(this.d oubleField);
data.Add(this.d ecimalField);
....

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********@spr ynet.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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.ushortFiel d = 2;
this.uintField = 2;
this.ulongField = 2;
this.floatField = 2;
this.doubleFiel d = 2;
this.decimalFie ld = 2;

data = new ArrayList();
data.Add(this.b yteField);
data.Add(this.s hortField);
data.Add(this.i ntField);
data.Add(this.l ongField);
data.Add(this.s byteField);
data.Add(this.u shortField);
data.Add(this.u intField);
data.Add(this.u longField);
data.Add(this.f loatField);
data.Add(this.d oubleField);
data.Add(this.d ecimalField);
....

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(byteFi eld);
data.Add(shortF ield);
data.Add(intFie ld);

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
4299
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 (recipes for making cosmetics etc) in 3 stages: Stage 1: basic info such as title, method etc and number of Phases (steps in recipe). Stage 2: dynamically generated form containing the exact number of phases as textboxes, depending on the number...
0
7296
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 ago and it works fine. The question is if the ArrayList has a set of different objects of different type, every time different. We know what types should be involved, but we don't know what objects in what order and how many are used in the...
6
13425
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 very little luck processing the Hashtable itself in VB6 (I can add a reference to the project so it knows what a Hashtable is, but I'm not having much luck looping through all objects in the Hashtable), so I decided to try a different idea.
5
3842
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 connectivity for basic operations and such. I've ran into a snag that I just can't think myself out of. Here's an example: I have an object for a Student called StudentRecord. It has properties such as name, grade, identification number, etc.
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.