将VB set / get函数转换为c#函数

时间:2018-02-12 16:00:55

标签: c# vb.net activex

我试图将以下vb set / get函数转换为c#。它通过ActiveX页面使用,如:

Item.CtxString(document.getElementById("setvar").value+"1")=document.getElementById("setval").value;

或:

 Item.CtxString("var1") = "var";

VB.NET中使用以下代码:

Public Property CtxString(ByVal strItemType As String) As String
        Get
            Try
                Return myContext.ContextString(strItemType)
            Catch ex As Exception
                Return ""
            End Try
        End Get
        Set(ByVal value As String)
            Try
                myContext.ContextString(strItemType) = value
            Catch ex As Exception
            End Try
        End Set
    End Property

 Public Sub SetCtxString(ByVal strItemType As String, ByVal value As String)
        Try
            myContext.ContextString(strItemType) = value
        Catch ex As Exception
        End Try
    End Sub

我试图将其从VB转换为C#,具有以下功能:

   public string CtxString
    {
        get
        {
            return ctxString;

        }
        set
        {
            ctxString = value;
        }
    }

public void SetCtxString(string value)
{
    this.ctxString = value;
}

ContextString是c ++中使用的函数,需要将其转换为c#aswell ..

STDMETHODIMP CContextATL::get_ContextString(BSTR strItemType, BSTR *pVal)
{
    try
    {
        _bstr_t strItem(strItemType, true);
        _bstr_t strTemp;
        char szBuffer[2048] = {0};

        CContextItem *pItem = _Module.GetContextItemFromEnvironment(m_strEnv, (char *)strItem);
        if(pItem != NULL)
        {
            strTemp = pItem->GetContextStringValue().c_str();
            *pVal = ::SysAllocString(static_cast<const wchar_t*>(strTemp)); 
            sprintf( szBuffer, "ContextString Key = '%s' Value = '%s' read by Client %s with name = %s in Environment %s\r\n", (char *)strItem, (char *)strTemp, m_strId.c_str(), m_strClientName.c_str(), m_strEnv.c_str());
        }
        else
        {
            sprintf( szBuffer, "ContextString Key = '%s' not found while reading by Client %s with name = %s in Environment %s\r\n", (char *)strItem, m_strId.c_str(), m_strClientName.c_str(), m_strEnv.c_str());
        }
        _Module.WriteDebugString(szBuffer);
    }
    catch(_com_error & e)
    {
        ATLTRACE("CContextATL::get_ContextString exception : %s\n", e.ErrorMessage());
    }

    return S_OK;
}

任何可以帮助我将以下函数从VB.NET转换为c#的人

2 个答案:

答案 0 :(得分:0)

VB属性是“参数化属性” - 这在C#中不可用,因此您可以将其转换为2个单独的方法:

public string get_CtxString(string strItemType)
{
    try
    {
        return myContext.ContextString(strItemType);
    }
    catch (Exception ex)
    {
        return "";
    }
}
public void set_CtxString(string strItemType, string value)
{
    try
    {
        myContext.ContextString(strItemType) = value;
    }
    catch (Exception ex)
    {
    }
}

您原来的'set'方法现在是多余的:

public void SetCtxString(string strItemType, string value)
{
    try
    {
        myContext.ContextString(strItemType) = value;
    }
    catch (Exception ex)
    {
    }
}

答案 1 :(得分:0)

这个怎么样? C#可以超载括号操作符。

我不知道ContextString是什么,但是,如果ContextStringDictionary或类型使用括号来获取值,您可以这样做:

public string this[string strItemType]
{
    get
    {
        try
        {
            return myContext.ContextString[strItemType];
        }
        catch (Exception ex)
        {
            return "";
        }
    }
    set
    {
        try
        {
            myContext.ContextString[strItemType] = value;
        }
        catch (Exception ex) { }
    }
}