I Have a dilemma: How to set a array so I can access from other functions inside the application (I've seen it done in Console Application, but I'm using Windows Application Templet)..., This is what I want:

public string[,] digit = new string[10, 3];
public string[] portion = new string[6];

portion[0] = " cents";
portion[1] = " dollar";
portion[2] = " dollars";
portion[3] = " Hundred";
portion[4] = " Thousand";
portion[5] = " Million";


digit[0, 0] = null;
digit[0, 1] = null;
digit[0, 2] = null;
digit[1, 0] = "One";
digit[1, 1] = "Eleven";
digit[1, 2] = "Ten";
digit[2, 0] = "Two";
digit[2, 1] = "Twelve";
digit[2, 2] = "Twenty";
digit[3, 0] = "Tree";
digit[3, 1] = "Thirteen";
digit[3, 2] = "Thirty";
digit[4, 0] = "Four";
digit[4, 1] = "Fourteen";
digit[4, 2] = "Forty";
digit[5, 0] = "Five";
digit[5, 1] = "Fifteen";
digit[5, 2] = "Fifty";
digit[6, 0] = "Six";
digit[6, 1] = "Sixteen";
digit[6, 2] = "Sixty";
digit[7, 0] = "Seven";
digit[7, 1] = "Seventeen";
digit[7, 2] = "Seventy";
digit[8, 0] = "Eight";
digit[8, 1] = "Eighteen";
digit[8, 2] = "Eighty";
digit[9, 0] = "Nine";
digit[9, 1] = "Nineteen";
digit[9, 2] = "Ninety";

then from my functions do something like

private string DgtStr(int x, int z)
{
return digit[x, z];
}

but digt and portion has to be a public (global) array accessible form anywhere...

Member Avatar for iamthwee

Yeah make em global =)

Declare your arrays as static in some class which is visible to all your application, and then you can access them as :
MyClass.digit[x,z]

public class ManageDigits
    {
        /// <summary>
        /// when the class is called the array is built for the first time
        /// </summary>
        public ManageDigits() {
            x_DigitSearchedFor[0 , 0] = string.Empty;
            x_DigitSearchedFor[0 , 1] = string.Empty;
            x_DigitSearchedFor[0 , 2] = string.Empty;
            x_DigitSearchedFor[1 , 0] = "One";
            x_DigitSearchedFor[1 , 1] = "Eleven";
            x_DigitSearchedFor[1 , 2] = "Ten";
            x_DigitSearchedFor[2 , 0] = "Two";
            x_DigitSearchedFor[2 , 1] = "Twelve";
            x_DigitSearchedFor[2 , 2] = "Twenty";
            x_DigitSearchedFor[3 , 0] = "Tree";
            x_DigitSearchedFor[3 , 1] = "Thirteen";
            x_DigitSearchedFor[3 , 2] = "Thirty";
            x_DigitSearchedFor[4 , 0] = "Four";
            x_DigitSearchedFor[4 , 1] = "Fourteen";
            x_DigitSearchedFor[4 , 2] = "Forty";
            x_DigitSearchedFor[5 , 0] = "Five";
            x_DigitSearchedFor[5 , 1] = "Fifteen";
            x_DigitSearchedFor[5 , 2] = "Fifty";
            x_DigitSearchedFor[6 , 0] = "Six";
            x_DigitSearchedFor[6 , 1] = "Sixteen";
            x_DigitSearchedFor[6 , 2] = "Sixty";
            x_DigitSearchedFor[7 , 0] = "Seven";
            x_DigitSearchedFor[7 , 1] = "Seventeen";
            x_DigitSearchedFor[7 , 2] = "Seventy";
            x_DigitSearchedFor[8 , 0] = "Eight";
            x_DigitSearchedFor[8 , 1] = "Eighteen";
            x_DigitSearchedFor[8 , 2] = "Eighty";
            x_DigitSearchedFor[9 , 0] = "Nine";
            x_DigitSearchedFor[9 , 1] = "Nineteen";
            x_DigitSearchedFor[9 , 2] = "Ninety";
        }
        /// <summary>
        /// depending on how this is used i may set it readonly
        /// this will prevent you from writing code that accidentally modifies
        /// the values within the array
        /// </summary>
        private string[,] x_DigitSearchedFor = new string[10 , 3];
        /// <summary>
        /// gets the position of the array at the specified points given
        /// </summary>
        /// <param name="PositionOne">first integer position</param>
        /// <param name="PositionTwo">second integer position</param>
        /// <returns>returns the position as string</returns>
        public string GetDigitAtPoint(int PositionOne , int PositionTwo) {
 
            this.x_PositionOne = PositionOne;   //for added functionality
            this.x_PositionTwo = PositionTwo;   //for added functionality
            return this.x_DigitSearchedFor[PositionOne , PositionTwo];
        }
        //now that we have the basic functionality we can spice it up a bit!
        private int x_PositionOne = 0 ,
                    x_PositionTwo = 0;
        public string GetNextDigit {
            get {
                this.x_PositionOne++;   //increase by one
                this.x_PositionTwo++;   //increase by two
                //return the result found
                string digitFound = string.Empty;
                if (this.x_PositionOne < this.x_DigitSearchedFor.GetLength(0) &&
                    this.x_PositionTwo < this.x_DigitSearchedFor.GetLength(1))
                {
                    digitFound = this.x_DigitSearchedFor[this.x_PositionOne , this.x_PositionTwo];
                } else
                {
                    this.x_PositionOne--;
                    this.x_PositionTwo--;
                }
                return digitFound;      //return the found digit
            }
        }
        /// <summary>
        /// gets the current digit that the positions are pointing too
        /// returns string.empty if outside the bounds of the array
        /// and also resets the positions to zero (if outside the bounds)
        /// </summary>
        public string GetCurrentDigit {
            get {
                string digitFound = string.Empty;
                if (this.x_PositionOne < this.x_DigitSearchedFor.GetLength(0) &&
                    this.x_PositionTwo < this.x_DigitSearchedFor.GetLength(1))
                {
                    digitFound = this.x_DigitSearchedFor[this.x_PositionOne , this.x_PositionTwo];
                } else
                    this.x_PositionOne = 0;    //reduce the value to zero
                    this.x_PositionTwo = 0;    //reduce the value to zero
                }
                return digitFound;      //return the found digit
            }
        }
        /// <summary>
        /// gets or sets the current first position within the array
        /// </summary>
        public int GetFirstPosition { get { return this.x_PositionOne; } set { this.x_PositionOne = value; } }
        /// <summary>
        /// gets or sets the second position within the array
        /// </summary>
        public int GetSecondPosition { get { return this.x_PositionTwo; } set { this.x_PositionTwo = value;}}
    }

as you can see you can really build a lot with this class, and to access the class is easy

if you lets say wanted position 5,2 you could write this into your code

//we are inside some method now
string digitFound = new ManageDigits().GetDigitAtPoint(5,2);

if you wanted to manipulate throughout a class you could write

namespace SomeNamespace
{
public class MyClass
{
public MyClass() {
//your initializer code here
}
private ManageDigits x_DigitManager = new ManageDigits();
}
}

now you can access the digit manager from any portion of your code within the class you are working with.

i hope this has helped you out some.

best regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.