Classes advice needed

This is a discussion on Classes advice needed within the C# Programming forums, part of the General Programming Boards category; I've successfully created a class with um, well, here: Code: using System; namespace SP2ClassLibrary { public class TechClass { /// ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    129

    Classes advice needed

    I've successfully created a class with um, well, here:

    Code:
    using System;
    
    namespace SP2ClassLibrary
    {
        public class TechClass
        {
            /// <summary>
            /// Agricultural Tech
            /// </summary>
            public int[] AT = new int[12]; // 0
            /// <summary>
            /// Birth Labs Tech
            /// </summary>
            public int[] BL = new int[12]; // 1
            /// <summary>
            /// Build Points Tech
            /// </summary>
            public int[] BP = new int[12]; // 2
    
            ... There is a lot more like this too...
        }
    }
    And I obviously start this with a normal 'TechClass tech = new TechClass();' (with the appropriate using).

    I will obviously change it to a get/set system if you seriously recommend doing so.

    First off, what is the best way to convert the property from tech.AT[x] to tech[x].AT? Make it TechClass[] tech = new TechClass[12];? I do suspect that would be the way. BTW, x is the player number, and the letters represent the specific technology level of the player, AT is agricultural tech, so tech.AT[0] is the Agricultural Tech Level of player zero.

    After switching this around, I would like to understand how to add um, submethods (sorry, names ALWAYS escape me!). For example, my program does a lot of calculations using these variables, especially text output to a form. One such case is properly formatting this on a form. It would be a lot more readable to have something such as tech[0].AT.Name (as a string type), or tech[0].AT.SpacedString that outputs the variable number (ie. 1) as a string with a set of preceding spaces. My aim is to achieve a very high level of readability in my code, and also to learn more about classes. Anyone up to this?
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    The easiest way is to dive down a bit deeper and instead of representing all the players levels in one object, represent each player with those attributes as an object. Then keep a collection of those objects.

    Code:
    namespace PlayerObject
    {
      public class Player
      {
         public struct AttributesStruc
         {
            public int AgTech;
            public int BirthLabTech;
            public int BuildPointsTech;
         }
     
         AttributesStruc m_MyAttributes;
         public AttributesStruc Attributes
         {
            get
            {
               return m_MyAttributes;
            }
         }
      
      }
    }
    This extra level of abstraction will help you later.
    Last edited by VirtualAce; 12-01-2007 at 01:46 PM.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Thanks for the assistance. I tried it that way, but it just didn't work quite well with some of the stuff I am doing inside my program.

    I found that Extension Methods is what I needed. With enumeration and other bits, I can now do exactly what I wanted, and the code is much more readable than I even thought it would be!

    I just have one issue right now...

    The class(es) have now become:

    Code:
    public class SystemClass
    {
        //got a constructor    
        private int _Owner;
        public int Owner
        {
            get { return _Owner; }
            set { _Owner= value }
        }
        //...there's more like this, with limits, exceptions and stuff
    }
    so I'd create it with:

    Code:
    SystemClass[] system= new SystemClass[600]; // you think i'm kidding...?
    for (int i = 0; i < 600 ; i++ )
    {
        system[i] = new SystemClass();
    }
    However, I want a property/variable called AdjSystem, to be an array of size 7. So I'd write it as system[x].AdjSystem[y] . This is the only one in this style, as I am wanting the ability to iterate through the number of adjacent systems this system has. I currently have this in the constructor:

    Code:
    public SystemClass
    {
        public SystemClass()
        {
            int[] _AdjSystem = new int[7];
            //... and other bits in here
        }
        private int[] _AdjSystem;
        public int AdjSystem
        {
            get{...}
            set{...}
        }
    }
    This only initialises as null, not the desired array. How can I do this?
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Are you trying to initialize the adj array? Are you doing the same for loop you did with the other array in which you new each object in the adj array? (unless you are assigning them to existing objects.)

    Not really sure what your question is here.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by DanFraser View Post
    However, I want a property/variable called AdjSystem, to be an array of size 7. So I'd write it as system[x].AdjSystem[y] . This is the only one in this style, as I am wanting the ability to iterate through the number of adjacent systems this system has. I currently have this in the constructor:

    Code:
    public SystemClass
    {
        public SystemClass()
        {
            int[] _AdjSystem = new int[7];
            //... and other bits in here
        }
        private int[] _AdjSystem;
        public int AdjSystem
        {
            get{...}
            set{...}
        }
    }
    This only initialises as null, not the desired array. How can I do this?
    Answering in reverse. First, notice that in your SystemClass constructor, you are declaring a local _AdjSystem which you are initializing. That local goes out of scope at the end of the constructor. Meanwhile, your class-scope _AdjSystem is never initialized. Try this:
    Code:
        public SystemClass()
        {
            _AdjSystem = new int[7];
            //... and other bits in here
        }
    If you want the system[x].AdjSystem[y] syntax, you might consider an indexer property instead of exposing the whole array to everything outside of the class.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Well I figured it out last night.

    Code:
    private int[] _AdjSystem = new int[7];
        public int AdjSystem
        {
            get{...}
            set{...}
        }
    Solves it, works perfectly.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc configuration under linux advice needed
    By vart in forum Tech Board
    Replies: 9
    Last Post: 01-10-2007, 02:46 PM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 02:46 AM
  3. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 11:19 AM
  4. Help With Inherited Classes Needed
    By elliott in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2005, 09:05 AM
  5. Help Needed On Classes
    By mindofpoison in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2005, 07:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21