tags:

views:

37

answers:

4

I have this line of code which I want to concatenate -or at least solve the loop problem...

test = 1 - ("0." & thisnumber(0) & thisnumber(1) & thisnumber(2) & thisnumber(3) ... thisnumber(500) )

I want this to have a loop in it...

I simply want to get all the array values into 1 variable sort of thing, -as it is too long for a decimal. -So I want it to loop and work test out.

-Increasing thisnumber() (-Which is an array holding values e.g. 2,5,0,0,0,0,0,0,3,0,0,1)

Until it gets to about 500,

Can some implement a loop into this?

Or suggest a way...

Thanks a lot..

James :)

A: 

Use a generic List(of Int32) for this because its much more readable and performant. Add your numbers to the list, then iterate through it and append the numbers to a StringBuilder.

Tim Schmelter
How would that work in my situation?
James Rattray
+1  A: 

forgive the untested C# syntax:

var intArray = new StringBuilder();
intArray.Append("0.")
foreach(var number in thisnumber)
{
  intArray.Append(number.toString());
}
var test = 1- Double.Parse(intArray.toString());
Ian Jacobs
So if I had values thisnumber(0) = 1, thisnumber(1) = 5, thisnumber(2) = 0, thisnumber(3) = 7... It would do 1 - 0.1507?
James Rattray
I didn't test it, but that's the idea. I don't know what kind of precision `Double` data type has off the top of my head, but this should get you started.
Ian Jacobs
Ok i'll give it a go =]
James Rattray
Using Decimal.Parse() may be more precise.http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/921a8ffc-9829-4145-bdc9-a96c1ec174a5/
Ian Jacobs
A: 

You could do that using LINQ (though I'm not clear what the 1 - (... portion is supposed to be, since you cannot subtract a string from a number).

To deal with the relevant portion of your question--"looping" the array access portion:

Dim data As String = String.Join((From Idx in Enumerable.Range(0, 500) Select thisnumber(Idx).ToString()).ToArray(), "")

That will concatenate all of the elements of the array into a single string.

This addresses the literal requirement of your question (looping only the array access part), but I would recommend you go with another, more readable solution, such as the StrinBuilder-based approach that another user has already outlined.

Adam Robinson
A: 

string.Concat will take an array of strings...

double d = 0.0;
string[] values = new[] { "1", "4", "0", "9" };

d = 1 - double.Parse(string.Concat("0.", string.Concat(values)));
gbogumil
Ok thanks i'll give it a try...
James Rattray