|
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 ");////////
}
}
|
|
|
|
|
Here is the exact error I am receiving:
System.InvalidOperationException: Invalid attempt to read when no data is present
But sqlReader.HasRows property is True.
Jude
|
|
|
|
|
|
What exactly do you want to get as output xml? can you please give an example?
|
|
|
|
|
Hi everybody,
I was looking a java code, and I would like to translate to C#, but I don't know if this feature exits in c#. The code is this:
public class Controller implements IController{
private State state;
private Speed speed;
private ProductHandler producthandler = new ProductHandler (){
@Override
public void changeState(State newState) {
state = newState;
}
@Override
public Speed getSpeed() {
return mds;
}
@Override
public void send(Packet pk) {
outputQueue.add(pk);
}
@Override
public void setSpeed(Speed newSpeed) {
speed = newSpeed;
}
};
ProductHandler is a class defined inside other class "dinamically". ProductHandler can access Controller attributes. Is this possible in C#? I have done it with events and so... but its very dirty. Is this annonymous classes, am i right? Is there something equivalent in .net 2.0?
Regards
modified on Friday, January 8, 2010 2:50 PM
|
|
|
|
|
C# has anonymous methods, but not anonymous classes. Maybe because you can use delegates, so there is no need for a whole class.. (according to the designers)
Anonymous methods can access the variables of the place they were declared in as well
|
|
|
|
|
I don't know Java, so I'm guessing a bit!
If you want to declare a new class nested inside another, you can and the nested class can access all members of the outer class.
If you want to extend an existing class, you can derive from it. Another option is to use extension methods, although they can only access the public members of the original class.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Thanks to both.
Nick, I have tried to do as you say, but I could't. Could you please paste a little example here?
Thanks in advance
|
|
|
|