VB 中的属性 [英] Properties in VB

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

问题描述

下面是写在 VB .cls 文件中的代码片段:

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

<小时>

我正在尝试将此代码迁移到 C#,但我不明白 Request("MonitorPath") 如何返回一个字符串.


I'm trying to migrate this code to C#, and I don't understand how Request("MonitorPath") is returning a string.

如果是 - 如何,因为 Let 没有任何返回类型?
如果没有 - sErrorLog = Request("MonitorPath") &\Log\Debug\Errors" 工作吗?

If yes - how, as Let does not have any return type?
If no - how does sErrorLog = Request("MonitorPath") & "\Log\Debug\Errors" work?

推荐答案

与旧的 vb6 代码等效的 C# 如下所示:

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);
   }
}

与该函数等效的 C# 是这样的:

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
}

对于问题的类型部分,如果您没有为项目指定返回类型,则返回类型为 Object.但这只是编译器类型.实际的对象引用将具有从 Object 继承的更具体的类型.在这种情况下,该类型是 String 但是,由于编译器只知道 Object,如果您想像对待字符串一样对待对象,则必须关闭 Option Strict.知道它真的是.那很糟.太糟糕了,除了特殊的 dynamic 关键字之外,C# 根本不允许支持它.相反,您最好始终选择特定类型.

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.

这篇关于VB 中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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