Thread: "public" public objects

  1. #1
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94

    "public" public objects

    Say I have a public class "class1" and in it is a public int "myint".
    No matter where I create an object with that class, it's not truly global, because it only works in the function I define it in. So if I define it in main it doesnt travel to any other functions. I get an error if I try to define it in just the class.

    What I'm asking is how to I make a class that I can make an object with that can be used anywhere in my program?
    Boy you stink, go take a shower before you continue to code. Better do your laundry and spray your chair too.

    The one and only resource for finding information.

    Next version of windows released!!!!

  2. #2
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Code:
    namespace mynamespace
    {
        public class myclass
        {
            public int asdf = 1;
            //other stuff
         }
    }
    That seems to work for me. What error are you getting when you just define it in the class?
    To code is divine

  3. #3
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    Code:
    using System;
    
    namespace Consoleapp1
    {
                    public class Character
    	{
    		public string name;
    		public int level;
    		public int xp;
    		public int attack;
    		public int damage;
    		public int hp;
    		public int str;
    		public int dex;
    		public int con;
    		public int ranged;
    		public int melee;
    		public int blocking;
    		public int dodging;
    	}
    	public class Class1
    	{
    		public static void Main(string[] args)
    		{
    		}
    		public static void CharGen()
    		{
    		}
    		public static void SetAllZero()
    		{
    		}
    	}
    }
    Basically what I am trying to do is create an object somewhere with the class "Character", and be able to modify the variables of that object in any function. I can only get it to work with one function. If I make an object in main called "player" with the class "Character" I can't modify the object's variables in "SetAllZero()"
    Boy you stink, go take a shower before you continue to code. Better do your laundry and spray your chair too.

    The one and only resource for finding information.

    Next version of windows released!!!!

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    First, you will need a constructer for the class Character.

    After that, in main, declare your character as:
    Code:
     Character g = new Character(any values you need here);
    Then in SetAllZero, pass g and do whatever you need there.
    Code:
    SetAllZero(Character g)
    That's how I got it to work anyways.


    EDIT: This is the code I used, you should be able to alter it to your needs:
    Code:
    using System;
    
    namespace Consoleapp1
    {
        public class Character
    	{
    		public string name;
    		public int level;
    		public int xp;
    		public int attack;
    		
    		public Character(string n)
    		{
    			this.name = n;
    			this.level = 0;
    			this.xp = 0;
    			this.attack = 0;
    		}
    	}
        public class Class1
    	{
    		
    		public static void Main(string[] args)
    		{
    			Character g = new Character("bob");
    			System.Console.Write(g.name);
    			SetAllZero(g);
    		}
    		public static void CharGen(string n)
    		{
    		}
    		public static void SetAllZero(Character g)
    		{
    			g.attack = 1;
    			System.Console.Write(g.attack);
    		}
    	}
    }
    To code is divine

  5. #5
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    Great! Thanks it works perfectly. I had forgotton about this. One last question, how do I clear the screen of a console window?
    Boy you stink, go take a shower before you continue to code. Better do your laundry and spray your chair too.

    The one and only resource for finding information.

    Next version of windows released!!!!

  6. #6
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    http://support.microsoft.com/default...b;en-us;319257

    I don't know if there is a normal method or not.
    To code is divine

  7. #7
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    I have encountered a new problem. I included the clearing function you supplied above. No matter what I do, anything I put in between the two while statements gets skipped over like it's not even there. The switches only go through 4 times, and it should be 5.

    Code:
    using System;
    using nsClearConsole;
    
    
    namespace Consoleapp1
    {
    	public class Character
    	{
    		public string name;
    		public int level;
    		public int xp;
    		public int attack;
    		public int str;
    		public int dex;
    		public int con;
    		public int hp;
    		public int ac;
    		public int ranged;
    		public int melee;
    		public int dodge;
    		public int block;
    		public int dmg;
    		
    		public Character()
    		{
    			this.level = 0;
    			this.xp = 0;
    			this.attack = 0;
    			this.str = 0;
    			this.dex = 0;
    			this.con = 0;
    			this.hp = 0;
    			this.ac = 0;
    			this.ranged = 0;
    			this.melee = 0;
    			this.dodge = 0;
    			this.block = 0;
    			this.dmg = 0;
    			this.name = "";
    		}
    	}
    	public class Class1
    	{
    		
    		public static void Main(string[] args)
    		{
    			Character player = new Character();
    			Console.Write("Welcome to C# test game\n");
    			Console.Write("Press enter to start creating your character\n");
    			Console.ReadLine();
    			CharGen(player);
    		}
    		public static void CharGen(Character player)
    		{
    			ClearConsole ClearMyConsole = new ClearConsole();
    			ClearMyConsole.Clear();
    			Console.Write("Enter your name:");
    			player.name = Console.ReadLine();
    			ClearMyConsole.Clear();
    			Console.WriteLine("You have 5 attribute points, please choose where to put them.");
    			Console.ReadLine();
    			int points = 5;
    			player.str = 10;
    			player.dex = 10;
    			player.con = 10;
    			int temp;
    			while(points != 0)
    			{
    				ClearMyConsole.Clear();
    				Console.WriteLine("(1)Strength-{0}\n(2)Dexterity-{1}\n(3)Consitution-{2}\nPoints remaining-{3}",player.str,player.dex,player.con,points);
    				temp = Console.Read();
    				switch(temp)
    				{
    					case '1':
    						player.str++;
    						points--;
    						break;
    					case '2':
    						player.dex++;
    						points--;
    						break;
    					case '3':
    						player.con++;
    						points--;
    						break;
    					default:
    						break;
    				}//switch
    			}//while
    			Console.WriteLine("Hello");
    			Console.WriteLine("Hello");
    			Console.WriteLine("Hello");
    			Console.ReadLine();
    			Console.WriteLine("Hello");
    			Console.WriteLine("Hello");
    			Console.WriteLine("Hello");
    			points = 5;
    			while(points != 0)
    			{
    				ClearMyConsole.Clear();
    				Console.WriteLine("(1)Ranged-{0}\n(2)Melee-{1}\n(3)Blocking-{2}\n(4)Dodging-{3}\nPoints remaining-{4}",player.ranged,player.melee,player.block,player.dodge,points);
    				temp = Console.Read();
    				switch(temp)
    				{
    					case '1':
    						player.ranged++;
    						points--;
    						break;
    					case '2':
    						player.melee++;
    						points--;
    						break;
    					case '3':
    						player.block++;
    						points--;
    						break;
    					case '4':
    						player.dodge++;
    						points--;
    						break;
    					default:
    						break;
    				}//switch
    			}//while
    		}//main
    	}//class
    }//namespace
    (class2) for the console clearing code

    Code:
    using System;
    using System.Runtime.InteropServices;
    
    namespace nsClearConsole
    {
    	/// <summary>
    	/// Summary description for ClearConsole.
    	/// </summary><BR/>
    	public class ClearConsole
    	{
    		private const int STD_OUTPUT_HANDLE  = -11;
    		private const byte EMPTY = 32;
    
    		[StructLayout(LayoutKind.Sequential)]
    			struct COORD
    		{
    			public short x;
    			public short y;
    		}
    
    		[StructLayout(LayoutKind.Sequential)]
    			struct SMALL_RECT
    		{
    			public short Left;
    			public short Top;
    			public short Right;
    			public short Bottom;
    		}
    
    		[StructLayout(LayoutKind.Sequential)]
    			struct	CONSOLE_SCREEN_BUFFER_INFO
    		{
    			public COORD dwSize;
    			public COORD dwCursorPosition;
    			public int wAttributes;
    			public SMALL_RECT srWindow;
    			public COORD dwMaximumWindowSize;
    		}
    
    		[DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    		private static extern int GetStdHandle(int nStdHandle);
    
    		[DllImport("kernel32.dll", EntryPoint="FillConsoleOutputCharacter", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    		private static extern int FillConsoleOutputCharacter(int hConsoleOutput, byte cCharacter, int nLength, COORD dwWriteCoord, ref int lpNumberOfCharsWritten);
    
    		[DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    		private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
    
    		[DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    		private static extern int SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);
    
    		private int hConsoleHandle;
    
    		public ClearConsole()
    		{
    			//
    			// TODO: Add constructor logic here.
    			//
    			hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    		}
    
    		public void Clear()
    		{
    			int hWrittenChars = 0;
    			CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();
    			COORD Home;
    			Home.x = Home.y = 0;
    			GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo);
    			FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars);
    			SetConsoleCursorPosition(hConsoleHandle, Home);
    		}
    	}
    }
    Last edited by VOX; 06-23-2005 at 08:59 AM.
    Boy you stink, go take a shower before you continue to code. Better do your laundry and spray your chair too.

    The one and only resource for finding information.

    Next version of windows released!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-08-2005, 12:05 AM
  3. Collision detection algorithm
    By Hannwaas in forum Game Programming
    Replies: 5
    Last Post: 11-30-2001, 01:27 PM
  4. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM
  5. Passing objects
    By Wiggin in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 08:00 AM