类和结构之间的交互

时间:2011-11-02 09:31:16

标签: c# .net struct class-design

好的,所以我的项目中的一个类经历了这个方法。

public static MemberName SplitTdsName(string tdsName)
    {
        NameSplitter preSplitName = new NameSplitter(tdsName);
        return preSplitName;
    }

MemberName是一个结构:

public struct MemberName
{
    public string Title;
    public string FirstNames;
    public string LastNames;

    public MemberName(string title, string firstNames, string lastNames)            
    {            
        Title = title;
        FirstNames = firstNames;
        LastNames = lastNames;
    }
}

和NameSplitter类:

public NameSplitter(string fullName)
    {
        nameInFull = fullName;
        SetAllowedTitles();
        SplitNamesAndRemovePeriods();
        SetTitles();
        MemberName splitName = new MemberName(titles, firstNames, lastNames);
        return splitName;
    }

这不起作用,因为当我将公共NameSplitter更改为公共MemberName NameSplitter时,SplitTdsName方法告诉我NameSplitter没有一个接受一个参数的方法。

我不知道如何解决这个问题,或者让它发挥作用。我是否需要将NameSplitter更改为NameSplitter静态类?

注意:我知道代码错误

跟着:

public static MemberName SplitTdsName(string tdsName)
    {
        return NameSplitter.NameSplitter(tdsName);
    }

public static MemberName NameSplitter(string fullName)
    {
        nameInFull = fullName;
        SetAllowedTitles();
        SplitNamesAndRemovePeriods();
        SetTitles();
        MemberName splitName = new MemberName(titles, firstNames, lastNames);
        return splitName;
    }

    public MemberName(string title, string firstNames, string lastNames)            
    {            
        Title = title;
        FirstNames = firstNames;
        LastNames = lastNames;
    }

6 个答案:

答案 0 :(得分:3)

您的代码中存在几个问题,很明显它无法编译。

1)您不能在构造函数中拥有return

public NameSplitter(string fullName)
{
     ...
     return splitName;
}

2)return类型和return语句必须一致。在这里,您将返回NameSplitter,但函数承诺MemberName

public static MemberName SplitTdsName(string tdsName)
{
   NameSplitter preSplitName = new NameSplitter(tdsName);
   return preSplitName;
}

快速修复(可能不完整,未经测试)

// class NameSplitter
public NameSplitter(string fullName)
    {
        nameInFull = fullName;
        SetAllowedTitles();
        SplitNamesAndRemovePeriods();
        SetTitles();
        //MemberName splitName = new MemberName(titles, firstNames, lastNames);
        //return splitName;
    }

public MemberName MemberName   // readonly property
{
    get { return new MemberName(titles, firstNames, lastNames); }
}

然后方法变为

public static MemberName SplitTdsName(string tdsName)
{
    NameSplitter preSplitName = new NameSplitter(tdsName);
    return preSplitName.MemberName ;
}

但我不相信MemberName应该是一个结构,一个类也是一个不错的选择。

答案 1 :(得分:1)

您应该使该方法成为一个返回MemberName的静态方法,并将结果分配给MemberName变量,而不是NameSplitter(在SplitTdsName方法中)

答案 2 :(得分:1)

尝试实现类:

    public static class NameSplitter
    {
        public static MemberName Split(string fullName)
        {
            ...
            MemberName splitName = new MemberName(titles, firstNames, lastNames);
            return splitName;
        }
    }

然后使用MemberName mn = NameSplitter.Split("<your data here>");

最终代码如下:

    public static class YourClass
    {
        ...
        public static MemberName SplitTdsName(string tdsName)
        {
            MemberName preSplitName = NameSplitter.Split(tdsName);
            return preSplitName;
        }
    }

    public static class NameSplitter
    {
        ...
        public static MemberName Split(string fullName)
        {
            nameInFull = fullName;
            SetAllowedTitles();
            SplitNamesAndRemovePeriods();
            SetTitles();
            MemberName splitName = new MemberName(titles, firstNames, lastNames);
            return splitName;
        }
    }

    public struct MemberName
    {
        public string Title;
        public string FirstNames;
        public string LastNames;

        public MemberName(string title, string firstNames, string lastNames)
        {
            Title = title;
            FirstNames = firstNames;
            LastNames = lastNames;
        }
    }

答案 3 :(得分:1)

//现有代码

public struct MemberName
{
    public string Title;
    public string FirstNames;
    public string LastNames;

    public MemberName(string title, string firstNames, string lastNames)            
    {            
        Title = title;
        FirstNames = firstNames;
        LastNames = lastNames;
    }
}

//下面的修改代码

public class NameSplitter
{

    // Added a property for returning splitName value thru' class
    private MemberName splitName;
    public MemberName SplitName
    {
        get { return splitName; }
        private set { splitName = value; }
    }

    public NameSplitter(string fullName)
    {
        nameInFull = fullName;
        SetAllowedTitles();
        SplitNamesAndRemovePeriods();
        SetTitles();

        splitName = new MemberName(titles, firstNames, lastNames);

       // return splitName; // Can't be done in a constructor and has to be returned via public property
    }
}

答案 4 :(得分:0)

1)

public static MemberName SplitTdsName(string tdsName)
    {
        NameSplitter preSplitName = new NameSplitter(tdsName);
        return preSplitName; // I don't understand why you're returning NameSplitter
                             // when method's return type is MemberName.
    }

2)

public NameSplitter(string fullName)
    {
        nameInFull = fullName;
        SetAllowedTitles();
        SplitNamesAndRemovePeriods();
        SetTitles();
        MemberName splitName = new MemberName(titles, firstNames, lastNames);
        return splitName; // How would a constructor return something?
    }

答案 5 :(得分:0)

我的判断是,你必须回到你的c#bible并回顾你想要实现的目标和设计。

要回答您的问题,请参阅重新设计的样本

class Program
{
    static void Main(string[] args)
    {
    }
    public static MemberName SplitTdsName(string tdsName)
    {
        NameSplitter preSplitName = new NameSplitter(tdsName);
        return preSplitName.GetMemberName();
    } 
}
public struct MemberName
{
    public string Title;
    public string FirstNames;
    public string LastNames;

    public MemberName(string title, string firstNames, string lastNames)
    {
        Title = title;
        FirstNames = firstNames;
        LastNames = lastNames;
    }
}
public class NameSplitter
{
    MemberName _memberName;
    public NameSplitter(string fullName)
    {
        _memberName = new MemberName("title", "firstName", "lastName");
    }

    public MemberName GetMemberName()
    {
        return _memberName;
    }
}