2

私はこの機能を持っています:

Function WriteTextToFile(ByVal data)
    Dim file As New System.IO.StreamWriter("E:\storage.txt")
    file.WriteLine(data)
    file.Close()
End Function

複数のストレージファイルを許可するためにファイルパスを渡すことができる変数になるように調整しようとしています。このようなもの:

Function AppendTextToFile(ByVal data, ByVal path)

        Dim file As New System.IO.StreamWriter(path, True)
        file.WriteLine(data)
        file.Close()

    End Function

ただし、関数1は機能しますが、関数2は機能しません-この役に立たないエラーが発生します。

Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
    'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'Object' to 'String'.
    'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'Object' to 'System.IO.Stream'. C:\Users\films ratings\films ratings\Form1.vb

私は通常PHPで作業していますが、この種のエラーはこれまで見たことがありません。それはどういう意味ですか?関数を書き直して必要なものを取得できますか?

4

2 に答える 2

4

変数pathString型として宣言すれば大丈夫です。

パスパラメーターをとして宣言したため、コンパイラーはコンストラクターのどのオーバーロードを使用するかを決定できませObjectん(特に指定しない場合はこれがデフォルトです)。

代わりに、次のように2番目の関数を宣言する必要があります。

Function AppendTextToFile(ByVal data, ByVal path As String)
于 2011-05-14T10:30:18.370 に答える
4

path(およびdata)パラメータをオブジェクトとして渡します。常に変数を強く入力するだけで、ここでのエラーはなくなります。

Function AppendTextToFile(ByVal data As String, ByVal path As String)

    Dim file As New System.IO.StreamWriter(path, True)
    file.WriteLine(data)
    file.Close()

End Function
于 2011-05-14T10:31:22.567 に答える