Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is wrong with my for-next loop 1

Status
Not open for further replies.

jmarkus

Technical User
Oct 15, 2002
124
CA
I have a For-Next Loop assigning values to an array, but each new value overwrites the value in the index before:

Code:
for p = 0 to kk-1
	pointdouble(0)=cpoint(p).coordinates.X
	pointdouble(1)=cpoint(p).coordinates.Y
	pointdouble(2)=cpoint(p).coordinates.Z

	point_data1(p).point = pointdouble
	point_data1(p).slope_type = UFConstants.UF_CURVE_SLOPE_NONE
	point_data1(p).crvatr_type = UFConstants.UF_CURVE_CRVATR_NONE
	
	lw.writeline("K= " & p.tostring &": " &point_data1(p).point(0) &","&point_data1(p).point(1) &","&point_data1(p).point(2))
	if p>0 then
		lw.writeline("K= " & p-1.tostring &": " &point_data1(p-1).point(0) &","&point_data1(p-1).point(1) &","&point_data1(p-1).point(2))
	end if
next p

The output becomes:
K= 0: 80.0068723329663,0.000311697607848771,25.0000086444894
K= 1: 82.6184557205712,0.0890023374493865,25.0002787371683
K= 0: 82.6184557205712,0.0890023374493865,25.0002787371683
K= 2: 85.3356535714023,0.362087419874367,24.9997263024703
K= 1: 85.3356535714023,0.362087419874367,24.9997263024703
K= 3: 88.0324414244833,0.840184094211939,25.0008079122174
K= 2: 88.0324414244833,0.840184094211939,25.0008079122174
K= 4: 90.6374012069239,1.52693367761155,25.0014357866076
K= 3: 90.6374012069239,1.52693367761155,25.0014357866076
K= 5: 93.0355329341884,2.41816831811692,24.9993864675541
K= 4: 93.0355329341884,2.41816831811692,24.9993864675541
K= 6: 95.1594576479436,3.48239559087328,24.9988022552044
K= 5: 95.1594576479436,3.48239559087328,24.9988022552044
K= 7: 96.9428619602756,4.68411422968257,25.0003322542663
K= 6: 96.9428619602756,4.68411422968257,25.0003322542663
K= 8: 98.3237291689222,5.98321234054376,24.9988888939346
K= 7: 98.3237291689222,5.98321234054376,24.9988888939346
K= 9: 99.2751578945642,7.33331493597748,25.0002114846827
K= 8: 99.2751578945642,7.33331493597748,25.0002114846827
K= 10: 99.8265572327264,8.68491999661199,24.9998931806953
K= 9: 99.8265572327264,8.68491999661199,24.9998931806953
K= 11: 100,10,25
K= 10: 100,10,25

Showing that each new index has the same value as the one before it. By the end of the loop the value is 100,10,25 for all members of the array.

What am I missing here?

Thanks,
Jeff
 

You are reusing the same object - pointdouble - in each element of the array. When you change the values of pointdouble, it changes them for all the other elements of the array because they are all pointing to the same object.

You need to instantiate a new pointdouble for each iteration of the loop.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks,

I moved my pointdouble declaration from outside the loop to inside and now it works!

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top