重新申报? [英] Re-declare??

查看:83
本文介绍了重新申报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我有一个叫另一个子的Sub。两个subs都使用一个公共对象,所以我用
使用Public在模块的顶部声明它,如下所示。


Public Writer As StreamWriter = File .CreateText(Application.StartupPath&

" \Update.txt")

我的Sub第一次运行时工作正常。但它第二次失败

告诉我无法写入已关闭的TextWriter。我猜这是

,因为Public部分只读取一次应用程序的实例。我需要关闭TextWriter,以便我可以在另一个进程中使用该文件。

我知道如何确保重新读取Public部分,或者,

为第二个Sub提供TextWriter?


-

Bob Hollness


-------------------------------------

我'请给我一个B请鲍勃

Hi all.

I have a Sub that calls another sub. Both subs use a common object, so I
used Public to declare it at the top of the module, as below.

Public Writer As StreamWriter = File.CreateText(Application.StartupPath &
"\Update.txt")

The first time my Sub runs it works fine. But the second time it fails
telling me "Cannot write to a closed TextWriter. I am guessing that this is
because the Public section is only read once instance of the application. I
have to close the TextWriter so that I can use the file in another process.
Any idea how I can either ensure that the Public section is re-read, or,
provide the TextWriter to the 2nd Sub?

--
Bob Hollness

-------------------------------------
I''ll have a B please Bob

推荐答案

我觉得我对你需要做的事情感到困惑。但你的问题是




如果你这样声明:


Public Writer As StreamWriter


Public Sub1()

如果Writer为Nothing则

Wrier = File.CreateText(Application.StartupPath&" \ Update .txt")

结束如果

''做点什么

结束子


Public Sub2 ()

如果Writer是Nothing那么

Wrier = File.CreateText(Application.StartupPath&" \Update.txt")

结束如果

''做某事

结束子


Public Sub DoSomethingThatMustCloseFile

作家。关闭

作家=没什么

结束子


现在这种方式打开文件如果关闭了。如果已经打开,则使用相同的

文件。我想这就是你所说的

在做什么。如果没有,你可以发布一些示例代码来更多地显示问题吗?


Chris

" Bob Hollness" < bo*@blockbuster.com>在消息中写道

新闻:好的************** @ TK2MSFTNGP14.phx.gbl ...
I think I''m a little confused at what you need to do. But your problem lies
in

If you declare like this:

Public Writer As StreamWriter

Public Sub1()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
''Do Something
End Sub

Public Sub2()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
''Do Something
End Sub

Public Sub DoSomethingThatMustCloseFile
Writer.Close
Writer = Nothing
End Sub

Now this way of doing it opens the file if it was closed. And uses the same
file if it was already opened. I think this is what you are talking about
doing. If not, can you post some sample code that shows the problem more?

Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
大家好。

我有一个调用另一个子的Sub。两个subs都使用一个公共对象,所以我使用Public在模块的顶部声明它,如下所示。

Public Writer As StreamWriter = File.CreateText(Application.StartupPath&
" \Update.txt")

第一次我的Sub运行它工作正常。但它第二次失败告诉我无法写入已关闭的TextWriter。我猜这个
是因为Public部分只读取了一次
应用程序的实例。我必须关闭TextWriter才能在另一个进程中使用该文件。知道如何确保重新阅读Public部分
,或者将TextWriter提供给第二个Sub?

-
Bob Hollness

-------------------------------------
我会的a B请Bob
Hi all.

I have a Sub that calls another sub. Both subs use a common object, so I
used Public to declare it at the top of the module, as below.

Public Writer As StreamWriter = File.CreateText(Application.StartupPath &
"\Update.txt")

The first time my Sub runs it works fine. But the second time it fails
telling me "Cannot write to a closed TextWriter. I am guessing that this
is because the Public section is only read once instance of the
application. I have to close the TextWriter so that I can use the file in
another process. Any idea how I can either ensure that the Public section
is re-read, or, provide the TextWriter to the 2nd Sub?

--
Bob Hollness

-------------------------------------
I''ll have a B please Bob



我觉得我对你需要做的事情感到困惑。但你的问题是




如果你这样声明:


Public Writer As StreamWriter


Public Sub1()

如果Writer为Nothing则

Wrier = File.CreateText(Application.StartupPath&" \ Update .txt")

结束如果

''做点什么

结束子


Public Sub2 ()

如果Writer是Nothing那么

Wrier = File.CreateText(Application.StartupPath&" \Update.txt")

结束如果

''做某事

结束子


Public Sub DoSomethingThatMustCloseFile

作家。关闭

作家=没什么

结束子


现在这种方式打开文件如果关闭了。如果已经打开,则使用相同的

文件。我想这就是你所说的

在做什么。如果没有,你可以发布一些示例代码来更多地显示问题吗?


Chris

" Bob Hollness" < bo*@blockbuster.com>在消息中写道

新闻:好的************** @ TK2MSFTNGP14.phx.gbl ...
I think I''m a little confused at what you need to do. But your problem lies
in

If you declare like this:

Public Writer As StreamWriter

Public Sub1()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
''Do Something
End Sub

Public Sub2()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
''Do Something
End Sub

Public Sub DoSomethingThatMustCloseFile
Writer.Close
Writer = Nothing
End Sub

Now this way of doing it opens the file if it was closed. And uses the same
file if it was already opened. I think this is what you are talking about
doing. If not, can you post some sample code that shows the problem more?

Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
大家好。

我有一个调用另一个子的Sub。两个subs都使用一个公共对象,所以我使用Public在模块的顶部声明它,如下所示。

Public Writer As StreamWriter = File.CreateText(Application.StartupPath&
" \Update.txt")

第一次我的Sub运行它工作正常。但它第二次失败告诉我无法写入已关闭的TextWriter。我猜这个
是因为Public部分只读取了一次
应用程序的实例。我必须关闭TextWriter才能在另一个进程中使用该文件。知道如何确保重新阅读Public部分
,或者将TextWriter提供给第二个Sub?

-
Bob Hollness

-------------------------------------
我会的a B请Bob
Hi all.

I have a Sub that calls another sub. Both subs use a common object, so I
used Public to declare it at the top of the module, as below.

Public Writer As StreamWriter = File.CreateText(Application.StartupPath &
"\Update.txt")

The first time my Sub runs it works fine. But the second time it fails
telling me "Cannot write to a closed TextWriter. I am guessing that this
is because the Public section is only read once instance of the
application. I have to close the TextWriter so that I can use the file in
another process. Any idea how I can either ensure that the Public section
is re-read, or, provide the TextWriter to the 2nd Sub?

--
Bob Hollness

-------------------------------------
I''ll have a B please Bob



感谢您的回复。这是我的代码。正如你所看到的那样,它会通过反复调用自己来回复。然后子ReadAllFiles整理了关闭流的

。我的问题是第二次在ReadDirs Sub中的

line" .WriteLine(sFile)上运行它的错误。

Sub ReadAllFiles

>
致电ReadDirs(WHATEVER_PATH,True)


WriteTxtUpdate.Flush

WriteTxtUpdate.Close


结束Sub

Sub ReadDirs(ByVal Sourcedir As String,ByVal递归为布尔值)


Sourcedir& = Path.DirectorySeparatorChar


如果fRecursive那么

每个sDir在Directory.GetDirectories(Sourcedir)

sDirInfo =新DirectoryInfo(sDir)

ReadDirs( sDirInfo.FullName,fRecursive)

sDirInfo =没什么

下一页

结束如果


使用WriteTxtUpdate

每个sFile在Directory.GetFiles(Sourcedir)

.WriteLine(sFile)

下一页

结束


结束子


-

Bob Hollness


-------------------------------------

我'请有一个B请鲍勃


克里斯,万事万物的主人 < chris@No_Spam_Please.com>在消息新闻中写了

:e0 ************* @ TK2MSFTNGP12.phx.gbl ...
Thanks for the reply. This is my code. As you can see, it recurses by
calling itself repeatedly. And then the sub ReadAllFiles tidies up by
closing the stream. My problem is that the 2nd time it runs it errors on
line ".WriteLine(sFile) in the ReadDirs Sub.
Sub ReadAllFiles

Call ReadDirs(WHATEVER_PATH, True)

WriteTxtUpdate.Flush
WriteTxtUpdate.Close

End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)

Sourcedir &= Path.DirectorySeparatorChar

If fRecursive Then
For Each sDir In Directory.GetDirectories(Sourcedir)
sDirInfo = New DirectoryInfo(sDir)
ReadDirs(sDirInfo.FullName, fRecursive)
sDirInfo = Nothing
Next
End If

With WriteTxtUpdate
For Each sFile In Directory.GetFiles(Sourcedir)
.WriteLine(sFile)
Next
End With

End Sub

--
Bob Hollness

-------------------------------------
I''ll have a B please Bob

"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:e0*************@TK2MSFTNGP12.phx.gbl...
我想我是对你需要做的事感到困惑。但是你的问题是

如果你这样声明:

Public Writer As StreamWriter

Public Sub1()
如果Writer什么都没有那么
Wrier = File.CreateText(Application.StartupPath&" \ Update.txt")
结束如果
'做某事
结束Sub

公共Sub2()
如果Writer什么都没有那么
Wrier = File.CreateText(Application.StartupPath&" \Update.txt")
结束如果
''做点什么
结束Sub

Public Sub DoSomethingThatMustCloseFile
Writer.Close
Writer = Nothing
End Sub

现在这种方式打开文件,如果它已关闭。如果已经打开,则使用
相同的文件。我认为这就是你在谈论的事情。如果没有,你能发布一些显示问题的示例代码吗?

Chris

" Bob Hollness" < bo*@blockbuster.com>在消息中写道
新闻:好的************** @ TK2MSFTNGP14.phx.gbl ...
I think I''m a little confused at what you need to do. But your problem
lies in

If you declare like this:

Public Writer As StreamWriter

Public Sub1()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
''Do Something
End Sub

Public Sub2()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
''Do Something
End Sub

Public Sub DoSomethingThatMustCloseFile
Writer.Close
Writer = Nothing
End Sub

Now this way of doing it opens the file if it was closed. And uses the
same file if it was already opened. I think this is what you are talking
about doing. If not, can you post some sample code that shows the problem
more?

Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
大家好。

Public Writer As StreamWriter = File.CreateText(Application.StartupPath&
" \Update.txt")

第一次我的Sub运行它工作正常。但它第二次失败告诉我无法写入已关闭的TextWriter。我猜这个
是因为Public部分只读取了一次
应用程序的实例。我必须关闭TextWriter才能在另一个进程中使用该文件。知道如何确保重新阅读Public
部分,或者将TextWriter提供给第二个Sub?

-
Bob Hollness

-------------------------------------
我会的a B请Bob
Hi all.

I have a Sub that calls another sub. Both subs use a common object, so I
used Public to declare it at the top of the module, as below.

Public Writer As StreamWriter = File.CreateText(Application.StartupPath &
"\Update.txt")

The first time my Sub runs it works fine. But the second time it fails
telling me "Cannot write to a closed TextWriter. I am guessing that this
is because the Public section is only read once instance of the
application. I have to close the TextWriter so that I can use the file
in another process. Any idea how I can either ensure that the Public
section is re-read, or, provide the TextWriter to the 2nd Sub?

--
Bob Hollness

-------------------------------------
I''ll have a B please Bob




这篇关于重新申报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆