simple question regarding windows programming
I've been teaching myself programming just for the heck of it for a while now, and I've decided to migrate from console applications to Windows applications (who the heck wants to use a console application anyway :p ). There are a lot of things that are confusing to me, but I have two main questions for now. These are probably more of a general OOP nature, but C# is the language I'm using, so I figured I would ask here.
I'm trying to write a simple flashcard program. Let's say I have an OpenFile() function that opens a text file, reads the data, and puts each Q&A pair into it's own Flashcard object, then stores these Flashcard objects in an array. My question is, where do I "keep" these objects so that I can access them for the duration of the program? My understanding is that once the OpenFile() function runs its course, the objects will be out of scope and will summarily be destroyed.
In a console application, I would just declare the Flashcard objects/array in main() (or whatever function everything is running from), and pass them around as needed. I just can't wrap my head around how this works in a GUI environment, given that there doesn't seem to be any "foundation" that everything runs off of.
My second question is, assuming I have some sort of ShowQuestion() function in my Flashcard class, how do I get this function to have access to the objects on the form (textboxes, labels, etc)? Windows programming is very confusing compared to console programming :eek:
[1558 byte] By [
kefka95] at [2007-11-20 11:16:56]

# 1 Re: simple question regarding windows programming
You will probably have to load the data first and get it all prepared and then make the data static so it will stay for the remainder of the programs life. Then depending on how you have things set up, you could either pass them to the classes via construction, or use properties (get {} set{}), or pass them through method calls.
Oh, and by the way, Cyan totally ruled Kefka ;) lol
# 2 Re: simple question regarding windows programming
Hi !
You are right ! Windows programming is totally other then console programming. Beginning from the design pattern. You dont have to think inthis endless looping cycle which goes from question to question.
You may have stored your questions and answers in a database and loading the data into a class at beginning then depending on which style you want e.g multiple choice: you will have some questions on a form and the answers with option buttons so you can choose the answer. a button is to be pressed and the next question occures and in a list you store if the answer was correct or wrong.
So the going forward in this case is driven by the click delegate of the answer button.
Your startup code only showes the form, loads the data and presents the first question and its possible answers or maybe some of them
If you have some questions on one page you may have a 'next' button instead of an 'answer' button.
The user answers e.g. 4 questions,then pressing next, gets the next for questions, answers them, presses next and so on.
The data doesn't need to be static They life as long as the dataClass lifes which you build and in which you have loaded all the questions from your database.
Multiple choice Q&A is very typical for computed solutions as a machine is no human and has problems to evaluate sentences for checking if an answer was correct. It might be useful if the answer is only a single word. So e.g if you are asking Who was the friend of Asterix :? you may only allow Obelix as an answer maybe. Also mathematical results doesn't need multiple choice here you may use a Textbox as an answerfield.
So basically you have to decide at designtime whats the answers would be and how the best way to get measureable results for correct / incorrect.
The biggest difference I would say between a console application and a windows application is that a windows application is message driven, a console Application very often runs on loops. Or its an application whorking with the user prompt at its commandline like
MyConsoleApp -f:c:\myPath\MyFile -v -r
Something like that to show what I'm talking about. as I have problems to express this in English. But also this more and more gets to a userfriendly design with a nice Form for choosing options and entering data.
So I would suggest you to read some basic books about Windows Forms programming, there are some good books on the market showing easy examples to get the designpattern behind.
# 3 Re: simple question regarding windows programming
Thank you for the replies, and sorry for my late reply, I had to take a bit of a break from my little project. Here is a very simplified version of what I don't understand. The code is pretty unorganized, but the important thing is at the end, when I actually create the flashcard objects.
What do I "do" with these objects to prevent them from being destroyed and in order for button2 (or any other object) to have access to them? Again, sorry if this is a silly question, I just don't get it :cry:
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int count = 0;
string temp = "";
string filename = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
filename = (openFileDialog1.FileName.ToString());
FileInfo flashcardFile = new FileInfo(filename);
StreamReader inFile = flashcardFile.OpenText();
do
{
temp = inFile.ReadLine();
count++;
} while (temp != null);
int totalCards = (count - 1) / 2;
string result = "Read " + totalCards + " flaschards!";
label1.Text = result;
Card[] flashcards = new Card[totalCards];
for (int x = 0; x < totalCards; x++)
flashcards[x] = new Card();
// code for setting questions & answers here
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
# 4 Re: simple question regarding windows programming
namespace WindowsApplication2{
public partial class Form1 : Form{
private int formsLifetime; // example where to put Fields with Lifetime of the form itself
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
int count = 0; // Lifetime for lifetime of this method
string temp = "";
string filename = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
filename = (openFileDialog1.FileName.ToString());
FileInfo flashcardFile = new FileInfo(filename);
StreamReader inFile = flashcardFile.OpenText();
do{
temp = inFile.ReadLine();
// you need to strore your temp line into an array for every times going through that here
count++;
} while (temp != null);
int totalCards = (count - 1) / 2; // ? heee you read count lines of ONE Flashcard here
string result = "Read " + totalCards + " flaschards!";
label1.Text = result;
Card[] flashcards = new Card[totalCards]; // whats this ? Do you have a Card Class if yes show code
for (int x = 0; x < totalCards; x++)
flashcards[x] = new Card();
// code for setting questions & answers here
}
}
}
As you said its a bit disorganised as this all will never work in the moment I guess it will not compile too. For getting variables useable the whole time the form exists set them as private variables into your Form class
# 5 Re: simple question regarding windows programming
namespace WindowsApplication2{
public partial class Form1 : Form{
private int formsLifetime; // example where to put Fields with Lifetime of the form itself
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
int count = 0; // Lifetime for lifetime of this method
string temp = "";
string filename = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
filename = (openFileDialog1.FileName.ToString());
FileInfo flashcardFile = new FileInfo(filename);
StreamReader inFile = flashcardFile.OpenText();
do{
temp = inFile.ReadLine();
// you need to strore your temp line into an array for every times going through that here
count++;
} while (temp != null);
int totalCards = (count - 1) / 2; // ? heee you read count lines of ONE Flashcard here
string result = "Read " + totalCards + " flaschards!";
label1.Text = result;
Card[] flashcards = new Card[totalCards]; // whats this ? Do you have a Card Class if yes show code
for (int x = 0; x < totalCards; x++)
flashcards[x] = new Card();
// code for setting questions & answers here
}
}
}
As you said its a bit disorganised as this all will never work in the moment I guess it will not compile too. For getting variables useable the whole time the form exists set them as private variables into your Form class
Again thank you for the reply, although I don't think I'm doing a very good job of getting my question across. I'm actually not worried about the code in my last post (it didn't do a good job of pointing out my question) - it's actually working exactly as it's supposed to and as I planned for it to. My question is more what to do *after* that code runs (see my simplified code snippet below).
I can't initialize the array as a private member of the Form class because I don't know how big the array will be at compile time - that's why it reads through the file first, to determine how big the array needs to be.
Even if I did know the size of the array at compile time (which I won't), my understanding is that I can't just create the array, but I have to create each individual object of the array as well (as opposed to C++ where you can simply declare the array and be done with it). Obviously this necessitates some sort of loop to create these objects (especially if there are potentially thousands of objects in the array). This can not be done outside of a function. Therefore, the objects must be created inside of a function. If they're created inside of a function, they will go out of scope and be destroyed as soon as the function ends.
My question is, how do I "hold onto" these objects without being perpetually involved in a game of object Hot Potato where the objects have to be continually passed around else they will be destroyed? I tried to make them static, but I always get a "The modifier 'static' is not valid for this item" error.
Below is an example of my question in the absolute simplest way I can put it - ignore the code I posted earlier (I was very tired when I threw it together :rolleyes: ) - the simple example below isn't supposed to do anything, it's just the easiest way I can think of to ask my question:
namespace TestProgram
{
public partial class Form1 : Form
{
// I can not create the flashcard objects up here,
// because I can not run
//for (int x = 0; x < ARRAY_SIZE; x++)
// flashcard[x] = new Card();
// outside of a function
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int ARRAY_SIZE = 100;
Card [] flashcard = new Card[ARRAY_SIZE];
for (int x = 0; x < ARRAY_SIZE; x++)
flashcard[x] = new Card();
// ^^^ I have now created the objects -
// my question is what do I do with the objects
// in the flashcard array so that they can in some way
// be accessed or manipulated in the button2_Click()
// function?
}
private void button2_Click(object sender, EventArgs e)
{
// how can I access the flashcard objects in here? Or, where/how
// can I create a function that I can call to access the flashcard
// objects?
}
}
public class Card
{
// this class does nothing as far as this example
// is concerned, it's only here so I have an object
// to create
}
}
I'm probably making this far more difficult than it needs to be, but I can't imagine this is actually such a complicated thing to accomplish. Or, if I'm just being completely insane and have absolutely no idea what I'm talking about, please let me know that as well :D
# 6 Re: simple question regarding windows programming
using System;
using System.Windows.Forms;
namespace TestProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//New class fields
protected const int ARRAY_SIZE = 100;
protected Card[] flashcard = new Card[ARRAY_SIZE];
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < ARRAY_SIZE; x++)
flashcard[x] = new Card();
// ^^^ I have now created the objects -
// my question is what do I do with the objects
// in the flashcard array so that they can in some way
// be accessed or manipulated in the button2_Click()
// function?
}
private void button2_Click(object sender, EventArgs e)
{
// flashcard is now a class field, all functions within this class can utilize it
}
}
public class Card
{
// this class does nothing as far as this example
// is concerned, it's only here so I have an object
// to create
}
}
You can create variables and class objects and the like within the scope of the class itself, you just cant assign to them unless they're const. However, you CAN assign to them from within a called function and then the data contained in those class fields will be available to all functions in the class.
Until data has been assigned to them however, they will contain null values :D
# 7 Re: simple question regarding windows programming
I finally found a way to get it working the way I want it. It seems way more convoluted than it needs to be, but it works :D Basically I created three tiers of classes:
a CardDealer object, which is created as a member of the form class and holds the CardDeck and Card objects (and exists for the life of the program)
a CardDeck object, which is created in order to hold the Card objects
and the Card objects, which are the flashcards themselves, which are contained in the CardDeck object, which in turn is contained in the CardDealer object
Sounds complicated, but it's the only way I could think of to get it to work :rolleyes:
# 8 Re: simple question regarding windows programming
I finally found a way to get it working the way I want it. It seems way more convoluted than it needs to be, but it works :D Basically I created three tiers of classes:
a CardDealer object, which is created as a member of the form class and holds the CardDeck and Card objects (and exists for the life of the program)
a CardDeck object, which is created in order to hold the Card objects
and the Card objects, which are the flashcards themselves, which are contained in the CardDeck object, which in turn is contained in the CardDealer object
Sounds complicated, but it's the only way I could think of to get it to work :rolleyes:Actually it sounds like the correct approach. This design is much easier to understand than a design that stores individual items in separate containers. When you can decompose your problem into areas of responsibilities and represent these areas with objects, then you're on to something. It seems you have done this.
Arjay at 2007-11-9 11:43:40 >

# 9 Re: simple question regarding windows programming
I finally found a way to get it working the way I want it. It seems way more convoluted than it needs to be, but it works :D Basically I created three tiers of classes:
a CardDealer object, which is created as a member of the form class and holds the CardDeck and Card objects (and exists for the life of the program)
a CardDeck object, which is created in order to hold the Card objects
and the Card objects, which are the flashcards themselves, which are contained in the CardDeck object, which in turn is contained in the CardDealer object
Sounds complicated, but it's the only way I could think of to get it to work :rolleyes:
It doesn't only sound complicated, it is complicated. Why nor simple using
private List<Cards> cardList = new List<Cards>();
as a member of your form class
You can create the card objects then in the method where you are reading them and adding them to the list and they will have the same lifetime as the form has. And thats all for this.
And have a look into that things like generic Lists, Dictionarys and all that things. Understanding that will make your life much easier, because you dont need to define the length of a list in before. You simple add items toit and it grows automatically.
I also have trained myself in C# mostly myself from Books. And what I have seen in my own studies was : if I wanted to go from A to B and instead of this have gone from A to Z and then To S from there to Y and there back to C, up to K and then last end to B, it seems I had been a bit confused, isn't it ?. :D
So IMHO have a look on lifetime of objects to get it fully understood, find the terms you dont fully understand and your code will shrink down to a few lines. :wave:
