2011-11-02 67 views
0

好的,所以我的項目中的一個類通過這種方法。類和結構之間的相互作用

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

成員名稱是一個結構:

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公衆成員名稱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; 
    } 
+2

這段代碼編譯過? – sll

+2

你似乎試圖從我認爲是'NameSplitter'構造函數'返回'的東西 - 你不能這樣做。請提供完整的「NameSplitter」代碼。 –

+0

您的NameSplitter的構造函數不能返回除NameSplitter類之外的任何內容,它不能返回MemberName類。也許你想要一個簡單的構造函數,然後一個方法來'拆分'字符串並返回MemberName對象 – tonycoupland

回答

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)在構造函數中不能有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應該是一個結構,一個類將是一個粘性也是選擇。

+0

我以爲這麼多,所以如何讓這個類返回一個MemberName? – ediblecode

+0

一個類不能**返回**任何東西。你可以有一個靜態方法,或者將結果存儲在另一個對象讀取的屬性中。沒有別的 –

+0

因此,如果我在NameSplitter類(titles,firstNames和LastNames)中創建三個屬性,那麼我可以從TdsTransformer訪問這些屬性並從中創建MemberName? – ediblecode

1

你應該使該方法返回一個成員名稱的靜態方法和結果分配給成員名稱變量,而不是NameSplitter(在你的SplitTdsName法)

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

您的觀點爲空白。 –

0

我的判斷是,你必須回到你的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; 
    } 
} 
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 
    } 
}