Thread: Simple C# structure question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    1

    Simple C# structure question

    I think it's simple .. and if I'm doing it wrong, please just let me know .. I'm not so far in I can't fix it.

    I wanted a separate .cs file to hold my variables that my entire project would use. I created the class like this to house the variables:

    Code:
    namespace MyNamespace
    {
        public static class VariableDeclarations
        {
            public class Variables
            {
                public int[, ,] Playfield = new int[50, 50, 50];
            }
        }
    }
    This is very dumbed-down obviously.

    To access this class from the main program .cs file .. I had to use

    Code:
            VariableDeclarations.Variables Vars = new VariableDeclarations.Variables();
    ..outside of the classes so it could be accessed by all the classes in that .cs file.

    This worked great and I could use Vars.Playfield[x,y,z] without an issue, but when then I needed to create a method to initialize this playfield with 1.

    The method is set up like this and is also in it's own separate.cs file.

    Code:
    public class Initialize
    {
        //VariableDeclarations.Variables Vars = new VariableDeclarations.Variables();
        public static void InitializePlayfield()
        {
            VariableDeclarations.Variables Vars = new VariableDeclarations.Variables();
            Vars.ZLoop1 = 0;
            while (Vars.ZLoop1 < 12)
            {
                Vars.YLoop1 = 0;
                while (Vars.YLoop1 < 12)
                {
                    Vars.XLoop1 = 0;
                    while (Vars.XLoop1 < 12)
                    {
                        Vars.Playfield [Vars.XLoop1, Vars.YLoop1, Vars.ZLoop1] = 1;
                        Vars.XLoop1 = Vars.XLoop1 + 1;
                    }
                    Vars.YLoop1 = Vars.YLoop1 + 1;
                }
                Vars.ZLoop1 = Vars.ZLoop1 + 1;
            }
        }
    }
    Everything is in the same namespace.

    Now when I call this method, it doesn't save the values in the Playfield variable. When it comes to the point of accessing it in the main loop, the values are gone.

    I'm wondering why the values seem to be reset back to 0 or null after I go from one class to another. I'm very new to C#, so don't laugh.

    I was guessing it was because I had to use 2 separate:
    Code:
            VariableDeclarations.Variables Vars = new VariableDeclarations.Variables();
    ..commands in 2 separate .cs files and there were 2 instances of Vars. But then again, I thought Vars would be public and accessible across all of the .cs files.

    Also, please correct my terminology. I'm going all out and want to start this right and get in the good habits.

    Thanks in advance!

    Sinc'
    Sketch

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    You have a scope issue. Look at this for a second: http://en.wikibooks.org/wiki/C++_Pro...Scope/Examples

    Then look at your own code and see what is going on, i know the above link is in C++ but its the same for every language.

    You declare something inside a function, after exiting the function this variable ceases to exists. There are some ways to overcome this, return the variable on exiting the function. Or use a field from the class and use this field in your method by using this.yourVariableName.

    One other way is by passing the reference of the variable to the function being called (look into the keywords ref and out in C# - http://www.c-sharpcorner.com/UploadF...t_and_ref.aspx)

    Also when having an array as a field in a class that needs to be accessed from outside this class its usefull to create a getter for this field. Doing this you can then access anything inside the playfield array by doing something like VariableDeclarationsInstance.Playfield[somevalue][anothervalue].

    Also note that its a good habit to have your fields in a class start with a non capital letter. This way you can write all your getters and setters starting with a capital. Making code more readable (in my opinion, I'm sure other people will have other opinions about this).

    Oh and if you really want to do this with classes, just remove the static keyword, create a constructor for the class that will initialize all values (if needed). Later on access the fields using getters and setters (this way you can for instance already check that you are not trying to access an element from an array that does not even exists ...).

    Anyhow, to conclude, it seems that you are way over your head by trying to use all these things. First get the basics of any programming language right (like the out of scope thing), after that you can start to fiddle with creating classes that are either static or public.

    Do not get me wrong, one has to start somewhere, but it's better to learn things right from the start then to create your own way of achieving things while there are far better ways to achieve the same. So just look for some good tutorials on C# explaining most common things (you do not need to start learning how to use all classes from the .NET framework, this is simply impossible! - but just learn the basics of how everything works , classes, structs, constructor , destructor, try-n-catch although you should avoid these as much, difference between heap and stack, learn what goes onto the heap or stack and what not, and so on...).

    Many people think C# is simpler then let's say C++ because you do not need to do any memory management (in most cases). But I dont agree with this (to some extent), you need to know what is happening behind all the syntactical sugar and layer after layer already created for you by the .NET framework. When you know what is really going on, things like "why is the value set back to its default even though I change it here and there?" will nolonger be a problem for you.
    Last edited by GanglyLamb; 09-12-2007 at 02:45 AM.

  3. #3
    Sasquatch mog's Avatar
    Join Date
    Dec 2006
    Location
    Caves of Narshe
    Posts
    16
    Maybe i dont get the whole picture here.. but.

    If you want a class to hold all your variables/Datasets and equal, you need to declare the variable static also.

    For example:

    Data.cs
    Code:
    namespace Mog
    {
        public static class Data
        {
            private static ArrayList _objlist;
            
            static Data()
            {
                _objlist = new ArrayList();
            }
    
            public static ArrayList Objectlist
            {
                get
                {
                    return _objlist;
                }
            }
        }
    }
    OtherClass.cs:
    Code:
    namespace Mog
    {
        public class Sasquatch
        {
    
            // other stuff related to class
    
            public void PrintObjList()
            {
                foreach (ArrayList al in Data.ObjectList)
                {
                    System.Console.WriteLine(al.ToString());
                }
            }
        }
    }
    In this example i dont have any objects in my ArrayList, but the underlying system make sure that Data()-constructor run only once and that before any member is used anywhere.


    I would recommend to learn C++ without .NET before you start using C#.
    This because you understand how things work under the hood... (Just like someone already said i think.)
    Last edited by mog; 09-13-2007 at 11:34 AM. Reason: typo, typo, typo

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    Gloucester, England
    Posts
    11

    Cool Another way...

    Or if you just don't want your variables cluttering up your screen because you have so many, you can:

    1. Declare regions, #region regionlabel and #endregion in C#
    2. Declare your classes as Partial (with the same name). All partial classes are essentially the same class, only they can be anywhere in your program.


    Hope this helps.

    Pete

  5. #5
    Sasquatch mog's Avatar
    Join Date
    Dec 2006
    Location
    Caves of Narshe
    Posts
    16
    Quote Originally Posted by Pete_O View Post
    Or if you just don't want your variables cluttering up your screen because you have so many, you can:

    1. Declare regions, #region regionlabel and #endregion in C#
    2. Declare your classes as Partial (with the same name). All partial classes are essentially the same class, only they can be anywhere in your program.


    Hope this helps.

    Pete
    What??

    I dont think there is any room for a "or you can" -post!

    It is more like you´re sayin: "C# also works like this".
    You dont have to enter all code that belongs to a class inte the same file.. = Have nothing to do with the original question, assume that im just a page in a book.. read the question again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about the C programming tutorial #1
    By elsheepo in forum C Programming
    Replies: 13
    Last Post: 01-19-2009, 08:59 PM
  2. Passing by Reference. Simple question.
    By d3m105 in forum C Programming
    Replies: 6
    Last Post: 10-31-2007, 12:47 PM
  3. Data structure question.
    By ronenk in forum C Programming
    Replies: 9
    Last Post: 10-03-2004, 10:58 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. structure question
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-17-2002, 09:17 PM