Thread: Arrays as fields

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    12

    Arrays as fields

    I have had a hard time searching for the answer to my problem because it always comes up with indexing the class itself, and not the array as a field of the class. I assume the answer is simple, but I am confused by it and not sure what to do.

    So here is my problem:
    Code:
            private PointF[] inputArray;
            public PointF[] InputArray
            {
                get
                {
                    return inputArray;
                }
                set
                {
                    AdjustedArray = translatePoint(value);
                    inputArray = value;
                }
            }
      
            public PointF[] AdjustedArray;
    
            private PointF translatePoint(PointF point)
            {
                
                Do stuff
                return translatedPointF;
                 
            }
    I am setting the Arrays to the proper size in the contructor.

    So I have an array as part of my class and I want to adjust the values as I pass them to the array. It would be nice to do this in the set{}.

    The problem is that I don't understand how it knows what index is being passed to it and how I can pass that index to the adjusted array. If I take the AdjustedArray line out of the set{}, I can access the array no problem. . . but that then requires me to explicitly adjust the AdjustedArray from outside of the class and it seems like it would be a better idea to adjust it from within the class. . . especially considering I want every point adjusted.

    Right now, this bit of code will not compile.

    So how do I pass the index that was passed to the InputArray? Or am I thinking about this all wrong?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Perhaps I'm confused. Wouldn't your InputArray set take an array (not a point)? So you could then make translatePoint take an array of PointF, and return an array of PointF (if that's possible in C#, which I actually don't know, but would guess so).

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    tabstop,

    Thanks for the reply.

    I can do is something like this:

    MyObjectInstance.InputArray[50] = new PointF(100f, 200f);

    and the value will be properly stored. So I don't believe that it only takes an array, as I can index any value of the array.

    My goal is to update AdjustedArray[i] at the same time I set InputArray[i].

    Does that make any more sense?

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You are exposing your internals to the public, you won't get a notification if a value changes.

    Code:
    using System;
    using System.Drawing;
    
    namespace ConsoleApplication1
    {
        internal class Test
        {
            private PointF[] inputArray;
    
            public PointF[] InputArray
            {
                get
                {
                    Console.WriteLine(" G etter called");
    
                    return inputArray;
                }
                set
                {
                    Console.WriteLine(" S etter called");
    
                    inputArray = value;
                }
            }
        }
    
        internal class Program
        {
            static void Main()
            {
                Test MyObjectInstance = new Test();
    
                Console.WriteLine( "Calling Setter:");
    
                // this will call the setter. A new array gets set.
                MyObjectInstance.InputArray = new PointF[100];
    
                Console.WriteLine("Calling Getter:");
                // 
                // the following line will call the -> getter <-. The array is taken and one value of it is changed.
                // Not the array itself is changed, it's still at the same position.
                //
                // PointF[] array = MyObjectInstance.InputArray;
                // array[50] = new PointF(100f, 200f);
                //
                // would have the same effect
                //
                MyObjectInstance.InputArray[50] = new PointF(100f, 200f);
    
                Console.ReadLine();
            }
        }
    }
    Consider another design, the easiest way would be a setter function, maybe accompanied by a getter function returning a readonly ICollection of some sort.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    nvoigt,

    Thank you. I get it. Very good explanation. I think I will just take your advice and create a setter function.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You need a class that wraps the array assignment, so you can perform whatever actions you want on the assigned data (in your case a translation). Notice the self-indexer in PointList ( "this[int Index]" ), this is what gives the illusion of indexing the array while in fact you're indexing the class (see code at bottom).

    Code:
    	public class PointList
    	{
    		public PointList(System.Drawing.PointF[] Points, System.Drawing.PointF[] TranslatedPoints)
    		{
    			_Points = Points;
    			_TranslatedPoints = TranslatedPoints;
    		}
    
    		public System.Drawing.PointF this[int Index]
    		{
    			get
    			{
    				return _Points[Index]; //Retrieve the original point
    			}
    			set
    			{
    				_Points[Index] = value; //Store the original point
    				_TranslatedPoints[Index] = TranslatePoint(value); //Store the translated point
    			}
    		}
    
    		private System.Drawing.PointF TranslatePoint(System.Drawing.PointF Point)
    		{
    			//*** Translate somehow ***
    
    			return Point;
    		}
    
    		private System.Drawing.PointF[] _Points;
    		private System.Drawing.PointF[] _TranslatedPoints;
    	}
    
    	public class SomeClass
    	{
    		public SomeClass()
    		{
    			//Not sure if it's your intention to store both original and translated points,
    			//but if so you need two arrays
    			_Points = new System.Drawing.PointF[666];
    			_TranslatedPoints = new System.Drawing.PointF[666];
    
    			_InputArray = new PointList(_Points, _TranslatedPoints);
    		}
    
    		public PointList InputArray
    		{
    			get
    			{
    				//Expose the PointList class
    				return _InputArray;
    			}
    		}
    
    		private System.Drawing.PointF[] _Points = null;
    		private System.Drawing.PointF[] _TranslatedPoints = null;
    
    		private PointList _InputArray = null;
    	}
    Then you can use it like so:

    Code:
    var c = new SomeClass();
    c.InputArray[23] = new System.Drawing.PointF(12.34f, 56.78f);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Lists: adding fields (not nodes) & more
    By Mariano L Gappa in forum C++ Programming
    Replies: 15
    Last Post: 11-09-2005, 07:26 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM