Ho preso un esempio da CodeProject e mi sono accorto che il codice è fortemente cablato e impossibile da riutilizzare. Dovendo riscriverlo ne approfitto per far condividere quelli che secondo me sono i vantaggi di uno stile corretto di programmazione.
Questo è il codice come veniva presentato dall'esempio
E questo è come penso debba essere strutturato un codice per essere riusato:Codice PHP:private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("You have not typed the URL", "URL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string URL = textBox1.Text;
string filetype = URL.Substring(URL.LastIndexOf(".") + 1, (URL.Length - URL.LastIndexOf(".") - 1));
filetypevalue.Text = filetype.ToUpper();
string filename = URL.Substring(URL.LastIndexOf("/") + 1, (URL.Length - URL.LastIndexOf("/") - 1));
namelabel.Text = filename;
System.Net.WebRequest req = System.Net.HttpWebRequest.Create(textBox1.Text);
req.Method = "HEAD";
System.Net.WebResponse resp = req.GetResponse();
long ContentLength = 0;
long result;
if (long.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
{
string File_Size;
if (ContentLength >= 1073741824)
{
result = ContentLength / 1073741824;
kbmbgb.Text = "GB";
}
else if (ContentLength >= 1048576)
{
result = ContentLength / 1048576;
kbmbgb.Text = "MB";
}
else
{
result = ContentLength / 1024;
kbmbgb.Text = "KB";
}
File_Size = result.ToString("0.00");
sizevaluelabel.Text = File_Size;
}
}
}
Codice PHP:public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("You have not typed the URL", "URL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//aggiorno la grafica della form da una classe esterna in modo da limitare le dimensioni in numero di metodi della classe corrente
FormHelper.AggiornaGrafica_SettaNomeETipoFile(this);
long ContentLength = WebUtiliy.GetContentLength(textBox1.Text);
//aggiorno la grafica della form da una classe esterna in modo da limitare le dimensioni in numero di metodi della classe corrente
FormHelper.AggiornaGrafica_SettaDimensioniFile(this, ContentLength);
}
}
}
public static class WebUtiliy
{
/// <summary>
/// Restituisce la dimensione in byte di un oggetto presente ad un determinato url
/// </summary>
public static long GetContentLength(string url)
{
WebRequest req = HttpWebRequest.Create(url);
req.Method = "HEAD";
WebResponse webResponse = req.GetResponse();
//Calcola la dimensione del file via web
return GetContentLength(webResponse);
}
/// <summary>
/// Restituisce la dimensione in byte di un oggetto WebResponse
/// </summary>
public static long GetContentLength(WebResponse webResponse)
{
long ContentLength = 0;
long.TryParse(webResponse.Headers.Get("Content-Length"), out ContentLength);
return ContentLength;
}
/// <summary>
/// Metodo che converte un intero rappresentante una dimensione in Byte nella sua rappresentazione stringa in KB/MB/GB
/// </summary>
public static long ConvertByteDimensionToString(long ContentLength, out string unitaDiMisura)
{
long result;
if (ContentLength >= 1073741824)
{
result = ContentLength / 1073741824;
unitaDiMisura = "GB";
}
else if (ContentLength >= 1048576)
{
result = ContentLength / 1048576;
unitaDiMisura = "MB";
}
else
{
result = ContentLength / 1024;
unitaDiMisura = "KB";
}
return result;
}
}
/// <summary>
/// Classe utilizzata per raggruppare i metodi che modificano l'interfaccia grafica della form
/// </summary>
public static class FormHelper
{
public static void AggiornaGrafica_SettaDimensioniFile(Form1 form1, long ContentLength)
{
string unitaDiMisura;
string File_Size = WebUtiliy.ConvertByteDimensionToString(ContentLength, out unitaDiMisura).ToString("0.00");
form1.kbmbgb.Text = unitaDiMisura;
form1.sizevaluelabel.Text = File_Size;
}
public static void AggiornaGrafica_SettaNomeETipoFile(Form1 form1)
{
string URL = form1.textBox1.Text;
string filetype = URL.Substring(URL.LastIndexOf(".") + 1, (URL.Length - URL.LastIndexOf(".") - 1));
string filename = URL.Substring(URL.LastIndexOf("/") + 1, (URL.Length - URL.LastIndexOf("/") - 1));
form1.filetypevalue.Text = filetype.ToUpper();
form1.namelabel.Text = filename;
}
}


Rispondi quotando