问题
Below is the snippet written in a VB .cls file :
Public Property Get Request() As String
Request = m_sRequest
End Property
Public Property Let Request(sData As String)
m_sRequest = sData
ParseRequest sData
End Property
In another class the line below is used:
Public Sub LogError(Request As RequestParameters, ByVal sData As String, ibErr As CibErr)
Dim sErrorLog as string
sErrorLog = Request("MonitorPath") & "\Log\Debug\Errors"
If Dir(sErrorLog, vbDirectory) = "" Then
MkDir sErrorLog
End If
.
.
.
End Sub
I'm trying to migrate this code to C#, and I don't understand how Request("MonitorPath")
is returning a string.
If yes - how, as Let
does not have any return type?
If no - how does sErrorLog = Request("MonitorPath") & "\Log\Debug\Errors"
work?
回答1:
If Request("MonitorPath")
is in a class that does not contain Property Get/Get Request()
then its using a method within itself that is called Request
. (It can't call another classes property without an instance qualification).
回答2:
The equivalent C# to that old vb6 code looks like this:
private string m_Request;
public string Request
{
get {return m_Request;}
set
{
m_Request = value;
ParseRequest(value);
}
}
The C# equivalent to the function is this:
public void LogError(RequestParameters Request, string Data, CibErr ibErr)
{
// the "Request" here is a different Request than the property above
// I have to guess a bit, but I think it's probably an indexed property
string ErrorLog = Request["MonitorPath"] + @"\Log\Debug\Errors";
// There is no need to check if the folder exists.
// If it already exists, this call will just complete with no changes
Directory.CreateDirectory(ErrorLog);
//generally, checking for existence of items in the file system before using them is BAD
// the file system is volatile, and so checking existence is a race condition
// instead, you need to have good exception handling
}
For the type parts of the question, if you don't specify a return type for an item, the return type is Object
. But that's just the compiler type. The actual object reference will have a more specific type that inherits from Object. In this case, that type is String
However, since the compiler only knows about Object
you have to turn Option Strict Off if you want to just treat the object like the string you know it really is. That's bad. So bad, that C# doesn't allow support this at all outside of the special dynamic
keyword. Instead, you are much better off choosing specific types all the time.
回答3:
I think you're conflating these two uses of Request
- the first usage may be unrelated to the second.
Note that the function is passing in an object called Request
- if RequestParameters
is something like a Dictionary(Of String, String)
(C# equivalent of Dictionary<string, string>
), then it could very easily return a value for Request("MonitorPath")
, and it wouldn't involve the property at all.
If that doesn't help you, what does the structure of RequestParameters
look like?
来源:https://stackoverflow.com/questions/17859748/properties-in-vb