简体   繁体   English

覆盖 ToString() 方法

[英]Override ToString() method

I need to redefine the toString () method, to contemplate a specific situation.我需要重新定义 toString() 方法,以考虑具体情况。

When you want to connect to Oracle, you do it with the Oracle.Data.Access .dll, I am having problems with version 12 of the client, when trying to get the value of an output parameter and converting it to string you get the word "null" , on the other hand, with version 10 of the client, when doing the same, "" is obtained.当您想连接到 Oracle 时,您可以使用 Oracle.Data.Access .dll 进行连接,我遇到了客户端版本 12 的问题,当尝试获取输出参数的值并将其转换为字符串时,您会得到单词 "null" ,另一方面,对于版本 10 的客户端,在执行相同操作时,会获得 ""。

Example:例子:

string  _value = "";

OracleConnection cone = new OracleConnection(this.conectionString);
OracleCommand cmd = new OracleCommand("schema.PKG.sp_null_test", cone);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("po_mensaje", OracleDbType.Varchar2,300);
cmd.Parameters["po_mensaje"].Direction = ParameterDirection.Output;

try
{
   cone.Open();
   cmd.ExecuteNonQuery();
   _value = cmd.Parameters["po_mensaje"].Value.ToString();
    //This returns "" if the parameter is not loaded from the database in 
    //version 10 and "null" in 12
}
catch (OracleException ex)
{
   throw ex;
}

I tried this:我试过这个:

public override string ToString()
{
   return base.Equals("null") ? "" : base.ToString();
}

But ToString() still returns the "null" word when the result of oracleParameter.Value is converted to string .但是当oracleParameter.Value的结果转换为string时, ToString()仍然返回"null"字。

Can I make ToString() return "" instead of "null" , when this happens?发生这种情况时,我可以让ToString()返回""而不是"null"吗?

It must be like this一定是这样的

public override string ToString()
{
  return base.Equals(DbNull.Value) ? "" : base.ToString();
}

I don't think you are override the ToString() method from the cmd.Parameters["po_mensaje"].Value .我认为您没有从cmd.Parameters["po_mensaje"].Value覆盖ToString()方法。 When looking at this code I believe you are just overriding the ToString() method from your current class.在查看此代码时,我相信您只是覆盖了当前类中的ToString()方法。

To override such a function you need to extend the Value -class and override the method there.要覆盖这样的函数,您需要扩展Value类并覆盖那里的方法。 I believe this would be too much at this point.我相信在这一点上这太过分了。

The easier one would be to create a new method where you use the cmd.Parameters["po_mensaje"].Value as a parameter.更简单的方法是创建一个新方法,使用cmd.Parameters["po_mensaje"].Value作为参数。

So basically something like this:所以基本上是这样的:

public string GetValueFromParameter(var Value)
{
    if(Value == null)
        return ""; 
    else
        return Value.ToString(); 
}

Then you can call it like this:然后你可以这样称呼它:

try
{
   cone.Open();
   cmd.ExecuteNonQuery();
   _value = GetValueFromParameter(cmd.Parameters["po_mensaje"].Value);
    //This returns "" if the parameter is not loaded from the database in 
    //version 10 and "null" in 12
}
catch (OracleException ex)
{
   throw ex;
}

Couldn't test the function, cause I have no running oracle server.无法测试该功能,因为我没有运行 oracle 服务器。 But you should get the idea.但你应该明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM