Connecting Tech Pros Worldwide Help | Site Map

Why values are doubling when I use structures?

Newbie
 
Join Date: Oct 2009
Posts: 4
#1: Oct 7 '09
I have my public structure in a separate module:
Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Public Structure BezierSpline
  3.  
  4.     Public PointsCount As Integer
  5.     Public NodesCount As Integer
  6.     Public Points() As Point
  7.     Public NodeSpread() As Size
  8.     Public NodesState() As Byte
  9.  
  10.     Public Function AddPoint...
  11.     End Function
  12.  
  13.     Public Function DeletePoint...
  14.     End Function
  15. End Structure
  16.  
When I use a new instanse of structure, same values are changing in the previous one. Why?

Expand|Select|Wrap|Line Numbers
  1.     Private Splines() As BezierSpline
  2.     Private m_Splines_View() As BezierSpline
  3.     ....
  4.  
  5.         ReDim Splines(n_Spline)
  6.         ReDim m_Splines_View(n_Spline)
  7.     ....
  8.  
  9.  For j As Integer = 0 To m_Spline
  10.             With m_Splines_View(j)
  11.     ....
  12.                     .Points(mp).X = Splines(j).Points(mp).X + rx
  13.                     .Points(mp).Y = Splines(j).Points(mp).Y + ry
  14.             End With
  15.   Next
  16.  
...changs BOTH m_Splines_View(j).Points(mp).X AND Splines(j).Points(mp).X !!!
What do I do wrong?!
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: Oct 7 '09

re: Why values are doubling when I use structures?


Does it have the same effect if you don't do the "with" statement, and type out the full object?

m_Splines_View(j).Points(mp).X = Splines(j).Points(mp).X + rx
Newbie
 
Join Date: Oct 2009
Posts: 4
#3: Oct 8 '09

re: Why values are doubling when I use structures?


Yes. it have. The values of Splines(j).Points change ANY TIME I change values of m_Splines_View(j).Points! Even like that:

m_Splines_View(j).Points(mp).X = rx

Splines(j).Points(mp).X BECOMES EQUAL rx!

I changed the Stucture type to class, tried put it in List(Of BezierSpline) but nothing helps - THEY CHANGE BOTH! :-(
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: Oct 8 '09

re: Why values are doubling when I use structures?


Hmm, doesn't do it for me in C#, but C#s a lot better at not letting you get away with things.

Did you remember to:
*declare how many elements are in your array of BeizerSplines
*initialize each value
*decalre how many elements are in the Points array of each BeizerSpline
*initialize each value of them
Newbie
 
Join Date: Oct 2009
Posts: 4
#5: Oct 9 '09

re: Why values are doubling when I use structures?


How do I initialize an array with New keyword?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#6: Oct 9 '09

re: Why values are doubling when I use structures?


Something like:
Dim fred As Points() = New Points(9) {}

or something
Newbie
 
Join Date: Oct 2009
Posts: 4
#7: Oct 9 '09

re: Why values are doubling when I use structures?


Is it possible to declare an array containg other arrays? If so, how do I do it?
Reply


Similar Visual Basic .NET bytes