- 1
- 2
- 3
- 4
- 5
mainDays = 0;
for (var d = emplDoc.EventDate.AddMonths(1).AddDays(-1).Date; d <= emplDoc.DateEndWork.Date; d = d.AddMonths(1)) {
mainDays += 2;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+116
mainDays = 0;
for (var d = emplDoc.EventDate.AddMonths(1).AddDays(-1).Date; d <= emplDoc.DateEndWork.Date; d = d.AddMonths(1)) {
mainDays += 2;
}
+116
var
s1:string;
i:integer;
const
m=50;
begin
write ('Введите строку: ');
for i:=1 to m do
begin
write ('s1[',i,']');
readln (s1[i]);
end;
writeln ('Длина строки: ', length(s1));
end.
Это Pascal. Вот так студенты физмата вводят строку, а потом считают ее длину.
+116
public struct RowForReportBookRecordIssue
{
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
public string Column5 { get; set; }
public string Column6 { get; set; }
public string Column7 { get; set; }
public string Column8 { get; set; }
public string Column9 { get; set; }
public string Column10 { get; set; }
}
Использование соответствующее:
RowForReportBookRecordIssue.Column1 = ...; RowForReportBookRecordIssue.Column2 = ...;
Даже страшно, если что-то придется править... :(
+116
// Установка обработкчиков событий
tbTitle.TextChanged += (x, y) => _target.Title = tbTitle.Text;
cbContractorType.SelectedIndexChanged += (x, y) => _target.ContractorType = cbContractorType.SelectedIndex;
tbFullTitle.TextChanged += (x, y) => _target.Title = tbFullTitle.Text;
tbINN.TextChanged += (x, y) => _target.INN = tbINN.Text;
// Для физ. лица
dtpBirthDate.ValueChanged += (x, y) => _target.birthDate = dtpBirthDate.Value;
tbPassportSeria.TextChanged += (x, y) => _target.PassportSeria = tbPassportSeria.Text;
tbPassportNumber.TextChanged += (x, y) => _target.PassportNumber = tbPassportNumber.Text;
dtpPassportDateRegistred.ValueChanged += (x, y) => _target.PassportDateRegistred = dtpPassportDateRegistred.Value;
tbPassportOrganizationRegistred.TextChanged += (x, y) => _target.PassportOrganizationRegistred = tbPassportOrganizationRegistred.Text;
tbRegistredPassportAddress.TextChanged += (x, y) => _target.RegistredPassportAddress = tbRegistredPassportAddress.Text;
// Для юр лица
tbOgranizationJuridicalAddress.TextChanged += (x ,y) => _target.OgranizationJuridicalAddress = tbRegistredPassportAddress.Text;
tbOrganizationMainBankAcouuntNumber.TextChanged += (x, y) => _target.OrganizationMainBankAcouuntNumber = tbOrganizationMainBankAcouuntNumber.Text;
tbOrganizationKPP.TextChanged += (x, y) => _target.OrganizationKPP = tbOrganizationKPP.Text;
tbOrganizationOGRNIP.TextChanged += (x, y) => _target.OrganizationOGRNIP = tbOrganizationOGRNIP.Text;
tbOrganizationIFNS.TextChanged += (x, y) => _target.OrganizationIFNS = tbOrganizationIFNS.Text;
tbOrganizationOKATO.TextChanged += (x, y) => _target.OrganizationOKATO = tbOrganizationOKATO.Text;
tbOrganizationOKPO.TextChanged += (x, y) => _target.OrganizationOKPO = tbOrganizationOKPO.Text;
У меня денюха поэтому решил отмаппить форму по новому
+116
/// <summary>
/// Сохраняет элементы справочника
/// </summary>
public IEnumerable SaveBookElements(IList elements) {
IList result = null;
try {
if (elements != null && elements.Count > 0) {
if (elements.GetType().GetGenericArguments().Length == 1 && elements[0].GetType() != elements.GetType().GetGenericArguments()[0]) {
var mi = typeof(BooksWorker).GetMethod("SaveTypedBookElements", BindingFlags.NonPublic | BindingFlags.Instance)
.MakeGenericMethod(new[] { elements[0].GetType() });
var casted = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(new[] { elements[0].GetType() }));
foreach (var t in elements) {
casted.Add(t);
}
result = mi.Invoke(this, new object[] { casted }) as IList;
}
}
}
catch (Exception ex) {
throw new ApplicationException("Ошибка!", ex);
}
return result;
}
Автор проникся рефлексией :-!
+116
// Method that returns anonymous type as object
object ReturnAnonymous() {
return new { City="Prague", Name="Tomas" };
}
void Main() {
// Get instance of anonymous type with 'City' and 'Name' properties
object o = ReturnAnonymous();
// This call to 'Cast' method converts first parameter (object) to the
// same type as the type of second parameter - which is in this case
// anonymous type with 'City' and 'Name' properties
var typed = Cast(o, new { City="", Name="" });
Console.WriteLine("{0}, {1}", typed.City, typed.Name)
}
// Cast method - thanks to type inference when calling methods it
// is possible to cast object to type without knowing the type name
T Cast<T>(object obj, T type) {
return (T)obj;
}
via http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/c1c179bb-ea88-4633-970a-947f0dd1e71f/
+116
if (rbNewPackage.Checked)
{
var ls = from ent in Program.DB.UserInfos
where
(from rp in Program.DB.ReceivedPackages
join r in Program.DB.RecordBooks on rp.id equals r.id_package
where rp.id_user == ent.id &&
r.date == (from recb in Program.DB.RecordBooks
join recp in Program.DB.ReceivedPackages on recb.id_package equals recp.id
where recp.id_user == ent.id
select recb.date).Max() && r.id_status == 1
select rp).Count() != 0
select ent;
grdEntrantList.DataSource = Program.DB.UserInfos.Where(t => ls.ToList().Contains(t));
}
else if (rbCancelled.Checked)
{
var ls = from ent in Program.DB.UserInfos
where
(from rp in Program.DB.ReceivedPackages
join r in Program.DB.RecordBooks on rp.id equals r.id_package
where rp.id_user == ent.id &&
r.date == (from recb in Program.DB.RecordBooks
join recp in Program.DB.ReceivedPackages on recb.id_package equals recp.id
where recp.id_user == ent.id
select recb.date).Max() && r.id_status == 5
select rp).Count() != 0
select ent;
grdEntrantList.DataSource = Program.DB.UserInfos.Where(t => ls.ToList().Contains(t));
}
else if (rbConditional.Checked)
{
var ls = from ent in Program.DB.UserInfos
where
(from rp in Program.DB.ReceivedPackages
join r in Program.DB.RecordBooks on rp.id equals r.id_package
where rp.id_user == ent.id &&
r.date == (from recb in Program.DB.RecordBooks
join recp in Program.DB.ReceivedPackages on recb.id_package equals recp.id
where recp.id == rp.id
select recb.date).Max() && r.id_status == 3
select rp).Count() != 0
select ent;
grdEntrantList.DataSource = Program.DB.UserInfos.Where(t => ls.ToList().Contains(t));
}
else if (rbOfficial.Checked)
{
var ls = from ent in Program.DB.UserInfos
where
(from rp in Program.DB.ReceivedPackages
join r in Program.DB.RecordBooks on rp.id equals r.id_package
where rp.id_user == ent.id &&
r.date == (from recb in Program.DB.RecordBooks
join recp in Program.DB.ReceivedPackages on recb.id_package equals recp.id
where recp.id == rp.id
select recb.date).Max() && r.id_status == 4
select rp).Count() != 0
select ent;
grdEntrantList.DataSource = Program.DB.UserInfos.Where(t => ls.ToList().Contains(t));
}
говногод моего бывшего коллеги по работе))
+116
public static class ControlManager
{
public static UIElement GetControlByName(UIElement control, string name)
{
if (EntityType.GetValue(control, "Name").Equals(name))
return control;
if (EntityType.IsProperty(control, "Children"))
{
foreach (var element in (UIElementCollection)EntityType.GetValue(control, "Children"))
{
if (EntityType.GetValue(element, "Name").Equals(name))
return element;
var temp = GetControlByName(element, name);
if (temp != null)
return temp;
}
}
if (EntityType.IsProperty(control, "Child"))
{
var element = (UIElement)EntityType.GetValue(control, "Child");
if (EntityType.GetValue(element, "Name").Equals(name))
return element;
var temp = GetControlByName(element, name);
if (temp != null)
return temp;
}
if (EntityType.IsProperty(control, "SelectionElement"))
{
var element = (UIElement)EntityType.GetValue(control, "SelectionElement");
if (EntityType.GetValue(element, "Name").Equals(name))
return element;
var temp = GetControlByName(element, name);
if (temp != null)
return temp;
}
return null;
}
public static UIElement GetControlByType(UIElement control, Type type)
{
if (control.GetType() == type)
return control;
if (EntityType.IsProperty(control, "Children"))
{
foreach (var element in (UIElementCollection)EntityType.GetValue(control, "Children"))
{
if (element.GetType() == type)
return element;
var temp = GetControlByType(element, type);
if (temp != null)
return temp;
}
}
if (EntityType.IsProperty(control, "Child"))
{
var element = (UIElement)EntityType.GetValue(control, "Child");
if (element.GetType() == type)
return element;
var temp = GetControlByType(element, type);
if (temp != null)
return temp;
}
if (EntityType.IsProperty(control, "SelectionElement"))
{
var element = (UIElement)EntityType.GetValue(control, "SelectionElement");
if (element.GetType() == type)
return element;
var temp = GetControlByType(element, type);
if (temp != null)
return temp;
}
return null;
}
}
Автор тот же.
Этот код пошатнул мою психику. А может так и надо. а?
всё-таки stringly-static programming....
+116
namespace Containers
{
public class TBinaryWaitingQueue<TItem> where TItem : struct
{
private readonly TBinaryQueue<TItem> _queue;
TBinaryWaitingQueue(int amountOfitem)
{
_queue = new TBinaryQueue<TItem>(amountOfitem);
}
public void Enqueue(TItem[] items)
{
throw new NotImplementedException();
}
public void Enqueue(TItem[] items, int beginItem, int amountOfItem)
{
throw new NotImplementedException();
}
public void Dequeue(TItem[] items, int beginItem, int amountOfItem)
{
throw new NotImplementedException();
}
public TItem[] Dequeue(int amountOfItem)
{
throw new NotImplementedException();
}
}
}
Досталось в наследство от предков. Этому коду уже года 4.
+116
public class TObjects
{
public delegate T DeferredConstruction<out T>();
public static void Dispose<T>(ref T objectForDispose)
{
var typeOfObjectForDispose = typeof(T);
if (!typeOfObjectForDispose.IsClass)
DisposeIfIDisposable(ref objectForDispose, typeOfObjectForDispose);
else
if (!Equals(objectForDispose, null))
DisposeIfIDisposable(ref objectForDispose, typeOfObjectForDispose);
objectForDispose = default(T);
//objectForDispose = (T)(object)(null);
}
public static void Create<T>(ref T objectForCreate, DeferredConstruction<T> newObject)
{
Dispose(ref objectForCreate);
objectForCreate = newObject();
}
private static void DisposeIfIDisposable<T>(ref T objectForDispose, Type typeOfObjectForDispose)
{
bool canDisposable = (objectForDispose as IDisposable) != null;
if (canDisposable)
{
var dispose = typeOfObjectForDispose.GetMethod("Dispose");
dispose.Invoke(objectForDispose, new object[] { });
}
}
}