Software Design Question: Properties and Functions

P

pbd22

Hi.

I have recently been working with FTP using microsofts clsFTP class.
I have been attempting to alter the clsFTP.Upload method to get the
number of bytes as
they are being sent in addition to sending the actual file.

to do this, i have added the following property:

Public Property ByteCount() As Int32
Get
Return m_iBytesTotal
End Get
Set(ByVal value As Int32)
m_iBytesTotal = value
End Set
End Property

and i have changed the upload Sub to a Function in addition to adding
the below 2 lines
(between "?" lines) to the function:

[SNIP....]

input = New FileStream(sFileName, FileMode.Open)
' Upload the file
m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)

Do While (m_iBytes > 0)

cSocket.Send(m_aBuffer, m_iBytes, 0)
m_iBytes = input.Read(m_aBuffer, 0,
m_aBuffer.Length)
'??????????????????????????????????????????????????????????????????????????????
m_iBytesTotal += m_iBytes
Return ByteCount = m_iBytesTotal
'??????????????????????????????????????????????????????????????????????????????
Loop

input.Close()

[SNIP...]

This works great. I can get the bytes in progress once the upload is
started. The problem
is that the "actual upload" doesnt happen. Is it possible that the fact
that i have changed
this code from a Sub to a Function (in addition to adding a return
statement inside the while
loop) is causing this problem? When I remove the 2 lines and change
this Function back to
a Sub the upload goes without a hitch.

i hope i have explained myself clearly.
thanks in advance.
 
P

pbd22

Thanks Dale.

I can't really do that because the function (it was originally a Sub)
handles a bunch of
other code and m_iBytes is used in more than one place. what i am
trying to do:

1. upload the file in the while loop to the FTP server
2. count the bytes as the file gets uploaded and return the numbers,
real-time

so, my problem is essentially, how do i access a count *as it is
happening* from a
while loop inside a Sub? i was hoping to do this by accessing the
property ByteCount() and it works if the property is returned from the
while loop. but, as you point out, the method exits there and nothing
else gets executed (so the file doesnt ever upload).

I'll try exploring yoru ideas a bit more. let me know if any others
occur.
thanks again.

ps - the full upload method(s) below (from the FTP class):


' Upload a file.
Public Sub UploadFile(ByVal sFileName As String)
UploadFile(sFileName, False)

End Sub

' Upload a file and set the resume flag.
Public Sub UploadFile(ByVal sFileName As String, ByVal bResume
As Boolean)
Dim cSocket As Socket
Dim offset As Long
Dim input As FileStream
Dim bFileNotFound As Boolean
If (Not (m_bLoggedIn)) Then
Login()
End If

cSocket = CreateDataSocket()
offset = 0

If (bResume) Then

Try

SetBinaryMode(True)

offset = GetFileSize(sFileName)

Catch ex As Exception

offset = 0

End Try

End If

If (offset > 0) Then

SendCommand("REST " & offset)

If (m_iRetValue <> 350) Then

'throw new IOException(reply.Substring(4));

'Remote server may not support resuming.

offset = 0

End If

End If

SendCommand("STOR " & Path.GetFileName(sFileName))

If (Not (m_iRetValue = 125 Or m_iRetValue = 150)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

' Check to see if the file exists before the upload.

bFileNotFound = False

If (File.Exists(sFileName)) Then

' Open input stream to read source file

input = New FileStream(sFileName, FileMode.Open)

If (offset <> 0) Then

input.Seek(offset, SeekOrigin.Begin)

End If

' Upload the file

m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)

Do While (m_iBytes > 0)

cSocket.Send(m_aBuffer, m_iBytes, 0)
m_iBytes = input.Read(m_aBuffer, 0,
m_aBuffer.Length)

m_iBytesTotal += m_iBytes
ByteCount = m_iBytesTotal

Return ByteCount

Loop

input.Close()

Else

bFileNotFound = True

End If

If (cSocket.Connected) Then

cSocket.Close()

End If

' No point in reading the return value if the file was
' not found.

If (bFileNotFound) Then

MessageString = m_sReply
Throw New IOException("The file: " & sFileName & " was
not found. Can not upload the file to the FTP Server.")

End If

ReadReply()

If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub
 
T

Tom Leylan

Take a step back from the immediate problem and consider it the more generic
question "how do I communicate from within a process to objects outside the
process?" That you want a count is simply one example.

The keyword is "events". Raise an event passing the count as a customized
(subclassed) EventArgs.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top