vangelisbb Δημοσ. 1 Φεβρουαρίου 2013 Δημοσ. 1 Φεβρουαρίου 2013 Για και χαρά σας Είμαι νέος στον προγραμματισμό και θα ήθελα την άποψή σας στο θέμα κατά πόσο χρειάζεται και θα μπορούσε αυτός ο κώδικας να γραφτεί ποιο όμορφα. παρακάτω παραθέτω τον κώδικα από μία φόρμα. το οποίο είναι γραμμένο σε VB 2010 Ευχαριστώ για τον χρόνο σας Imports System Imports System.Xml Imports System.IO Imports FSUIPC Imports MySql.Data Imports MySql.Data.MySqlClient Imports System.Text.RegularExpressions Public Class frm_home Public dbstr As String = "Server=localhost;database=mydb;uid=root;pwd=dbpass" Public dbcon As New MySqlConnection(dbstr) Dim strquery As String = "" Dim mysqlcmd As MySqlCommand Dim dr As MySqlDataReader Public dbadapter As New DataTable Private Sub frm_home_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load readpass() OpenFSUIPC() dbocon() pirepquerie() pilotinfo() pireprow() End Sub Private Sub pireprow() Dim id As String = lblid.Text Dim da As New MySqlDataAdapter("SELECT submitdate, flightnum, depicao, arricao, flighttime from phpvms_pireps WHERE pilotid='" & id & "'", dbcon) Dim dt As New DataTable da.Fill(dt) pirepgrid.DataSource = dt End Sub Private Sub pilotinfo() Dim id As String = lblid.Text Dim totalhours As String Dim transferhours As String Dim totalhour As String strquery = "SELECT * FROM phpvms_pilots WHERE pilotid='" & id & "'" mysqlcmd = New MySqlCommand(strquery, dbcon) dbcon.Open() dr = mysqlcmd.ExecuteReader While dr.Read txtid.Text = dr.Item("code") & dr.Item("pilotid") txtname.Text = dr.Item("lastname") & Space(1) & dr.Item("firstname") txtemail.Text = dr.Item("email") txthub.Text = dr.Item("hub") txtflights.Text = dr.Item("totalflights") totalhours = dr.Item("totalhours") transferhours = dr.Item("transferhours") totalhour = (totalhours + transferhours) txthours.Text = totalhour End While dr.Close() dbcon.Close() End Sub Public Sub dbocon() Try Dim username As String = lblusername.Text Dim str() As String Dim str2 As String = username str = Regex.Split(str2, "GSA") Dim exusername As String = str(1) lblid.Text = str(1) Dim id As String = lblid.Text strquery = "SELECT * FROM phpvms_pilots WHERE pilotid='" & id & "'" mysqlcmd = New MySqlCommand(strquery, dbcon) dbcon.Open() dr = mysqlcmd.ExecuteReader While dr.Read lblpilotname.Text = dr.Item("lastname") & Space(1) & dr.Item("firstname") lblprank.Text = dr.Item("rank") End While dr.Close() dbcon.Close() Catch ex As Exception MsgBox("Something Went Wrong" & ex.Message) End Try End Sub Public Sub pirepquerie() Dim table As New DataTable Dim id As String = lblid.Text Dim strquery As String = "SELECT * FROM phpvms_pireps WHERE pilotid='" & id & "'" Try dbcon.Open() pirepgrid.DataSource = strquery dbcon.Close() Catch ex As Exception MsgBox("Datagrid Went Wrong" & ex.Message) End Try End Sub Public Sub OpenFSUIPC() Try ' Attempt to open a connection to FSUIPC (running on any version of Flight Sim) FSUIPCConnection.Open() ' Opened OK so disable the Connect button Me.btnconnect.Enabled = False lblfsuipcstatus.Text = "Conected" lblfsuipcstatus.ForeColor = Color.Green ' and start the timer ticking to drive the rest of the application Me.Timer1.Interval = 200 Me.Timer1.Enabled = True ' Set the AI object Catch ex As Exception ' Badness occurred - show the error message lblfsuipcstatus.Text = "Disconected" lblfsuipcstatus.ForeColor = Color.Red End Try End Sub Public Sub readpass() Dim username As String 'check if file myxml.xml is existing If (IO.File.Exists("c:\pass.xml")) Then 'create a new xmltextreader object 'this is the object that we will loop and will be used to read the xml file Dim document As XmlReader = New XmlTextReader("c:\pass.xml") 'loop through the xml file While (document.Read()) Dim type = document.NodeType 'if node type was element If (type = XmlNodeType.Element) Then 'if the loop found a <FirstName> tag If (document.Name = "Username") Then lblusername.Text = document.ReadInnerXml.ToString() lblusername.Font = New Font(lblusername.Font, FontStyle.Bold) username = document.ReadInnerXml.ToString() document.Close() End If End If End While Else End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Try Catch exFSUIPC As FSUIPCException If exFSUIPC.FSUIPCErrorCode = FSUIPCError.FSUIPC_ERR_SENDMSG Then ' Send message error - connection to FSUIPC lost. ' Show message, disable the main timer loop and relight the ' connection button: ' Also Close the broken connection. Me.Timer1.Enabled = False Me.btnconnect.Enabled = True FSUIPCConnection.Close() lblfsuipcstatus.Text = "disconected" lblfsuipcstatus.ForeColor = Color.Red Else ' not the disonnect error so some other baddness occured. ' just rethrow to halt the application Throw exFSUIPC End If End Try End Sub Private Function MySqlDataAdapter(ByVal strquery As String, ByVal dbcon As Object) As MySqlDataAdapter Throw New NotImplementedException End Function End Class
goldenlion Δημοσ. 1 Φεβρουαρίου 2013 Δημοσ. 1 Φεβρουαρίου 2013 Καλησπέρα σου 1. Μόνο κάτι κενές γραμμές θα μείωνα εγώ και 2. προσθήκη μεγάλου αριθμού απο σχόλια! είναι σε θετικά ικανοποιητικά επίπεδα γραμμένος ο κώδικας αν θέλεις να το φτιάξεις παραπάνω απο οτι είπα τότε αξίζει μόνο αν πάει για παρουσίαση σε μεγάλο κοινό και τέτοια φάση...
MitsakosGR Δημοσ. 1 Φεβρουαρίου 2013 Δημοσ. 1 Φεβρουαρίου 2013 Σίγουρα θέλει σχόλια όπως είπε και ο goldenlion. Σβήσε μερικές κενές γραμμές από την "readpass" (στα End If). Δική μου πρόταση είναι να υπάρχουν 1-2 κενές γραμμές όπου αλλάζει κάτι (πχ ανάμεσα στις function ή στα import και την class) έτσι ώστε όποιος το διαβάσει να ξέρει που αρχίζει και τελειώνει ένα block κώδικα. Επίσης στα queries με τη βάση μην βάζεις "καρφωτά" τις παραμέτρους. Χρησιμοποίησε τα Parameters (MySqlParameter στην περίπτωσή σου). 1
iceblade Δημοσ. 1 Φεβρουαρίου 2013 Δημοσ. 1 Φεβρουαρίου 2013 1) Πρόσθεση κενών γραμμών ανάμεσα στα functions 2) Αφαίρεση περιττών κενών γραμμών στα End While End If κλπ 3) Πρόσθεση σχολίου στην αρχή κάθε function που περιγράφει συνοπτικά τι κάνει αυτό το function
albNik Δημοσ. 1 Φεβρουαρίου 2013 Δημοσ. 1 Φεβρουαρίου 2013 Γενικά να αποφεύγεις public fields, καλύτερα (read only) properties. Ότι έχει Dispose() βάλε using (όχι Close). 1
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα