【问题标题】:Visual Basic - How to use a variable from one function in anotherVisual Basic - 如何在另一个函数中使用一个函数的变量
【发布时间】:2014-11-18 00:46:01
【问题描述】:

我已经检查了谷歌和这里的建议答案,但不幸的是没有运气。

我需要做的最后一件事是让电子邮件将rateNbr 变量读入电子邮件正文,但它只是空的。

我尝试将Public Function FuncRateCheckFile 读作Public Function FuncRateCheckFile(ByVal rateNbr As String),以尝试使其能够在函数外部调用,但这会在其他地方调用时破坏函数。 :(

这里是代码,关于我指的是哪里的 cmets:

Public Function FuncRateCheckFile()

    Dim blnContinue As Boolean
    Dim strLine As String
    Dim strSearchFor, strSearchWrd, LineCount, objFSO, objTextFile, arrLines
    Dim dteNow As Date
    Dim newDate As String
    '//==============================================================================================
    '//  DECLARED
    Dim rateNbr As String
    '//==============================================================================================

    FuncRateCheckFile = False
    blnContinue = True


    If blnContinue Then

        Const ForReading = 1
            'Get todays date and reformat it
            dteNow = DateValue(Now)
            newDate = Format(dteNow, "dd/MM/yy")
            strSearchWrd = newDate
            'Read the whole file
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objTextFile = objFSO.OpenTextFile(m_RateCheckFile, ForReading)

            LineCount = 0
            Do Until objTextFile.AtEndOfStream

                strLine = objTextFile.ReadLine()

                If InStr(strLine, strSearchWrd) <> 0 Then

                   arrLines = Split(strLine, vbCrLf)
                   LineCount = LineCount + 1

                End If
            Loop

                'Log a message to state how many lines have todays day, and if there are none, log an error
                If LineCount <> 0 Then
                    '//==============================================================================================
                    '//  "rateNbr" IS WHAT I AM TRYING TO GET TO PUT IN THE EMAIL
                    LogMessage "Rate file date is correct"
                    rateNbr = "Number of rates for " & newDate & " in the file recieved on " & newDate & " is " & LineCount
                    LogMessage rateNbr
                    EmailAdvice2
                    objTextFile.Close
                    '//==============================================================================================

                Else

                    blnContinue = False
                    LogError "Failed to retrieve Current Rate date, please check rate file.."
                    EmailAdvice
                    objTextFile.Close

                End If

    End If

    FuncRateCheckFile = blnContinue

    LogMessage "Completed Check Rate file"

End Function


Private Function EmailAdvice2()


Dim strSMTPFrom As String
Dim strSMTPTo As String
Dim strSMTPRelay As String
Dim strTextBody As String
Dim strSubject As String
Dim oMessage As Object
'//==============================================================================================
'//  DECLARED AGAIN
Dim rateNbr As String
'//==============================================================================================

Set oMessage = CreateObject("CDO.Message")
strSMTPFrom = "no-reply@work.com.au"
strSMTPTo = "me@work.com.au"
strSMTPRelay = "smtp.relay.com"
'//==============================================================================================
'//  THIS MAKES THE TEXT BODY BLANK, BUT THE EMAIL STILL SENDS
strTextBody = rateNbr
'//==============================================================================================
strSubject = "Todays rates"
'strAttachment = "full UNC path of file"

oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMessage.Configuration.Fields.Update

oMessage.Subject = strSubject
oMessage.From = strSMTPFrom
oMessage.To = strSMTPTo
oMessage.textbody = strTextBody
'oMessage.AddAttachment strAttachment


oMessage.Send

End Function

我很肯定它是空白的,因为我在EmailAdvice2() 下声明了rateNbr,然后没有给它任何东西来填充变量。但是不知道怎么让它调用FuncRateCheckFile()下的变量。

感谢大家的帮助。

【问题讨论】:

  • 事实上,您的问题是代码转储。请将代码简化为说明您的问题所需的最小示例。
  • 查看Scope;在过程中声明,rateNbr 仅存在于本地。所以你有两个,一个在FuncRateCheckFileEmailAdvice2。这甚至是VB.NET吗?有一些非常 VBA 外观的语法和对象使用,例如 FileScripting 对象。
  • 您发布了一个非常含糊(而且有些杂乱无章)的问题描述和一堵巨大的代码墙。这不是问题在这里的工作方式。请edit 明确说明问题,将代码减少到您的问题所需的最低限度,并就该代码提出具体问题。
  • 抱歉,我已编辑以使代码更有意义并减少代码。我在发帖之前没有读过它,我应该读过。它看起来确实像漫无边际。我什至没有完成标题!
  • 这看起来不像 VB.Net 代码。

标签: function vb6 basic


【解决方案1】:

正如 Plutonix 所说,这是一个范围问题。

将“rateNbr”变量的声明移到类级别,并删除函数内的本地声明:

Dim rateNbr As String ' <-- out at class level it will be accessible from both functions

Public Function FuncRateCheckFile()
    ...
    ' REMOVE both the decalarations of "rateNbr" that are INSIDE your functions
    ...
End Function 

Private Function EmailAdvice2()
    ...
    ' REMOVE both the decalarations of "rateNbr" that are INSIDE your functions
    ...
End Function 

【讨论】:

  • 啊,伙计。这太明显了,我现在觉得有点傻。有时你只需要另一双眼睛,我猜。非常感谢,非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2013-04-22
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多