ok i have a huge problem...everytime i access a protected variable outside the class where it was declared in it gives me the error message of
"object reference not set to an instance of an object"...if i access it in the same class as i declared it in it doesnt give me the error...so probably the problem is in the inhereting of the variable from the class to another class....heres the code
Memory2.cs
decode.csCode:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SML { class Memory2 { protected int[] memory; /*** REGISTERS ***/ protected int accumulator; protected int programCounter; protected int instructionRegister; protected int operationCode; protected int operand; public void Memory() { Initialize(); } public void Initialize() { int index = 0; memory = new int[100]; try { for (; index < 100; index++) { memory[index] = 0; } } catch(Exception e){ Console.WriteLine("Message: "+e.ToString()); } } public void Dump() { int i = 0,dummy = 0, dummyAccum = 0; Console.WriteLine("REGISTERS:"); if (accumulator >= 0) { if (accumulator.ToString().Length == 1) Console.WriteLine("Accumulator: \t +000" + accumulator); else if (accumulator.ToString().Length == 2) Console.WriteLine("Accumulator: \t +00" + accumulator); else if (accumulator.ToString().Length == 3) Console.WriteLine("Accumulator: \t +0" + accumulator); else Console.WriteLine("Accumulator: \t +" + accumulator); } else { dummyAccum = Math.Abs(accumulator); if (dummyAccum.ToString().Length == 1) Console.WriteLine("Accumulator: \t -000" + dummyAccum); else if (dummyAccum.ToString().Length == 2) Console.WriteLine("Accumulator: \t -00" + dummyAccum); else if (dummyAccum.ToString().Length == 3) Console.WriteLine("Accumulator: \t -0" + dummyAccum); else Console.WriteLine("Accumulator: \t -" + dummyAccum); } if(programCounter.ToString().Length == 1) Console.WriteLine("Program Counter: \t 0" + programCounter); else Console.WriteLine("Program Counter: \t " + programCounter); if (instructionRegister >= 0) { if (instructionRegister.ToString().Length == 1) Console.WriteLine("Instruction Register: +000" + instructionRegister); else if (instructionRegister.ToString().Length == 2) Console.WriteLine("Instruction Register: +00" + instructionRegister); else if (instructionRegister.ToString().Length == 3) Console.WriteLine("Instruction Register: +0" + instructionRegister); else Console.WriteLine("Instruction Register: +" + instructionRegister); } else { instructionRegister = Math.Abs(instructionRegister); if (instructionRegister.ToString().Length == 1) Console.WriteLine("Instruction Register: -000" + instructionRegister); else if (instructionRegister.ToString().Length == 2) Console.WriteLine("Instruction Register: -00" + instructionRegister); else if (instructionRegister.ToString().Length == 3) Console.WriteLine("Instruction Register: -0" + instructionRegister); else Console.WriteLine("Instruction Register: -" + instructionRegister); } if(operationCode.ToString().Length == 1) Console.WriteLine("Operation Code: \t 0"+ operationCode); else Console.WriteLine("Operation Code: \t " + operationCode); if(operand.ToString().Length == 1) Console.WriteLine("Operand: \t\t 0"+ operand); else Console.WriteLine("Operand: \t\t " + operand); Console.WriteLine("\nMEMORY: "); Console.Write(" 0 1 2 3 4 5 6 7 8 9\n"); for (i = 0;i<100 ;i++) { if (memory[i] >= 0) { displayMemoryNumber(i); if (memory[i].ToString().Length == 1) Console.Write("+000" + memory[i] + " "); else if (memory[i].ToString().Length == 2) Console.Write("+00" + memory[i] + " "); else if (memory[i].ToString().Length == 3) Console.Write("+0" + memory[i] + " "); else Console.Write("+" + memory[i] + " "); } else { dummy = Math.Abs(memory[i]); if (dummy.ToString().Length == 1) Console.Write("-000" + dummy + " "); else if (dummy.ToString().Length == 2) Console.Write("-00" + dummy + " "); else if (dummy.ToString().Length == 3) Console.Write("-0" + dummy + " "); else Console.Write("-" + dummy + " "); } } } private void displayMemoryNumber(int index) { switch(index) { case 0: Console.SetCursorPosition(0, 9); Console.Write(index + " "); break; case 10: Console.SetCursorPosition(0, 10); Console.Write(index + " "); break; case 20: Console.SetCursorPosition(0, 11); Console.Write(index + " "); break; case 30: Console.SetCursorPosition(0, 12); Console.Write(index + " "); break; case 40: Console.SetCursorPosition(0, 13); Console.Write(index + " "); break; case 50: Console.SetCursorPosition(0, 14); Console.Write(index + " "); break; case 60: Console.SetCursorPosition(0, 15); Console.Write(index + " "); break; case 70: Console.SetCursorPosition(0, 16); Console.Write(index + " "); break; case 80: Console.SetCursorPosition(0, 17); Console.Write(index + " "); break; case 90: Console.SetCursorPosition(0, 18); Console.Write(index + " "); break; } } } }
Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SML { class decode:Memory2 { public void LoadtoMemory(int address, int value) { //check if the memory address is valid if ((address >= 0) && (address <= 99)) memory[address] = value; else Console.Write("\nError: Invalid address at " + address + ".\n"); } public void display() { programCounter = 0; for (; programCounter <= 99; programCounter++) { if (memory[programCounter] == 4300) { Console.WriteLine("\t\t\t*******Program Ended*******"); break; } else { Console.Clear(); operationCode = memory[programCounter] / 100; operand = memory[programCounter] % 100; instructionRegister = memory[programCounter]; Dump(); decode2(); Console.ReadLine(); } } } public void display2() { programCounter = 0; for (; programCounter <= 99; programCounter++) { if (memory[programCounter] == 4300) { Console.WriteLine("\t\t\t*******Program Ended*******"); break; } else { Console.Clear(); operationCode = memory[programCounter] / 100; operand = memory[programCounter] % 100; //Dump(); decode2(); } } } public void decode2() { if (operationCode == 10) { read(); } else if (operationCode == 11) { write(); } else if (operationCode == 20) { load(); } else if (operationCode == 21) { store(); } if (operationCode == 30) { add(); } if (operationCode == 31) { subtract(); } if (operationCode == 32) { multiply(); } if (operationCode == 33) { divide(); } if (operationCode == 40) { } if (operationCode == 41) { branchneg(); } if (operationCode == 42) { } if (operationCode == 43) { } } public void branch() { programCounter = operand - 1; } public void branchneg() { if (accumulator < 0) { programCounter = operand - 1; } } public void branchzero() { if (accumulator == 0) { programCounter = operand - 1; } } public void read() { Console.Write("INPUT: "); memory[operand] = int.Parse(Console.ReadLine()); } public void write() { Console.WriteLine(memory[operand]); } public void load() { accumulator = memory[operand]; } public void store() { memory[operand] = accumulator; } public void add() { accumulator = accumulator + memory[operand]; } public void subtract() { accumulator = accumulator - memory[operand]; } public void multiply() { accumulator = accumulator * memory[operand]; } public void divide() { accumulator = accumulator / memory[operand]; } } }
pls help guyz...every reply would be appreciated...nosebleed na kaayo ko ani gamay na lang kuwang




Reply With Quote