使用未分配的变量“格式”,“ attr1”,“ attr2”,“ attr3”

oni3619

我们如何通过其他方法的返回值?我希望得到的值formatattr1attr2attr3GetFormat()方法。但是不知何故我无法得到它。我想念的是什么?但是我确实初始化了它们,但是没有用。

public static bool GetFormat()
{
    string format, attr1, attr2, attr3 = string.Empty;
    try
    {
        string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(globalFormat.ToString());
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//DisplayName");
        format = nodes[0].SelectSingleNode("Format").InnerText.ToString();
        attr1 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
        attr2 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
        attr3 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
        if (string.IsNullOrWhiteSpace(globalFormat) || string.IsNullOrWhiteSpace(attr1) || string.IsNullOrWhiteSpace(attr2))
        {
            return false;
        }
    }
    catch (Exception)
    {
        return false;
    }

    return true;
}
 
public static string GetProfileDisplayName(string profileUID)
{
    string format, attr1, attr2, attr3 = string.Empty;
    if (GetFormat())
    {
        using (var context = GetAccessEntitiesContext())
        {
            var user = context.vw_Profile.Where(x => x.ProfileUID.Equals(profileUID)).FirstOrDefault();

            return string.Format(format, user.GetType().GetProperty(attr1).GetValue(user), user.GetType().GetProperty(attr2).GetValue(user), user.GetType().GetProperty(attr3).GetValue(user));
        }
    }
    else
    {
        format = "{0} {1}";
        attr1 = "FirstName";
        attr2 = "LastName";
        using (var context = GetAccessEntitiesContext())
        {
            var user = context.vw_Profile.Where(x => x.ProfileUID.Equals(profileUID)).FirstOrDefault();

            return string.Format(format, user.GetType().GetProperty(attr1).GetValue(user), user.GetType().GetProperty(attr2).GetValue(user));
        }
    }
}
库纳尔·穆克吉(Kunal Mukherjee)

实现此目的有多种方法,您必须重构您的GetFormat方法。

方法1)制作一个DTO,然后DTO从您的方法中返回它-

public static GetFormatModel GetFormat()
{
    GetFormatModel model = new GetFormatModel();
    try
    {
        string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(globalFormat.ToString());
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//DisplayName");
        model.format = nodes[0].SelectSingleNode("Format").InnerText.ToString();
        model.attr1 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
        model.attr2 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
        model.attr3 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
        
        if (string.IsNullOrWhiteSpace(globalFormat)
        || string.IsNullOrWhiteSpace(model.attr1) 
        || string.IsNullOrWhiteSpace(model.attr2))
        {
            model.isSuccess = false;
            return model;
        }
    }
    catch (Exception)
    {
        model.isSuccess = false;
        return model;
    }

    model.isSuccess = true;
    return model;
}

// DTO
public class GetFormatModel
{
    public bool isSuccess { get; set; }
    public string format { get; set; } = string.Empty;
    public string attr1 { get; set; } = string.Empty;
    public string attr2 { get; set; } = string.Empty;
    public string attr3 { get; set; } = string.Empty;
}

方式2)返回一个 generic Tuple

public static Tuple<bool, string, string, string, string> GetFormat()
{
    Tuple<bool, string, string, string, string> tplGetFormat = default(Tuple<bool, string, string, string, string>);
    try
    {
        string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(globalFormat.ToString());
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//DisplayName");
        tplGetFormat.Item2 = nodes[0].SelectSingleNode("Format").InnerText.ToString();
        tplGetFormat.Item3 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
        tplGetFormat.Item4 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
        tplGetFormat.Item5 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
        
        if (string.IsNullOrWhiteSpace(globalFormat)
        || string.IsNullOrWhiteSpace(tplGetFormat.Item3) 
        || string.IsNullOrWhiteSpace(tplGetFormat.Item4))
        {
            tplGetFormat.Item1 = false;
            return tplGetFormat;
        }
    }
    catch (Exception)
    {
        tplGetFormat.Item1 = false;
        return tplGetFormat;
    }

    tplGetFormat.Item1 = true;
    return tplGetFormat;
}

方式3)返回out参数-

public static bool GetFormat(
out string format, 
out string attr1, 
out string attr2, 
out string attr3
)
{
    format = attr1 = attr2 = attr3 = string.Empty;
    try
    {
        string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(globalFormat.ToString());
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//DisplayName");
        format = nodes[0].SelectSingleNode("Format").InnerText.ToString();
        attr1 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
        attr2 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
        attr3 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
        if (string.IsNullOrWhiteSpace(globalFormat) || string.IsNullOrWhiteSpace(attr1) || string.IsNullOrWhiteSpace(attr2))
        {
            return false;
        }
    }
    catch (Exception)
    {
        return false;
    }

    return true;
}

// Usage
string format = attr1 = attr2 = attr3 = string.Empty;
GetFormat(out format, out attr1, out attr2, out attr3);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SQL选择按attr1和attr3的不同条件按attr1分组的数据

来自分类Dev

在2个函数中使用dataframe attr

来自分类Dev

使用attr()更新CSS变量

来自分类Dev

如何使用attr()jQuery

来自分类Dev

使用CSS3 attr()进行旋转变换

来自分类Dev

使用CSS3 attr()进行旋转变换

来自分类Dev

在变量中使用 attr() 添加类?

来自分类Dev

Symfony2表单的attr属性使用翻译器

来自分类Dev

Symfony2表格的attr属性使用翻译器

来自分类Dev

attr_accessor与attr_reader和实例变量

来自分类Dev

jQuery .attr();

来自分类Dev

使用jQuery attr()设置“ css”

来自分类Dev

无法使用 attr() 更改“类”

来自分类Dev

Vue JS v-attr="expression1 && exp2 && exp3" 不起作用

来自分类Dev

类变量上的attr_accessor

来自分类Dev

定义jquery attr变量的正确方法?

来自分类Dev

取 $elements 变量并缩小 attr 等于 x

来自分类Dev

我如何使用attr()从变量将值插入textarea

来自分类Dev

我如何使用attr()从变量将值插入textarea

来自分类Dev

使用.attr()后无法使用.find()标记

来自分类Dev

使用cheerio,无法使用.attr 获取属性

来自分类Dev

将Angular 2变量绑定到CSS伪类内容:attr()表达式

来自分类Dev

jQuery使用prop或attr禁用链接元素

来自分类Dev

在新插件中使用attr()函数?

来自分类Dev

如何使用attr_encrypted的灯具

来自分类Dev

使用$ .data(“ value”)与$ .attr(“ data-value”)

来自分类Dev

使用没有attr_accessible的枚举

来自分类Dev

如何使用jqlite抓取元素的attr数据?

来自分类Dev

在伪元素的attr()中使用元素的内容