Here is my problem, I can create windows based form programs and can usually find out how to do things. However, I don't understand Object Orientated programming, is it much more complicated than what creating windows forms where the only separate modules seem to be forms which you can create instances of.
How would I integrate a method and make it work with a form? I know this is a very general question, but I really need to start learing how to create OO programs!
AlanPosted Sep 18, 2007, 5:21 PM
The .NET framework class library is itself built using OO principles and was in fact programmed mostly in C# by Microsoft.
The classes which are typically used to read and write text files are the StreamReader and StreamWriter classes both of which are in the System.IO namespace. Another class which is often used for string concatenation is the StringBuilder class which is in the System.Text namespace. The following code demonstrates the use of all three in a simple case :
// write some text to a new text file
StreamWriter sw = new StreamWriter("drew.txt");
sw.WriteLine("Apples");
sw.WriteLine("Oranges");
sw.WriteLine("Pears");
sw.Close();
// read it back again into a comma separated string
StreamReader sr = new StreamReader("drew.txt");
StringBuilder lines = new StringBuilder();
string line = null;
while ((line = sr.ReadLine()) != null)
{
lines.Append(line);
lines.Append(", ");
}
sr.Close();
// remove final comma and space, before converting to a string
lines = lines.Remove(lines.Length - 2, 2);
string result = lines.ToString();
// display in a messagebox
MessageBox.Show(result);
Drew TaylorPosted Sep 18, 2007, 1:25 PM
Thanks guys, you have both provided useful answers! I think I also need to learn about global/private/public variables etc I understand nearly all of the syntax and code but it's just understanding the OO bit!
I programmed an application which stored user input in text files and displayed it when required and I was using alot of duplicate code! If I understood OO would I call a method to create new text files and other stuff? I guess whatever I was repeating in the code could actually be made simpler and into a method?
Ollie McPheePosted Sep 18, 2007, 1:13 PM
You should really take some time to learn the general concepts of OO programming. It is quite simple to write small programs inside forms, but the larger your application the more difficult it will become and you will find yourself duplicating similar code in multiple forms.
These beginner videos on the MSN site are very good, I can reccomend the videos by Bob Tabor for a good overview.
OO can be difficult to get your head around at first - but once you've made that leap things will become a lot easier for you.
Cheers,
McPhee
AlanPosted Sep 18, 2007, 12:58 PM
Just define your method within the Form class by giving it a name, access modifier, return type ('void' if none) and specifying any parameters and their types.
If you only want to call the method within the form itself you can declare it as 'private' otherwise declare it as 'public'.
For example, here's a quick method which you can place in your Form class's code:
private int SumSeries(int start, int end, int step)
{
int total = 0;
for (int i = start; i <= end ; i += step)
{
total += i;
}
return total;
}
As you'll see it sums an arithmetic series of numbers. All you need to do to call it, is to specify the starting and ending numbers and the 'step' (i.e. the difference between each number in the series).
So, if you place this code in a button's click eventhandler and you have some textbox (textBox1 say) then 15 should appear in it when you click the button:
int total = SumSeries(1, 5, 1);
textBox1.Text = total.ToString();