|
Hi,
you really should spend some time and come up with realistic class names, you may not believe it right away, but it will help you in making fewer mistakes.
Without seeing any of the code, my best guess is you somewhere have an extraneous new Class2() and are suddenly facing the rollcount1 of a new class2 instance, which obviously is not holding the same value rollcount1 has in an earlier class2 instance.
|
|
|
|
|
Hey Luc Pattyn,
you had a good guess and based on your answer I was able to fix the problem, but it's slightly more complex than simply turning the ints into static ints.
And, Yeah I usually set the name of my classes to something appropriate, but this is a friends' project and he needed help on it, and I couldn't help so I decided to come to the professionals
Thanks again!
|
|
|
|
|
Make rollcount1 static.
There are only 10 types of people in this world — those who understand binary, and those who don't. |
|
|
|
|
|
Wow that worked . Now, if you don't mind, could you please go into detail on what exactly static is or what it does? Pleassse
|
|
|
|
|
Some more info on static here [^]. Warning - static is not always the best way to do things. I would suggest you also look at restructuring your code as has been mentioned in the other post.
Also, vote for an answer if it helps .
There are only 10 types of people in this world — those who understand binary, and those who don't. |
|
|
|
|
|
Hi,
static members of a class get shared by all instances of that class, so they each get allocated memory only once, and all instances read and write that single instance. A typical example would be a counter to keep track how many instances of some class have been created, or are in existence; such count, by its very nature, does not belong to any such instance, it oversees all instances. That is what static is for.
"solving" a problem by adding static to some variables is very bad practice in general, it does not conform to best practices in object-oriented programming.
In your case, again assuming you had two instances of class2 where you really only wanted one, it is definitely the wrong way to solve the problem. You should:
- not create a second class2 instance;
- instead pass the first instance to interested parties, or better yet pass delegates to execute the operation you want to happen inside class2.
It is my impression you are not really qualified to help your friend; I suggest you buy and study an introductory book on C# before passing advice to others...
|
|
|
|
|
Thanks for the help :P
Thing is, we're both in grade 11, and our teachers don't exactly, you know, teach.
I have to figure everything out for myself pretty much, and solving it by adding static is a good solution for now in my opinion.
He had his whole classroom attempt to solve his problem, but nobody succeeded, so he's pretty desperate.
Plus, it's a simple snakes and ladders game that he's attempting, not anything professional.
But once again, thanks for your help, I learned something new today ^.^
|
|
|
|
|
Raztor0 wrote: solving it by adding static is a good solution
That depends on the purpose of rollcount .
If it's counting the number of ALL rolls in the game, then static is fine as it's not directly related to an instance of the class. If the class had proper names that would belong to a Dice class. It would be better to have a public static (readonly) property with a private static field. Even in this situation, it may be better to have a Game class and the value would be an instance member of that.
If it's counting the number of throws per player then it would need to be an instance member - of a Player class, this class should raise an event to tell the main form (or any other interested party) that the value has changed.
It's really quite easy to do and worth doing properly. Once you've done it the correct way once, you'll never take the nasty 'static' approach again unless it's the correct solution.
If you want further help you'll need to post some relavent code snippets as so far we are just guessing!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
I could post some code, but do you think I should post ALL the code?
It's 690 lines spread out into Form1, and two other classes, but they're really just a ton of if statements and methods through which I return variables.
Keep in mind this isn't my code, it's a friends' code who needed help with this problem so I don't really fully understand how it's working either.
So, should I post it all? I think it might be necessary for you to see it all
modified on Saturday, January 9, 2010 10:26 AM
|
|
|
|
|
No - there's no need to post all that!
Your friend may be better off working out the plan on paper first and doing some recoding afterwards. This often gives a clearer indication of what classes, properties, methods and events are needed without writing a line of code.
Coding is then just a matter of implenting the above which should then be trivial. Starting by coding often causes problems like he has now as the classes and communication between them are not clearly defined.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Alright, thanks to everyone who helped I've learned something that will help me in the future
|
|
|
|
|
will u please explain the code ?
|
|
|
|
|
Hi,
I am a begginer in C# and opencv . i need your help in my project. i have the saved avi file( sequence of frames ) using videowriter class .Now , i want to open saved avi file . I dont have any clue about this .Can you please tell me how to open avi file using opencv and c#.
Thanking you,
sarala
|
|
|
|
|
I am asuming you mean play an AVI video? if so, this article[^] may be a good starting point
|
|
|
|
|
Does this[^] article help?
/ravi
|
|
|
|
|
you just import the mediaplayer in to ypur project and play the file
|
|
|
|
|
I received an error ( Invalid attempt to read when no data is present ) while reading data with a SqlDataReader. I went to the data and found the last record, then I debugged from that record.
Code:
sqlReader.Read(); //this is after the last row has already been read, should throw exception?
if (!sqlReader.HasRows)//sqlReader.HasRows = true...??
After this it loops back to a while and tries to read data, in which I get the error.
I would appreciate if someone could explain to me the error of my ways
Jude
|
|
|
|
|
Do you have the following structure ?
while(sqlReader.Read())
{
}
|
|
|
|
|
HasRows doesn't tell you how many rows you haven't "Read() ", it only tells you if there are rows in the reader. Read advances you to the next row, starting from one before the first; the first time you call it you're placed at the first row.
To loop through all of the rows use this:
while (sqlReader.Read())
{
}
For Reference: MSDN[^]
|
|
|
|
|
I have the following
while (sqlReader.Read())
{
while(sqlReader["SOME FIELD"] == variable)
{
sqlReader.Read()
if (!sqlReader.HasRows)
{
blEnd = true;
break
}
if (blEnd){break}
}
}
Jude
|
|
|
|
|
What if the data is NULL in the database? Before you check sqlReader["SOME FIELD"] against your target value you should check it against DBNull.Value .
Also, this:
sqlReader.Read()
if (!sqlReader.HasRows)
has the same problem as before. HasRows doesn't care if you called Read or how many times it's called. It always returns the same thing. You shouldn't have more than one call to Read and you should always check it's return value.
What it looks like you're trying to do if go through all of the rows in the reader and do some work when the value of sqlReader["SOME FIELD"] equals variable . If you found variable then exit the loop.
If that's what you're doing then try this:
bool found = false;
while (sqlReader.Read() && !found)
{
if (sqlReader["SOME FIELD"] == DBNull.Value)
{
continue;
}
string dbVal = sqlReader["SOME FIELD"] as string;
if (dbVal != variable)
{
continue;
}
found = true;
}
Even if it's not what you're doing, note that there's only one Read , it's return value is checked and we're checking for DBNull.Value . dbVal should be the same type as whatever variable is, I just picked string as an example. Also note that <code> HasRows is not needed.
|
|
|
|
|
Thank you much for your reply.
What I am confused about in your example is the comment to 'continue to the next row'. How do I advance the cursor without calling Read()?
Jude
|
|
|
|
|
continue[^]
The continue; statement returns the code to the while() line which calls Read() , and that's how it advances to the next row
|
|
|
|
|
In fact you should use it in the other way:
DataTableReader dataTableReader = myDatabase.CreateDataReader();
if (dataTableReader.HasRows)
{
while (dataTableReader.Read())
{
Console.Out.Write(...);
}
}
Read will returns a boolean and does not through an exception.
Moreover it also should depend on your provider (MySQL, SQL Server...), the cursor method used behind may conduct to the problem.
You should read that you can use Read before HasRows but in the past I got some problems with some providers.
Hope it helps
|
|
|
|
|
DataTableReader dataTableReader = myDatabase.CreateDataReader();
if (dataTableReader.HasRows)
{
while (dataTableReader.Read())
{
//// write here... eg : messagebox.show("oUT pUT ");////////
}
}
|
|
|
|