Anonymousaaaaaaaaaaaaaaaahhhh

hayy everybody,

samll problem i bet for you guys but a big one for me! i cant get my head around populating arrays! i know how arrays work but when it comes to populating them my head goes blank! please help me understand!

i have this in a textbox that i need populating!

08:22:39 , 8.20E+01

08:22:41 , 1.00E+03

08:22:43 , 1.00E+03

on the left its the xdata for the array!

on the right is the ydata for the array!

i have two arrays as double , one is xdata and one is ydata,

now i know how to pull from an array but dont know how to put data back into the array! the ammount of data varies but i dont care bout that i can sort that out i just need to understand this first, any help would be nice, many thanx Smile



Re: Visual C# General confused in populating array!

Ryan McBeth

Anonymousaah,

If you want to do what I think you want to do, this should work. Note that you will have to validate the textbox to insure that there are no unauthorized characters, and converting from scientific notion is its own problem. But if you just have a simple textbox and a button, this should fill up two arrays.

The basic strategy here is that we already have a lot of good information about the condition of the data. We have a newline delimiter and a comma delimiter. So we split the data first, by newline and shove that into an array of strings. Then we split the data a second time into a smaller chunk, and convert that chunk into the double neeeded for insertion into the double array. There are more elegant solutions, but this will work.

Best if luck, Ryan

///Declare the data arrays

Double[] Array1 = new double[4];

Double[] Array2 = new double[4];

//declare the holding string array. We will need it

//in a couple of lines to strip commas out of the

string[] sHoldingString = new string[1];

//Take the information and slap it into an array of strings

//as indicated by newline

string[] sMyStringArray = txtArrayIn.Text.Split('\n');

//Now that we have the array of strings, walk through the

//array of strings, split out the individual strings, and slap it into an array

for (int i = 0; i < 4; i++)

{

//Take out the line

sHoldingString = sMyStringArrayIdea.Split(',');

//now we have the data. Convert to a couble and assign

Array1Idea = Convert.ToDouble(sHoldingString[0]);

Array2Idea = Convert.ToDouble(sHoldingString[1]);

}





Re: Visual C# General confused in populating array!

Peter Ritchie

If you need to accept columnar data I would suggest using a grid control. Using TextBox for columnar data means you have to validate the separators and all that stuff. You should be able to create a grid with a time column and a number column and have it validate the text entered is valid...






Re: Visual C# General confused in populating array!

Anonymousaaaaaaaaaaaaaaaahhhh

Smile thanx for both of your hellp! i understand arrays so much more better thanx! and peter thanx for the new info! didnt know bout that!

thanx

anonymoussssaaaaaaaaaahhhhh Smile





Re: Visual C# General confused in populating array!

Anonymousaaaaaaaaaaaaaaaahhhh

hi,

im getting this error,

iv never used this way befor so unable to understand whats wrong

textBox8.Text.Split

the split is not recognised by the compiler! do i have to refrence if so what one

thanx




Re: Visual C# General confused in populating array!

Anonymousaaaaaaaaaaaaaaaahhhh

k this is strange,

for

textbox1.text.split(\n)

//that works fine now but the next part where the split is doesnt work

sHoldingString = sMyStringArray.Split(',');

it says system.array does not contain a defanition for "split"

this is my mistake but i dont need it now in double but in float

so how do i get it to float the array that i created is in float but there is no conversion from double, couldnt find on the net on string

thanx





Re: Visual C# General confused in populating array!

Anonymousaaaaaaaaaaaaaaaahhhh

hi

iv tried this but it says canot convert float to float[] and the same with everything else i do

yData[] = float.Parse(sHoldingString[0]);

xData[] = float.Parse(sHoldingString[1]);

yData[] = double.Parse(sHoldingString[0]);

xData[] = double.Parse(sHoldingString[1]);

yData[] = convert.Toint32(sHoldingString[0]);

xData[] = convert.Toint32(sHoldingString[1]);

yData and xData are both float. iv changed them so i could get round this problem but still wont work!





Re: Visual C# General confused in populating array!

sirjis

Are xData and yData supposed to be arrays That first line you wrote is telling it:

"parse the first string in the sHoldingString array to a float, and then assign it to yData[]".

First, I don't think I've ever actually used [] there in an assignment, though it may be syntactically valid. The problem is that you can't just make an entire array equal a single number -- it either has to be an array of length 1 containing the number or not an array.





Re: Visual C# General confused in populating array!

Anonymousaaaaaaaaaaaaaaaahhhh

yes i only have two arrays 1 is xData and the other is yData

here is some code so its more understandable! the code that was given works only iv got this small problem!

public float[] yData = new float[1000];

public float[] xData = new float[1000];

//in timer is

yData[countTick] = result;

xData[countTick] = countTick * int.Parse(txtCheck.Text);

//********************************************************************

//CALCULATE ALL POINTS

//********************************************************************

private void RecalcScreenXYForAllPoints()

{

if (xData == null || yData == null) return;

if (xData.Length != yData.Length) return;

//********************************************************************

//CALCULATE POINTS

//********************************************************************

private Point CalculateScreenXYForPoint(int dataIndex)

{

Point p = new Point();

p.X = Convert.ToInt32(xRectangleLeft + (xData[dataIndex] - xRangeStart) /

(xRangeEnd - xRangeStart) * wRectangleWidth);

double temp1 = Math.Log(yData[dataIndex], yLogBase) - Math.Log(yRangeStart, yLogBase);

double temp2 = Math.Log(yRangeStart, yLogBase) - Math.Log(yRangeEnd, yLogBase);

p.Y = Convert.ToInt32(yRectangleBottom + ((temp1 / temp2) * hRectangleheight));

return p;

}

and then the the code im using now but it has the float problem!

private void textBox8_TextChanged(object sender, EventArgs e)

{

//declare the holding string array. We will need it

//in a couple of lines to strip commas out of the

string[] sHoldingString = new string[1];

//Take the information and slap it into an array of strings

//as indicated by newline

string[] sMyStringArray = textBox8.Text.Split('\n');

//Now that we have the array of strings, walk through the

//array of strings, split out the individual strings, and slap it into an array

for (int i = 0; i < 4; i++)

{

//Take out the line

sHoldingString = sMyStringArray = textBox8.Text.Split(',');

//now we have the data. Convert to a couble and assign

yData = float.Parse(sHoldingString[0]);

xData = float.Parse(sHoldingString[1]);

}

}

im getting a problem with just the last two parts the yData and the xData , iv tried diffrent ways but havent been sucsesfull

please help! ! !

thanx





Re: Visual C# General confused in populating array!

sirjis

This is wrong:

for (int i = 0; i < 4; i++)

{

//Take out the line

sHoldingString = sMyStringArray = textBox8.Text.Split(',');

//now we have the data. Convert to a couble and assign

yData = float.Parse(sHoldingString[0]);

xData = float.Parse(sHoldingString[1]);

}

}

You are settings sMyHoldingString and sMyStringArray equal to each other, and both equal to the entire text box, split by commas. Also, you are trying to set the entire xData and yData arrays to a single float. You need to assign them to a specific row.

You want something like:

for (int i = 0; i < sMyStringArray.Length; i++)

{

sHoldingString = sMyStringArrayIdea.Split(',');

yDataIdea = float.Parse(sHoldingString[0]);

xDataIdea = float.Parse(sHoldingString[1]);

}

But this still doesn't seem right to me, because I'm not sure what xData and yData arrays are supposed to be.





Re: Visual C# General confused in populating array!

Anonymousaaaaaaaaaaaaaaaahhhh

this is for a graph

xData is the time (10:53:15)

yData is the reading (3.10 E-1)

just like iv stated i want what in the csv file to go into the array, its simple but i cant get my head round it and why it wont let me parse the values into yData and xData what am i doing wrong its driving me up the wall!!!!!!!!





Re: Visual C# General confused in populating array!

Zamial

yData = float.Parse(sHoldingString[0]);

xData = float.Parse(sHoldingString[1]);

That cannot work

yData is an array so you must specify the array location

yData[23] = float.Parse(sHoldingString[0]);

xData[35356346] = float.Parse(sHoldingString[1]);

Let's break down some of the code you posted

//DECLARE AN ARRAY OF LENGTH 1 TO HOLD STRINGS

string[] sHoldingString = new string[1];

//Take the information and slap it into an array of strings

//as indicated by newline

//DECLARE A STRING ARRAY AND POPULATE IT WITH THE VALUES IN A TEXTBOX SPLIT BY A NEW LINE

string[] sMyStringArray = textBox8.Text.Split('\n');

//Now that we have the array of strings, walk through the

//array of strings, split out the individual strings, and slap it into an array

//LOOP THROUGH THE STRING ARRAY 4 TIME (0,1,2,3)

ON EACH LOOP WE WILL SET AN ARRAY TO THE VALUE OF AN ARRAY THAT IS THE VALUE OF A SPLIT FUNCTION. PLEASE NOTE HERE YOU APPEAR TO BE DOING THE SAME THING 4 TIMES. SHOLDINGARRAY WILL HOLD ALL YOUR STRINGS ON THE FIRST PASS, THAT'S WHAT THE SPLIT IS DOING FOR YOU. HOWEVER THIS ISN'T GOING TO THROW AN EXCEPTION

for (int i = 0; i < 4; i++)

{

//Take out the line

sHoldingString = sMyStringArray = textBox8.Text.Split(',');

//now we have the data. Convert to a couble and assign

//HERE WE SET AN ARRAY TO THE VALUE OF A STRING CONVERTED TO A FLOAT.

yData = float.Parse(sHoldingString[0]);

xData = float.Parse(sHoldingString[1]);

}

}

So your code appears to be doing things that are not needed. Let's do this instead.

string[] sHoldingArray;

sHoldingArray = textBox8.Text.Split(',');

for(int i = 0;i < sHoldingArray.Length;i++)

{

//I have no idea what you want in here, perhaps a float array but it seems one holds the time etc.

//There are definitly some areas that don't make sense to me.

}

Hopefully this makes any problems a bit clearer to you.





Re: Visual C# General confused in populating array!

Anonymousaaaaaaaaaaaaaaaahhhh

that isnot my problem my problem is getting the data from the two string arrays into the two float arrays, thats what i need to know how to do iv tried alot of methods so far but none has worked!

so the whole code works it just pulling them out of the string array into the float array! that all i need to do!

many thanx





Re: Visual C# General confused in populating array!

Zamial

Ok then. I can't be sure what your types are etc.

However you have lots of floats as strings in an array

ArrayList floatArray = new ArrayList();

foreach(string s in stringArray)

{

floatArray.Add(float.Parse(s));

}

That would loop thorough each string and insert into an arraylist. If you want array do this

float[] floatArray = new floatArray[stringArray.Length]

for(int index = 0;i < stringArray.Length;i++)

{

floatArray[index] = float.Parse(stringArray[index]);

}





Re: Visual C# General confused in populating array!

sirjis

Anonymousaaaaaaaaaaaaaaaahhhh wrote:

i have this in a textbox that i need populating!

08:22:39 , 8.20E+01

08:22:41 , 1.00E+03

08:22:43 , 1.00E+03

on the left its the xdata for the array!

on the right is the ydata for the array!

Okay, let's look at the original problem. In the first column is a time, and in the second column is a value. Since the time does not have a date with it, we'll use the TimeSpan class instead of DateTime to represent it.

Then you'll want something like this:

Code Snippet

string allData = textBox1.Text;

string[] lines = allData.Split('\n');

// create output arrays now that we know the size

TimeSpan[] tData = new TimeSpan[lines.Length];

double[] yData = new double[lines.Length];

// go through each of the lines

for (int lineNum = 0; lineNum < lines.Length; lineNum++)

{

string[] curValues = lines[lineNum].Split(',');

tData[lineNum] = TimeSpan.Parse(curValues[0]);

yData[lineNum] = double.Parse(curValues[1]);

}

You still need to implement exception handling in case something is in an incorrect format.

Also, if you want "xData" instead of "tData", you're going to have to choose some way to convert from time to a number...something like the TotalMinutes or TotalSeconds properties of the TimeSpan instances that are currently being stored in tData.