我想知道是否有一种方法可以在应用程序运行时更改Limit()变量? 这是我的代码:
void Start()
{
m_ButtonRun.onClick.AddListener(TaskOnClick);
}
public void TaskOnClick() //
{
string connectionString = "myconnection";
var client = new MongoClient(connectionString);
var database = client.GetDatabase("mydb");
//var database = server.GetDatabase("WIVEmydbData");
var collection = database.GetCollection<BsonDocument>("mycollection");
var sort = Builders<BsonDocument>.Sort.Descending("Time");
var document = collection.Find(new BsonDocument()).Sort(sort).Limit(limit: 1).ForEachAsync(d => Console.WriteLine(d));
Console.WriteLine(document.ToString());
//streamwriter writes the Console.WriteLine to multi-import.txt
FileStream filestream = new FileStream("import.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
var writeFile = new StreamWriter(filestream);
{
writeFile.AutoFlush = true;
Console.SetOut(writeFile);
writeFile.Write(document.ToString());
}
}
是否可以分配公共按钮以某种方式将限制增加1? 我的想法是要有3个按钮:“增加限制”,“执行数据库搜索”,“减少限制”。执行部分运行完美,只是想知道是否有可能即时定制Limit()。
编辑: 添加了完整的代码...哦,男孩 我想到的是在Limit()的()中放置一个类似X的东西,将X设置为默认值1,然后有两个按钮可以从X值中减去1。
答案 0 :(得分:1)
如评论中所述,我很确定您可以做类似的事情
[SerializeField] Button increaseButton;
[SerializeField] Button decreaseButton;
private void Start()
{
m_ButtonRun.onClick.AddListener(TaskOnClick);
increaseButton.onClick.AddListener(IncreaseLimit);
decreaseButton.onClick.AddListener(DecreaseLimit);
}
private int limit = 1;
public void IncreaseLimit()
{
limit++;
}
public void DecreaseLimit()
{
limit--;
limit = Mathf.Max(limit, 1);
}
public void TaskOnClick() //
{
string connectionString = "myconnection";
var client = new MongoClient(connectionString);
var database = client.GetDatabase("mydb");
//var database = server.GetDatabase("WIVEmydbData");
var collection = database.GetCollection<BsonDocument>("mycollection");
var sort = Builders<BsonDocument>.Sort.Descending("Time");
var document = collection.Find(new BsonDocument()).Sort(sort).Limit(limit).ForEachAsync(d => Console.WriteLine(d));
Console.WriteLine(document.ToString());
//streamwriter writes the Console.WriteLine to multi-import.txt
FileStream filestream = new FileStream("import.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
var writeFile = new StreamWriter(filestream);
{
writeFile.AutoFlush = true;
Console.SetOut(writeFile);
writeFile.Write(document.ToString());
}
}