Click here to Skip to main content
15,901,035 members
Home / Discussions / C#
   

C#

 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:09
Brian_TheLion14-Mar-19 1:09 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:19
Brian_TheLion14-Mar-19 1:19 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 1:54
mveRichard MacCutchan14-Mar-19 1:54 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:57
Brian_TheLion14-Mar-19 1:57 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 2:16
mveRichard MacCutchan14-Mar-19 2:16 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 2:22
Brian_TheLion14-Mar-19 2:22 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 2:25
mveRichard MacCutchan14-Mar-19 2:25 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 2:01
mveOriginalGriff14-Mar-19 2:01 
sr is declared in Main: (and please, format your code blocks!)
C#
public static void Main(string[] args)
    {
    Program prog = new Program();
    StreamReader sr = new StreamReader("demo.adv");
So it is a local variable, which only exists inside the curly brackets in which it is declared: outside the Main method, it doesn't exist, and can't be accessed. And more importantly, when Main ends, it will be destroyed. What you need to do is pass it to the functions that need it:
C#
public static void Main(string[] args)
    {
    Program prog = new Program();
    StreamReader sr = new StreamReader("demo.adv");
    
    Console.WriteLine("Start of Program");
    prog.y = 4;
    prog.ReadHeader(sr);
...    
    }
public void ReadHeader(StreamReader sr)
    {
    Console.WriteLine("Read Header");
    Text1 = sr.ReadLine(); 
    
    Console.WriteLine(y);
    i = 6;
    }
The other solution is to move sr out of Main, and make it class level:
C#
private StreamReader sr;
public static void Main(string[] args)
    {
    Program prog = new Program();
    prog.sr = new StreamReader("demo.adv");
    
    Console.WriteLine("Start of Program");
    prog.y = 4;
    prog.ReadHeader();
...    
    }
And then all the instance methods in you class can access it.

In all seriousness, this is basic stuff, which will be explained - carefully - in the Petzold book, and you really do need to read it before embarking on this project - or you will make so many mistakes that when you look back at it in three months time you'll cringe and wish you hadn't shown 13,889,528 other people your code... Laugh | :laugh:
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 2:13
Brian_TheLion14-Mar-19 2:13 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 2:31
mveOriginalGriff14-Mar-19 2:31 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 12:27
Brian_TheLion14-Mar-19 12:27 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 21:59
mveOriginalGriff14-Mar-19 21:59 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 23:43
Brian_TheLion14-Mar-19 23:43 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 23:47
mveOriginalGriff14-Mar-19 23:47 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion15-Mar-19 0:53
Brian_TheLion15-Mar-19 0:53 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:40
Brian_TheLion14-Mar-19 1:40 
QuestionRe: Trying to find the problem in this simple C$ program Pin
Luc Pattyn14-Mar-19 7:09
sitebuilderLuc Pattyn14-Mar-19 7:09 
AnswerRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 7:24
mveOriginalGriff14-Mar-19 7:24 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Luc Pattyn14-Mar-19 8:02
sitebuilderLuc Pattyn14-Mar-19 8:02 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 9:13
mveOriginalGriff14-Mar-19 9:13 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Luc Pattyn14-Mar-19 9:20
sitebuilderLuc Pattyn14-Mar-19 9:20 
QuestionDeleting items in group with a Condition [C#] Pin
Member 1418164913-Mar-19 20:34
Member 1418164913-Mar-19 20:34 
AnswerRe: Deleting items in group with a Condition [C#] Pin
OriginalGriff13-Mar-19 20:58
mveOriginalGriff13-Mar-19 20:58 
AnswerRe: Deleting items in group with a Condition [C#] Pin
Gerry Schmitz14-Mar-19 6:04
mveGerry Schmitz14-Mar-19 6:04 
GeneralRe: Deleting items in group with a Condition [C#] Pin
BillWoodruff15-Mar-19 0:17
professionalBillWoodruff15-Mar-19 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.