类和结构之间的交互

本文关键字:交互 之间 结构 | 更新日期: 2023-09-27 18:12:59

好的,所以我的项目中的一个类会使用这个方法。

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

这将不起作用,因为当我将public NameSplitter更改为public 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;
    }

类和结构之间的交互

你的代码中有几个问题,很明显它不会编译。

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

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

2)返回类型和返回语句必须一致。这里你返回一个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应该是一个结构体,类也是一个很好的选择。

尝试实现这样的类:

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

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

//现有代码

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

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?
    }

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

要回答您的问题,请参阅重新制作的示例

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