|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jun 2001
Città: Catania
Messaggi: 2693
|
[VB 2010] - Problema con function
Salve ragazzi,
come ben sapete sono ormai passato da tempo a VB Net e sto avendo qualche problema nella creazione di questa Function: Codice:
Public Function CheckIfFtpFileContains(ByVal path As String, ByVal SearchStr As String) As Boolean
If (path = Nothing Or path = "") Then
path = "/"
End If
_FtpRequest = CType(WebRequest.Create("ftp://" + _Host + path), FtpWebRequest)
_FtpRequest.Credentials = New NetworkCredential(_UserName, _Password)
_FtpRequest.UsePassive = False
_FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails
_FtpRequest.EnableSsl = _UseSSL
_FtpRequest.Proxy = Nothing
_FtpResponse = CType(_FtpRequest.GetResponse(), FtpWebResponse)
Dim sr As New IO.StreamReader(_FtpRequest.GetResponse().GetResponseStream())
Dim str As String = sr.ReadLine()
While Not str Is Nothing
If Not str.StartsWith(".") Then
CheckIfFtpFileContains = IIf(InStr(str, SearchStr) > 0, True, False)
If CheckIfFtpFileContains = True Then
MessageBox.Show("Trovato")
Exit While
End If
End If
str = sr.ReadLine()
End While
sr.Close()
sr.Dispose()
sr = Nothing
_FtpRequest = Nothing
'Return XXXXXXXXXXXXXXXXXXXXXXXXXXXX
End Function
![]() Come risolvo? Grazie
__________________
Unisciti a noi: http://www.swproduction.altervista.org/ - http://www.enews.altervista.org/
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jan 2007
Messaggi: 404
|
Guardando il codice posso capire se ftp contiene file allora ti restituisce true mentre se non ci sono ti deve restituire falso giusto?
Cmq tornando all'errore in pratica tu non dai nessun valore di ritorno alla funzione quindi io lo modificherei così: Codice:
Public Function CheckIfFtpFileContains(ByVal path As String, ByVal SearchStr As String) As Boolean
Dim Trovato as boolean = False
If (path = Nothing Or path = "") Then
path = "/"
End If
_FtpRequest = CType(WebRequest.Create("ftp://" + _Host + path), FtpWebRequest)
_FtpRequest.Credentials = New NetworkCredential(_UserName, _Password)
_FtpRequest.UsePassive = False
_FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails
_FtpRequest.EnableSsl = _UseSSL
_FtpRequest.Proxy = Nothing
_FtpResponse = CType(_FtpRequest.GetResponse(), FtpWebResponse)
Dim sr As New IO.StreamReader(_FtpRequest.GetResponse().GetResponseStream())
Dim str As String = sr.ReadLine()
While Not str Is Nothing
If Not str.StartsWith(".") Then
CheckIfFtpFileContains = IIf(InStr(str, SearchStr) > 0, True, False)
If CheckIfFtpFileContains = True Then
Trovato =true
MessageBox.Show("Trovato")
Exit While
End If
End If
str = sr.ReadLine()
End While
sr.Close()
sr.Dispose()
sr = Nothing
_FtpRequest = Nothing
Return Trovato
End Function
Ultima modifica di jackk87 : 18-12-2010 alle 15:33. |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Dec 2004
Messaggi: 3210
|
Aggiungo una considerazione su quel tipo di errore : da notare quel "on all code paths".
Una Function deve garantire sempre la restituzione del valore o oggetto previsto, in tutti i possibili percorsi del codice, perciò non è sufficiente piazzare un "Return" prima dell'End Function, per cavarsela. Ad esempio : Codice:
Public Function metodo(ByVal arg As Integer) As String
Select Case arg
Case 1
Return "Uno"
Case 2
Return "Due"
Case Else
End Select
End Function
Questo fatto può sembrare banale, all'apparenza, ma nella pratica non lo è affatto. Possono esserci "percorsi" del codice nascosti, basti pensare alle istruzioni che possono sollevare eccezioni. In un metodo "a regola d'arte" bisognerebbe prevedere ogni errore possibile e separare il codice in modo che ciascuna istruzione critica, in caso di errore ( Try Catch ) vada a restituire un valore o oggetto opportuno, ma in nessun caso deve bloccare l'esecuzione...
__________________
Contattami su FaceBook --> [ ::: MarcoGG su FaceBook ::: ] Visita il mio Blog --> [ ::: Il Blog di MarcoGG ::: ] |
|
|
|
|
|
#4 | |
|
Senior Member
Iscritto dal: Jan 2007
Messaggi: 404
|
Quote:
|
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Jun 2001
Città: Catania
Messaggi: 2693
|
Grazie 1000 ad entrambe;
alla fine ho cmq risolto applicando in anticipo la soluzione suggerita da jackk87
__________________
Unisciti a noi: http://www.swproduction.altervista.org/ - http://www.enews.altervista.org/
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:25.





















