2013-06-13 54 views
1

我想從另一個cs文件調用方法。我創建了該方法存在於類的新實例,但得到這個錯誤:類型''沒有構造函數定義C#

The type 'SteamKit2.SteamFriends' has no constructors defined

誰能幫助我瞭解爲什麼發生這種情況?

,其中我試圖從被如下

using System; 
using SteamKit2; 
using SteamTrade; 
using System.Collections.Generic; 
using SteamBot; 

namespace SteamBot 
{ 
public class HarvesterHandler : UserHandler 
{ 
    public int FriendCount = 0; 
    public int FriendToTrade = 0; 
    SteamFriends me = new SteamFriends(); 

    public override void OnLoginCompleted() 
    { 
     FriendCount = me.GetFriendCount(); 
     if (FriendToTrade < FriendCount) 
     { 
      Bot.SteamTrade.Trade(me.GetFriendByIndex(FriendToTrade)); 
      Log.Info("Sending Trade Request to Friend " + FriendToTrade + " of " + FriendCount); 
      return; 
     } 
     while (true) 
     { 
      Log.Warn("Finished trading"); 
     } 
    } 
} 
} 

的稱爲類如下(在一個單獨的CS文件)

using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using SteamKit2.Internal; 

namespace SteamKit2 
{ 
public sealed partial class SteamFriends : ClientMsgHandler 
{ 
    object listLock = new object(); 
    List<SteamID> friendList; 
    List<SteamID> clanList; 

    AccountCache cache; 


    internal SteamFriends() 
    { 
     friendList = new List<SteamID>(); 
     clanList = new List<SteamID>(); 

     cache = new AccountCache(); 
    } 
    public int GetFriendCount() 
    { 
     lock (listLock) 
     { 
      return friendList.Count; 
     } 
    } 
    public SteamID GetFriendByIndex(int index) 
    { 
     lock (listLock) 
     { 
      if (index < 0 || index >= friendList.Count) 
       return 0; 

      return friendList[ index ]; 
     } 
    } 
} 
} 
+2

儘量使構造'public',而不是'internal' – Nolonar

回答

1

構造是調用該方法的類定義爲內部。因此它只能從同一個組件中訪問。可能這個構造函數只是打算被工廠調用? SteamFriends是否和HarvesterHandler一樣在同一個項目中?

+0

鑑於命名空間的名稱,我敢打賭這是事實。 – Gorpik

+0

哦,等等,不,他們不在同一個項目中。同樣的解決方案,不一樣的項目 – AustinGeorge

+0

好吧,這是問題。內部只在同一個程序集中可見 - >同一個項目 –

-1

這是因爲參構造函數的可訪問性設置爲內部變化:就像您使用SteamBot,正確

internal SteamFriends() 

public SteamFriends() 
+1

如果我改變了,錯誤依然存在。 – AustinGeorge

+0

很難相信 – Thomas

+4

可疑的答案是,「SteamFriends」的構造函數可能被定義爲「internal」。 – Stefan

1

它看起來?

我不知道此行的目的:在您的個人需要處理程序

SteamFriends me = new SteamFriends(); 

一切都在Bot實例可用。對於Bot.SteamFriendsSteamFriends相關信息看(見SimpleUserHandler的一些例子)

關係到你的問題,我可以爲你提供一個選擇,因爲我不知道爲什麼GetFriendByIndex也不是沒有花更多的時間排除故障的正常工作項目。

你可以通過BOT(其中有一個最大的250個可能的朋友,如果我是閥門系統記憶極限正確)的整個朋友列表循環做這樣的事情:

foreach (Bot.SteamFriends.FriendsListCallback.Friend friend in callback.FriendList) 
{ 
    // Check the friend ID (Steam Profile ID - 64bit number) against what who you want to trade 
    if (friend.SteamID == "VALUE YOU ARE EXPECTING") 
    { 
     Bot.SteamTrade.Trade(friend); 
    } 
} 
0

使SteamFriends類中的所有方法都是靜態的,然後使用classname從類名訪問。 當然你會得到問題的解決方案。 喜歡 - :
公共靜態 INT GetFriendCount() { 鎖(listLock) { 返回friendList.Count; } } 公共靜態 SteamID GetFriendByIndex(INT指數) { 鎖(listLock) { 如果(索引< 0 ||指數> = friendList.Count) 返回0;

 return friendList[ index ]; 
    } 
} 

和從HarvesterHandler cs文件訪問。 不作爲班級的對象。
FriendCount = SteamFriends.GetFriendCount(!); ..

乾杯..

相關問題