Click here to Skip to main content
15,910,877 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to get a list og Outgoing MSMQ queues (without mention its knowing name) ? Pin
Luc Pattyn1-May-20 7:43
sitebuilderLuc Pattyn1-May-20 7:43 
GeneralRe: How to get a list og Outgoing MSMQ queues (without mention its knowing name) ? Pin
boozeeh11-May-20 12:20
boozeeh11-May-20 12:20 
GeneralRe: How to get a list og Outgoing MSMQ queues (without mention its knowing name) ? Pin
Luc Pattyn1-May-20 13:43
sitebuilderLuc Pattyn1-May-20 13:43 
GeneralRe: How to get a list og Outgoing MSMQ queues (without mention its knowing name) ? Pin
boozeeh12-May-20 1:31
boozeeh12-May-20 1:31 
QuestionUse DLL in C# Pin
Member 1481764029-Apr-20 15:16
Member 1481764029-Apr-20 15:16 
AnswerRe: Use DLL in C# Pin
Dave Kreskowiak29-Apr-20 16:56
mveDave Kreskowiak29-Apr-20 16:56 
AnswerRe: Use DLL in C# Pin
Luc Pattyn29-Apr-20 17:11
sitebuilderLuc Pattyn29-Apr-20 17:11 
AnswerRe: Use DLL in C# Pin
Eddy Vluggen30-Apr-20 9:34
professionalEddy Vluggen30-Apr-20 9:34 
AnswerRe: Use DLL in C# Pin
kalberts2-May-20 4:40
kalberts2-May-20 4:40 
QuestionCan not compare a file and same file stored in mysql - sha256 Pin
wilcodk29-Apr-20 7:40
wilcodk29-Apr-20 7:40 
AnswerRe: Can not compare a file and same file stored in mysql - sha256 Pin
Luc Pattyn29-Apr-20 8:07
sitebuilderLuc Pattyn29-Apr-20 8:07 
GeneralRe: Can not compare a file and same file stored in mysql - sha256 Pin
wilcodk30-Apr-20 1:32
wilcodk30-Apr-20 1:32 
AnswerRe: Can not compare a file and same file stored in mysql - sha256 Pin
Mycroft Holmes29-Apr-20 12:18
professionalMycroft Holmes29-Apr-20 12:18 
GeneralRe: Can not compare a file and same file stored in mysql - sha256 Pin
wilcodk30-Apr-20 1:28
wilcodk30-Apr-20 1:28 
AnswerRe: Can not compare a file and same file stored in mysql - sha256 Pin
Eddy Vluggen30-Apr-20 6:34
professionalEddy Vluggen30-Apr-20 6:34 
QuestionCompare string time to timer time (noobie) Pin
Member 1481029228-Apr-20 5:36
Member 1481029228-Apr-20 5:36 
AnswerRe: Compare string time to timer time (noobie) Pin
Gerry Schmitz28-Apr-20 6:48
mveGerry Schmitz28-Apr-20 6:48 
QuestionResize and move a drawn rectangle in win forms Pin
Member 1223285027-Apr-20 12:23
Member 1223285027-Apr-20 12:23 
AnswerRe: Resize and move a drawn rectangle in win forms Pin
Luc Pattyn27-Apr-20 13:17
sitebuilderLuc Pattyn27-Apr-20 13:17 
GeneralRe: Resize and move a drawn rectangle in win forms Pin
Member 1223285027-Apr-20 19:38
Member 1223285027-Apr-20 19:38 
GeneralRe: Resize and move a drawn rectangle in win forms Pin
Luc Pattyn28-Apr-20 4:50
sitebuilderLuc Pattyn28-Apr-20 4:50 
GeneralRe: Resize and move a drawn rectangle in win forms Pin
Member 1223285028-Apr-20 17:56
Member 1223285028-Apr-20 17:56 
GeneralRe: Resize and move a drawn rectangle in win forms Pin
Luc Pattyn29-Apr-20 2:30
sitebuilderLuc Pattyn29-Apr-20 2:30 
Hi,

1.
you have too many variables, this is called data redundancy. In particular you have both x,y,width,height and rect; in some parts you operate on x,y in others on rect.X and rect.Y, and that is why it does not behave as you would hope.

The rule is: avoid redundancy; there isn't anything positive about redundancy, it adds variables and code, and it just makes it harder to pinpoint logic errors.

Using a separate class helps in achieving this, or at least makes it harder to break the rule.

2.
To make matters worse, you change your data model (your "business logic") inside the Paint handler, where it says
rect=new Rectangle(x,...).
You should never modify your business model inside a Paint handler; a Paint handler should behave as an observer, and not as a participant.
Reason: you don't know when a Paint handler will be executed:
(a) calling Invalidate tells the system your business model has changed and a repaint is required, so a repaint message is queued, but not executed right away (e.g. several calls to Invalidate may result in a single repaint);
(b) a Paint handler will also run when something happened to your window, e.g. when a pop-up dialog (may be from another app or from Windows itself) temporarily had hidden (part of) your window.

The only kind of variables your Paint handler should ever modify are the ones that are needed by the Paint handler itself, so it would be OK to write if (pen==null) pen=new Pen(Color.Red); assuming pen isn't part of your business stuff.

3.
I counted the number of lines in your code and mine, excluding empty lines, lines with a single bracket, comments, and Console.WriteLine lines; my code (Form+Rect) is 43 lines, yours 50. Which tells me using a class is the right way to go: it does not add to the code, and it supports what is known as "separation of concerns": let each piece of code take care of what matters to it without being troubled with details that don't matter there. Example: my form tells the Rect to paint itself or to move itself without caring how that is done, whereas the Rect knows how to paint or move itself without knowing why it should.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum


modified 29-Apr-20 9:01am.

QuestionDate compare Pin
Carlos5827-Apr-20 10:42
Carlos5827-Apr-20 10:42 
AnswerRe: Date compare Pin
Luc Pattyn27-Apr-20 11:00
sitebuilderLuc Pattyn27-Apr-20 11:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.