|
I am not sure what you mean by "atomicity" given your example of what you'd change the code to (putting them on the same like doesn't affect the runtime behavior at all). If you want the operation to by atomic in a thread safety sense, you'd want to use locking, like this:
lock(lockingVariable)
{
position--;
bit >= 1;
}
You will also have to ensure anything else that has access to those variables does a similar lock. In this way, you can ensure that one thread isn't accessing the variables when only one of them has been changed.
gggustafson wrote: I am fiercely against this in a statement body
Why?
gggustafson wrote: But in this case I think it signals atomicity. Is there a better way to do this?
If you are talking about indicating to other programmers who read the code that the operation is intended to be atomic, could you just put in a comment, such as this?:
position--;
bit >>= 1;
If you really want to be explicit, you could do this:
{
position--;
bit >>= 1;
}
Sorry if that seems like a silly suggestion, but I really am not sure I understand your question.
|
|
|
|
|
I appreciate your thoughts.
I am fully aware that the text of a program does not affect its runtime behavior. lock is useful in concurrent processes to insure that critical regions are not simultaneously executed by multiple processes.
The reason that I am fiercely opposed to multiple statements on a line is for the same reason that I expect that edge detection be enforced. In an earlier article, Minimalist Coding Guidelines, I give an example of why:
Block Declaration
1 [ DllImport ( "gdi32.dll", EntryPoint = "BitBlt" ) ]
public static extern bool BitBlt ( IntPtr hdcDest, int nXDest,
int nYDest, int nWidth, int nHeight,
IntPtr hdcSrc, int nXSrc, int nYSrc,
int dwRop );
2 [ DllImport ( "gdi32.dll",
EntryPoint = "BitBlt" ) ]
public static extern bool BitBlt ( IntPtr hdcDest,
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc,
int nXSrc,
int nYSrc,
int dwRop );
I don't think that there are those who would claim that the style of Block 1 is superior to that of Block 2.
I'm sorry that I do not agree with your commenting style. A comment that relates to a statement should be placed to the right of the statement, not above it. This coding style makes the comment "part of the statement." Thus if the statement is removed or moved, the comment is removed or moved with it.
I do like the idea of creating a body surrounding the statements.
Thanks for your comments.
Gus Gustafson
|
|
|
|
|
You could write a class (or struct) to hold the two values and have a Decrement method that does both operations.
Or maybe something like bit = ( 1 << --position )
|
|
|
|
|
I like your
bit = ( 1 << --position );
I'll use that form, adding a comment as follows
bit = ( 1 << --position );
I fully realize that the statements are not atomic, but I do think that a reader might be guided during maintenance.
Thanks for the idea.
Gus Gustafson
|
|
|
|
|
You stated elsewhere that the expressions are in a private static method.
I still don't see the need for atomicity here.
Maybe if you explained why you require atomicity, we could understand.
On the other hand, we could just trust that you have a reason.
You have no obligation to justify this to us.
But, maybe, we might learn something new...
|
|
|
|
|
Unfortunately, there's no inherent concept of atomicity in C#, and no guarantee that you could handle it reliably. The classic example would be to provide some form of undo functionality which you could record your changes in (somehow), and then call this if your code were to fail somehow. While this, initially, might seem to be a viable idea, it's not quite as clear cut as this would indicate. For instance, how would you capture operations going on internally? What happens if those operations introduce side effects?
|
|
|
|
|
I agree. It's too bad. Obviously we are dealing with forcing a sequence of machine instructions to be executed without interrupt. In my days of assembly programming, I was able to suspend interrupts and then resume them by masking by a global interrupt mask bit.
It's too bad that C# doesn't support this. For example, a form like
atomic
{
:
statements
:
}
would have sufficed.
Thanks for your thoughts.
Gus Gustafson
|
|
|
|
|
A static method should at least be thread-safe.
|
|
|
|
|
The question is not thread safety; it's atomicity. The concepts are different.
Thanks for your comment.
Gus Gustafson
|
|
|
|
|
Hi,
Can anyone advise me on how to tell if a Forms icon is set to the default Visual Studio Icon or my own icon.
The reason I ask is that on opening it up into a tabbed document view, I only want to put the form's icon on the generated page tab is it's my own icon, not the VS one. I have everything working except the check to see if it's the default VS one.
Any help most gratefully received.
Thanks.
|
|
|
|
|
..correct me if I'm wrong, but you'll need to access the same property to "check" it as you'll need to "set" it. Wouldn't it be more logical to simply set it?
How would you call/invoke the checking method? Where, what event, and how would the signature look?
There's another problem; it's hard for the computer to compare pictures. As far as it's concerned, they're just a collection of bytes. You could generate a hash-value for the default VS-icon, and compare that to the hash-value generated from the current icon. If the hashes differ, then the icons differ.
Still, it'd be easier to have a single base-form, set the icon in there once, and inherit from there.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy, thanks for the response.
event as follows...
private void tabbedView1_DocumentAdded(object sender,DevExpress.XtraBars.Docking2010.Views.DocumentEventArgs e)
{
DevExpress.XtraBars.Docking2010.Views.BaseDocument bd = (DevExpress.XtraBars.Docking2010.Views.BaseDocument)e.Document;
if (bd.Form.ShowIcon && bd.Form.Icon.Size == new Size(16, 16)) // Default VS icon is 32x32 //
{
e.Document.Image = bd.Form.Icon.ToBitmap();
}
}
I'm checking the size at the moment to get round the issue as the VS one is 32x32 and any icon I might set on one of my forms is currently 16x16. I can't really go through all my forms too easily to check if I forgot to turn off the VS icon as I have over 400 forms in my project (it's pretty large)...
I only have a handful of forms at the moment where I might set my own icon to make them stand out. It would just be a bit cleaner if I was able to do something like bd.Form.Icon != default.icon etc...
|
|
|
|
|
MarkB123 wrote: It would just be a bit cleaner if I was able to do something like bd.Form.Icon != default.icon etc...
Start here[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks Eddy,
I'll give this a go
|
|
|
|
|
You're welcome
|
|
|
|
|
Hello, please I need help.
How can I fill a datagridview with two datasource?
Yours
|
|
|
|
|
You need to expand your question with more information so we can help you more.
As far as I am aware you can only have one datasource.
Is the information that you want to display in the same format?
Does the data need to be separated?
if the answers are yes to 1. and no to 2. then why not merge the 2 datasources together and then display it?
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
The DGV can only display a single datasource. You'll have to combine the datasets as previously suggested.
Are both coming from the same database? If yes, can you post the SQL-statements that you're trying to load in the grid? Perhaps we can come up with a nice and easy alternative
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
ok, the both comes from the same datagridview.
I want to fill my datagridview from a table "tax" and table "products" of the same database.
Please how can i combine the datasets?
|
|
|
|
|
geralson wrote: Please how can i combine the datasets? You'd combine them on the server; instead of fetching data FROM [tax] and FROM [products], you JOIN them.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
If you're determined not to use the server to do this, you'd have to create a new DataTable that contained the matching results from both sets, or use the techniques outlined here[^].
|
|
|
|
|
converting database schema in form of schema graph having primary and foreign key relationship and storing in cache using datastructure........?????????
|
|
|
|
|
Just throwing question marks at a sentence doesn't make this a question. What are you asking, and what have you written so far?
|
|
|
|
|
Visual Studio has tools where a table can be viewed as a schema diagram - if that is what you are looking for.
|
|
|
|
|
hey guys i'm making a ladder and snake game using C# and i'm stuck at the point where i need to move my players forward .. which means that the players move correctly only in round 1 but after that they start going backwards .
for example for player one the first turn the dice showed number 4 so it moved to square 4 , in second round the dice showed number 2 the player goes back to number two and not to number 6 as it's suppose to .. so can you help me with that i would really appreciate it
this is my code so far :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ladder__snake
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//variables to hold the players locations
int blueposition = 1;
int redposition = 1;
int round = 0;
bool drawbg = false;
Point[] squares = new Point[26];
public void main()
{
//array for the squares
squares[0] = new Point(50, 450);
squares[1] = new Point(150, 450);
squares[2] = new Point(250, 450);
squares[3] = new Point(350, 450);
squares[4] = new Point(450, 450);
squares[5] = new Point(450, 350);
squares[6] = new Point(350, 350);
squares[7] = new Point(250, 350);
squares[8] = new Point(150, 350);
squares[9] = new Point(50, 350);
squares[10] = new Point(50, 250);
squares[11] = new Point(150, 250);
squares[12] = new Point(350, 250);
squares[13] = new Point(350, 250);
squares[14] = new Point(450, 250);
squares[15] = new Point(450, 150);
squares[16] = new Point(350, 150);
squares[17] = new Point(250, 150);
squares[18] = new Point(150, 150);
squares[19] = new Point(50, 150);
squares[20] = new Point(50, 50);
squares[21] = new Point(150, 50);
squares[22] = new Point(250, 50);
squares[23] = new Point(350, 50);
squares[24] = new Point(450, 50);
}
private void button1_Click(object sender, EventArgs e)
{
round++;
label3.Text = round.ToString();
//for the blue player dice
Random rnd;
int guess;
rnd = new Random();
guess = rnd.Next(1, 7);
pictureBox1.Image = Image.FromFile(guess.ToString() + ".png");
// Get the Blue player moving
switch (guess)
{
case 1:
bluePlayer.Location = new Point(50, 450);
break;
case 2:
bluePlayer.Location = new Point(150, 450);
break;
case 3:
bluePlayer.Location = new Point(250, 450);
break;
case 4:
bluePlayer.Location = new Point(250, 350);
MessageBox.Show("It's a ladder move to square 8");
break;
case 5:
bluePlayer.Location = new Point(450, 450);
break;
case 6:
bluePlayer.Location = new Point(450, 350);
break;
}
//bluePlayer.Location = squares[blueposition+guess];
//BluePlayer += guess;
//currentPosition1 += guess;
// bluePlayer.Location = squares[BluePlayer+guess];
//guess = 0;
label4.Visible = false;
label5.Visible = true;
label5.Text = "Red Player's turn";
button2.Enabled = true;
button1.Enabled = false;
pictureBox2.Image = Image.FromFile("dice.png");
//blueposition += guess;
//bluePlayer.Location = squares[guess];
}
private void button2_Click(object sender, EventArgs e)
{
//for the red player dice
Random rnd;
int guess;
rnd = new Random();
guess = rnd.Next(1, 7);
pictureBox2.Image = Image.FromFile(guess.ToString() + ".png");
//redposition += guess;
switch (guess)
{
case 1:
redPlayer.Location = new Point(50, 445);
break;
case 2:
redPlayer.Location = new Point(150, 445);
break;
case 3:
redPlayer.Location = new Point(250, 445);
break;
case 4:
redPlayer.Location = new Point(250, 350);
MessageBox.Show("It's a ladder move to square 8");
break;
case 5:
redPlayer.Location = new Point(450, 445);
break;
case 6:
redPlayer.Location = new Point(450, 345);
break;
}
label5.Visible = false;
label4.Visible = true;
label4.Text = "Blue Player's turn";
button2.Enabled = false;
button1.Enabled = true;
pictureBox1.Image = Image.FromFile("dice.png");
}
private void groupBox2_Enter(object sender, EventArgs e)
{
if (!drawbg)
{ groupBox2.Visible = true; }
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Load_1(object sender, EventArgs e)
{
pictureBox1.Image = pictureBox2.Image = Image.FromFile("dice.png");
}
}
}
|
|
|
|
|