The garbage code is practiced in this way ...

xiaoxiao2021-03-17  219

Many people like to be online, in the forum, QQ group, to others to code ... and I am used to, I don't like to see someone else, because the code throws in the forum, although there is also a boutique, but most It is a novice practicing, every time you see the crying code. So usually just want to run, look at the structure, hierarchy. But every time you need to see someone else's code ..... I have encountered one today. The best garbage code ... A class is close to 900 lines of code .... Thread is unsafe, even if you can use the code, less than half, and that is less than half of the code, it is almost all garbage. Take a few examples, it is also to everyone, and it is also a ring to the novice. People are all coming, they have made low-level mistakes. The key is to have a next time .1, get the last week Today. Using long code, first build SQLConnectin and SQLCommand, SqlDataAdapter ..... then request "SELECT GETDATE () statement from the database, get a DataSet, then from this Dataset Tables [0] .rows [ 0] [0] Get the Object of a code current date, in order to transfer DataTime, use datetime.parse (), and this method requires String, so TOSTRING () has .... then Take another way, go to 1 week above ......... In fact, everyone knows, this is very simple, it is enough.

DateTime.today.Adddays

-

Seduce

)

2, get the last day of this month. It is more exaggerated than above ..... full use nearly 100 lines of code .....

1

/ ** /

///

2 /// Get the month of the month does not have time as: 20051130 3 /// 4 ///

5

public

Static

DateTime getMonthend (DateTime DT)

6

{7 datetime enddate; 8 string stryear, strmonth, strarday; 9 strMonth, strarday; 9 strMonTh, stranda = (dt.month> 9)? Dt.month.tostring (): "0" DT.MONTH .Tostring (); 11 strDay = getDaysofmonth (DT) .tostring (); 12 string strdate = stringEear "-" strmonth " " "; 13 enddate = convert.todatetime (strdate); 14 return enddate; 15 16 17}

18

19

/ ** /

///

How many days in this month 20 /// Date 21 /// Number of days

twenty two

Private

Static

int

GetDaysofmonth (DateTime DT)

twenty three

{24 int year, month, days = 0; 25 year = dt.year; 26 month = dt.month; 2728 // - Using the annual monthly information to get the current month's number of days. 29 Switch (Month) 30 {31 Case 1:32 Days = 31; 33 Break; 34 Case 2:35 IF (ISRUYEAR (DT)) 36 {37 Days = 29; 38} 39 else40 {41 days = 28; 42} 4344 Break; 45 case 3:46 days = 31; 47 Break; 48 case 4:49 days = 30; 50 break; 51 case 5:52 days = 31; 53 Break; 54 case 6:55 days = 30; 56 BREAK ; 57 case 7:58 days = 31; 59 Break; 60 case 8:61 days = 31; 62 Break; 63 case 9:64 days = 30; 65 Break; 66 case 10:67 days = 31; 68 Break; 69 case 11:70 days = 30; 71 Break; 72 case 12:73 days = 31; 74 Break; 75} 7677 return; 7879} 80

81

/ ** /

///

Is it a year 82 /// Date 83 /// is a leap year: True is not a leap year : False

84PRIVATE

Static

Bool

Isruyear (DateTime IDT)

85

{86 INT N; 87 n = idt.year; 8889 IF ((N% 400 == 0) || (N% 4 == 0 && n% 100! = 0)) 90 {91 Return True; 92} 93 Else94 {95 Return False; 96} 97}

In fact, just 1 sentence:

DateTime.Parse (DT.ToString)

"

YYYY-MM-01

"

))). AddMontHS (

1

) .Adddays

-

1

);

The next day, the next day is the last day of this month.

Everything in the various method code of the whole class has, it is actually just hope to remind some newbies, write code to pay attention to thinking.

Http://syeerzy.cnblogs.com/archive/2006/03/09/346726.html

转载请注明原文地址:https://www.9cbs.com/read-129714.html

New Post(0)