I found this strange in C#
-
I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian
-
I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian
A class should be a black box, where you don't have to know about the inside, and can't muck up the internal state from the outside. The parts where you find you need access to a variable, you'll often need a property.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
A class should be a black box, where you don't have to know about the inside, and can't muck up the internal state from the outside. The parts where you find you need access to a variable, you'll often need a property.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
Thanks Eddy for giving me another way of understanding things in C#. Correct me if I'm wrong but I'm also thinking that an instance variable is a reference to a variable. Brian
-
I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian
It sounds like you're not using OOP at all and just declaring all of your variable and classes as static. That's not good practice. But, that's just a guess as we can't see you're code and you haven't posted any.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
It sounds like you're not using OOP at all and just declaring all of your variable and classes as static. That's not good practice. But, that's just a guess as we can't see you're code and you haven't posted any.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakHi Dave. Sorry but my code is a bit messy as I'm experimenting while learning C#. At the bottom of the code you'll find num=i; then near the top I have numberOfVerbs = num; I could not use numberOfVerbs = i; I have found out since that I could have used the .long command to find out how many CollectText's created. I use to be able to run the code without errors but something has changed to cause some errors.
namespace Lads_in_CSharp
{
public class Program
{
int i = 0;
int num;
int numberOfVerbs;
int y=0;
int a = 0;
//String sr;
String Text1="";
String[] CollectText = new String[1000];
String [] TT = new String[3] ;
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); Console.WriteLine(prog.i); prog.ReadVerbs(sr); prog.ReadNouns(sr); prog.ReadObjects(sr); prog.ReadStartRoom(sr); prog.ReadRooms(sr); prog.ReadMessages(sr); prog.ReadAutoActions(sr); prog.ReadActions(sr); sr.Close(); Console.Read(); } public void ReadHeader(StreamReader sr) { Console.WriteLine("Read Header"); ReadLineOfText(sr); Console.WriteLine("Text1 is: " + sr); // Console.WriteLine(y); // i = 6; } public void ReadVerbs(StreamReader sr) // VERBS { Console.WriteLine("Read Verbs++++++++++"); // Verb1 = "Paint"; int i=0; ReadLineOfText(sr); numberOfVerbs = num; Console.WriteLine("Text1 is:: " + Text1); Console.WriteLine("Collect Text 2 = "+CollectText\[2\]); // TEST Console.WriteLine("Number of Verbs is "+num); // string\[\] authorsList = Text1.Split(","); Console.WriteLine("Comma separated strings"); // String of authors // string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar"; // Split authors separated by a comma followed by space //string\[\] author
-
Hi Dave. Sorry but my code is a bit messy as I'm experimenting while learning C#. At the bottom of the code you'll find num=i; then near the top I have numberOfVerbs = num; I could not use numberOfVerbs = i; I have found out since that I could have used the .long command to find out how many CollectText's created. I use to be able to run the code without errors but something has changed to cause some errors.
namespace Lads_in_CSharp
{
public class Program
{
int i = 0;
int num;
int numberOfVerbs;
int y=0;
int a = 0;
//String sr;
String Text1="";
String[] CollectText = new String[1000];
String [] TT = new String[3] ;
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); Console.WriteLine(prog.i); prog.ReadVerbs(sr); prog.ReadNouns(sr); prog.ReadObjects(sr); prog.ReadStartRoom(sr); prog.ReadRooms(sr); prog.ReadMessages(sr); prog.ReadAutoActions(sr); prog.ReadActions(sr); sr.Close(); Console.Read(); } public void ReadHeader(StreamReader sr) { Console.WriteLine("Read Header"); ReadLineOfText(sr); Console.WriteLine("Text1 is: " + sr); // Console.WriteLine(y); // i = 6; } public void ReadVerbs(StreamReader sr) // VERBS { Console.WriteLine("Read Verbs++++++++++"); // Verb1 = "Paint"; int i=0; ReadLineOfText(sr); numberOfVerbs = num; Console.WriteLine("Text1 is:: " + Text1); Console.WriteLine("Collect Text 2 = "+CollectText\[2\]); // TEST Console.WriteLine("Number of Verbs is "+num); // string\[\] authorsList = Text1.Split(","); Console.WriteLine("Comma separated strings"); // String of authors // string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar"; // Split authors separated by a comma followed by space //string\[\] author
Yeah, you've thrown all the variables at the class level. That's bad. You have no reason to do that at all. Rewrite the thing from scratch, but think about what each method is going to handle, what it's going to need to do that job, and what it's going to return. Each method is going to need its own variables, so declare them in that method. In this code, you've got no reason to have ANY class level variables at all.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian
I would strongly urge you to stop trying to write code until you have worked through the two books that I have suggested. Unless and until you fully understand the basics you will be falling over simple issues like this. There are no shortcuts to becoming a developer, it requires time, effort and practice, practice, practice.
-
I would strongly urge you to stop trying to write code until you have worked through the two books that I have suggested. Unless and until you fully understand the basics you will be falling over simple issues like this. There are no shortcuts to becoming a developer, it requires time, effort and practice, practice, practice.
I understand what your saying Richard and like you said it takes practice which is what I'm doing in writing some code. I see it like trying to drive a car. You can read a lot but you need to get behind the wheel and start driving to get the practice. I only submitted code because I was asked to do so by someone giving me some feedback. If I can write a simple example program from something I've learnt from a book and it successfully works then I move on to learn more of the C# programming. Brian
-
Yeah, you've thrown all the variables at the class level. That's bad. You have no reason to do that at all. Rewrite the thing from scratch, but think about what each method is going to handle, what it's going to need to do that job, and what it's going to return. Each method is going to need its own variables, so declare them in that method. In this code, you've got no reason to have ANY class level variables at all.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakThanks David for your feedback. Well it started as a old program I wanted to convert to C# and I decided toturn methods into classes to try and separate the code and give me some practice on using classes. The program collects information from a script file then compiles and writes the compiled data to file which is loaded into the main program. While I started to write the program code to create a suitable program I'm now using the code to experiment with and try out things I have learnt. The code is likely to be re-wriiten. Brian
-
Thanks Eddy for giving me another way of understanding things in C#. Correct me if I'm wrong but I'm also thinking that an instance variable is a reference to a variable. Brian
Doesn't have to be; you can also have a value-type as an instance variable. If it is not a value-type, then it is indeed a reference.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian