- 1
- 2
- 3
- 4
- 5
- 6
- 7
public static class StringExtensions
{
public static bool IsNulldOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+8
public static class StringExtensions
{
public static bool IsNulldOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
}
why
+1
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Excel;
using FPCLib.Models.Broadcasts;
namespace ExcelReadTests.Model.Путевка
{
public class MyExcel : IDisposable
{
private readonly DataTable table;
public MyExcel(string putevkaFileName)
{
table = ReadToTable(putevkaFileName);
}
internal DataTable ReadToTable(string excelFileName)
{
var stream = File.Open(excelFileName, FileMode.Open, FileAccess.Read);
var excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
return excelReader.AsDataSet().Tables[0];
}
public bool IsTvc()
{
if (table.Columns.Count == 10)
return true;
return false;
}
public bool IsFriday()
{
if (table.Columns.Count == 16)
return true;
return false;
}
public List<Broadcast> GetBroadcastsFriday()
{
var broadcasts = new List<Broadcast>();
for (var i = 0; i < table.Rows.Count; i++)
{
if (table.Rows[i][0].ToString().Length > 1)
{
broadcasts.Add(new Broadcast
{
Time = new Time(table.Rows[i][0].ToString()),
Title = table.Rows[i][1].ToString(),
File = table.Rows[i][3].ToString(),
Type = table.Rows[i][7].ToString(),
Id = table.Rows[i][2].ToString(),
Length = new Time(table.Rows[i][4].ToString())
});
}
}
return broadcasts;
}
public List<Broadcast> GetBroadcastsTvc()
{
var broadcasts = new List<Broadcast>();
for (var i = 0; i < table.Rows.Count; i++)
{
broadcasts.Add(new Broadcast
{
Time = new Time(table.Rows[i][0].ToString()),
Title = table.Rows[i][2].ToString().Trim(),
File = table.Rows[i][6].ToString().Trim(),
Type = table.Rows[i][9].ToString().Trim(),
Id = table.Rows[i][1].ToString().Trim(),
Length = new Time(table.Rows[i][5].ToString())
});
}
return broadcasts;
}
public void Dispose()
{
}
}
}
вот так открываю эксельку
DataTable ReadToTable(string excelFileName)
по количеству столбцов, определяю ее источник)
public bool IsTvc()
public bool IsFriday()
и даже поддерживаю интерфейс IDisposable))
+2
public static string _GetValueFromConfigFile(string sKey)
{
string sReturnValue = _scNullString;
string filePath = System.IO.Directory.GetCurrentDirectory() + @"\App.config";
// FOR TDOCS
//string filePath = @"D:\hosting\4856094\html\Bin\App.config";
XmlDocument doc = new XmlDocument();
XmlNode rootNode;
XmlNode xmlNode;
try
{
doc.Load(filePath);
rootNode = doc.DocumentElement;
xmlNode = rootNode.SelectSingleNode("descendant::add[@key='" + sKey + "']");
if (xmlNode != null) sReturnValue = xmlNode.Attributes["value"].Value;
return sReturnValue;
//return "";
}
catch// (Exception Ex)
{
//_ErrorDetail = Ex.Message;
return _scNullString;
}
}
Very helpful method to get data from app.config :)
Жаль что для web не работает :(
Note:
public const string _scNullString = "";
+2
if(itemsCount <= 2)
{
if(iteration == 1)
{
dragItemPos = new Vector3(-20, -55, 0);
}else if(iteration == 2){
dragItemPos = new Vector3(145, 75, 0);
}
}else if(itemsCount == 3){
if(iteration == 1)
{
dragItemPos = new Vector3(60, -170, 0);
}else if(iteration == 2){
dragItemPos = new Vector3(245, -45, 0);
}else if(iteration == 3){
dragItemPos = new Vector3(20, 90, 0);
}
}else if(itemsCount == 4){
if(iteration == 1)
{
dragItemPos = new Vector3(60, -170, 0);
}else if(iteration == 2){
dragItemPos = new Vector3(245, -45, 0);
}else if(iteration == 3){
dragItemPos = new Vector3(-80, 2, 0);
}else if(iteration == 4){
dragItemPos = new Vector3(140, 160, 0);
}
}
else{
if(iteration == 1)
{
dragItemPos = new Vector3(60, -170, 0);
}else if(iteration == 2){
dragItemPos = new Vector3(245, -45, 0);
}else if(iteration == 3){
dragItemPos = new Vector3(-100, -45, 0);
}else if(iteration == 4){
dragItemPos = new Vector3(25, 95, 0);
}else if(iteration == 5){
dragItemPos = new Vector3(190, 180, 0);
}
}
Определяем позиции объекта по их количеству и по номеру итерации. Массивы? не не слышал.
+7
public static bool CheckBoxValue(bool Checked)
{
return Conversions.ToBoolean(Interaction.IIf(Checked, true, false));
}
+1
string str3 = Strings.Trim(ID);
do
{
num2 = (short) Strings.InStr(str3, " ", CompareMethod.Binary);
if (num2 > 0)
{
str3 = str3.Substring(0, num2 - 1) + Strings.Mid(str3, num2 + 1);
}
}
while (num2 > 0);
А зачем нам str3.Replace(" ", string.Empty) ?
+2
bool isLiveLine = false;
bool isQALine = false;
if (lineInfo.IndexOf("QL") != -1)
{
isLiveLine = true;
isQALine = true;
}
else if (lineInfo.IndexOf("Q") != -1)
{
isLiveLine = false;
isQALine = true;
}
else if (lineInfo.IndexOf("L") != -1)
{
isLiveLine = true;
isQALine = false;
}
else
{
isLiveLine = false;
isQALine = false;
}
+2
public static byte[] HMACSHA256(ProtectedData key, byte[] data)
{
using (var _key = key.Get())
using (var hmac = new HMACSHA256(_key))
return hmac.ComputeHash(data);
}
public static byte[] HMACSHA256(ProtectedData key, Stream stream)
{
using (var _key = key.Get())
using (var hmac = new HMACSHA256(_key))
return hmac.ComputeHash(stream);
}
public static byte[] HMACSHA256(byte[] key, byte[] data)
{
using (var hmac = new HMACSHA256(key))
return hmac.ComputeHash(data);
}
public static byte[] HMACSHA256(byte[] key, Stream stream)
{
using (var hmac = new HMACSHA256(key))
return hmac.ComputeHash(stream);
}
public static byte[] MD5(byte[] data)
{
using (var h = System.Security.Cryptography.MD5.Create())
{ return h.ComputeHash(data); }
}
public static byte[] MD5(Stream stream)
{
using (var h = System.Security.Cryptography.MD5.Create())
{ return h.ComputeHash(stream); }
}
public static byte[] SHA1(byte[] data)
{
using (var h = System.Security.Cryptography.SHA1.Create())
return h.ComputeHash(data);
}
public static byte[] SHA1(Stream stream)
{
using (var h = System.Security.Cryptography.SHA1.Create())
return h.ComputeHash(stream);
}
public static byte[] SHA256(byte[] data)
{
using (var h = System.Security.Cryptography.SHA256.Create())
return h.ComputeHash(data);
}
public static byte[] SHA256(Stream stream)
{
using (var h = System.Security.Cryptography.SHA256.Create())
return h.ComputeHash(stream);
}
public static byte[] SHA384(byte[] data)
{
using (var h = System.Security.Cryptography.SHA384.Create())
return h.ComputeHash(data);
}
public static byte[] SHA384(Stream stream)
{
using (var h = System.Security.Cryptography.SHA384.Create())
return h.ComputeHash(stream);
}
public static byte[] SHA512(byte[] data)
{
using (var h = System.Security.Cryptography.SHA512.Create())
return h.ComputeHash(data);
}
public static byte[] SHA512(Stream stream)
{
using (var h = System.Security.Cryptography.SHA512.Create())
return h.ComputeHash(stream);
}
Психанул
+2
using Microsoft.VisualBasic;
public string ConvertEnoviaNameForDB1(string name, char Separ = '%')
{
string functionReturnValue = null;
functionReturnValue = name;
//input filename
//check ENOVIA filenames (example c0234244 --.catpart or c0234244--.catpart)
//output filename in DB format for operator LIKE(c0234244 --.catpart -> c0234244%.catpart)
int i = 0;
int loc1 = Strings.InStr(name, ".CATP", CompareMethod.Text);
bool NeedToConvert = false;
char Chr1 = '1';
char ChrBefore1 = '1';
//check catparts and catproducts ONLY
if (loc1 > 0)
{
int NumSymbols = loc1 - 2;
if (NumSymbols > 6)
NumSymbols = 6;
string tmpstr1 = Strings.Mid(name, loc1 - NumSymbols - 1, NumSymbols);
//analyse 4 chars max
for (i = 0; i <= loc1 - 2; i++)
{
Chr1 = name[loc1 - 2 - i];
//3-string array start from 0(position = count-1)
if ((Strings.Asc(ChrBefore1) >= 65 & Strings.Asc(ChrBefore1) <= 90))
{
if (Chr1 == ' ')
{
i = i + 2;
break; // TODO: might not be correct. Was : Exit For
}
else if (Chr1 == '-')
{
ChrBefore1 = Chr1;
//means can be two chars (ex. "AA") max
}
else if ((Strings.Asc(Chr1) >= 65 & Strings.Asc(Chr1) <= 90) & i < 2)
{
ChrBefore1 = Chr1;
}
else
{
break; // TODO: might not be correct. Was : Exit For
}
}
else if (ChrBefore1 == '-')
{
if (Chr1 == ' ')
{
i = i + 2;
break; // TODO: might not be correct. Was : Exit For
// And i < 3 Then 'means can be "---" - not more
}
else if (Chr1 == '-')
{
//ChrBefore1 = Chr1
}
else
{
i = i + 1;
break; // TODO: might not be correct. Was : Exit For
}
//ChrBefore1 = Chr1
// means start
}
else if (ChrBefore1 == '1')
{
if (Chr1 == '-' | (Strings.Asc(Chr1) >= 65 & Strings.Asc(Chr1) <= 90))
{
ChrBefore1 = Chr1;
}
else
{
i = i + 1;
break; // TODO: might not be correct. Was : Exit For
}
}
}
functionReturnValue = Strings.Left(name, loc1 - i) + Separ + Strings.Right(name, Strings.Len(name) - loc1 + 1);
}
return functionReturnValue;
}
Наличие комментария в 7ой строчке приводит в неописуемый восторг.
Без него понимать поведение функции пришлось бы с болью.
И да, в RegExp могут не только лишь все. Мало кто может.
+3
public class Logger
{
public static string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.log");
public static void Write(string message)
{
using (var sw = File.AppendText(filePath))
{
sw.WriteLine(DateTime.Now);
sw.WriteLine(message);
sw.WriteLine();
sw.Flush();
}
}
public static void Write(Exception exception)
{
using (var sw = File.AppendText(filePath))
{
sw.WriteLine(DateTime.Now);
sw.WriteLine("ERROR:");
sw.WriteLine(exception.Message);
sw.WriteLine(exception.StackTrace);
sw.WriteLine();
sw.Flush();
}
}
}
Нафига готовые решения? Вот - образец велосипедостроения! (И, тссс! Не вздумайте использовать его в многопоточной среде ;) А именно там он и используется по факту :) )
PS угадайте какой фортель выкинет сеё чудо при race condition