如果在c#中未正确检查其他条件?
我遇到了一个问题,我正在检查一些元素键值是否有效,以便继续二维码扫描。例如:如果“我的活动”属性为true,而“完成”属性为false,并且计划时间为当前时间+-30分钟的开始窗口,则继续扫描。如果没有,让我给他们看一个错误。我尝试用一个简单的if实现检查部分,但只检查活动的和完成的键值。有谁能帮我解决这个问题吗。谢谢你的帮助 这是我的密码:如果在c#中未正确检查其他条件?,c#,C#,我遇到了一个问题,我正在检查一些元素键值是否有效,以便继续二维码扫描。例如:如果“我的活动”属性为true,而“完成”属性为false,并且计划时间为当前时间+-30分钟的开始窗口,则继续扫描。如果没有,让我给他们看一个错误。我尝试用一个简单的if实现检查部分,但只检查活动的和完成的键值。有谁能帮我解决这个问题吗。谢谢你的帮助 这是我的密码: public void ScanQrCode() { BarcodeScanner.Scan(async (bar
public void ScanQrCode()
{
BarcodeScanner.Scan(async (barCodeType, barCodeValue) =>
{
BarcodeScanner.Stop();
var results = JsonConvert.DeserializeObject<dynamic>(barCodeValue);
var gettingTheName = (string) results.Evaluation.Value;
TextHeader.text = gettingTheName;
var qrCodeString = $"***************.firebaseio.com/Evaluations/.json?orderBy=\"$key\"&startAt=\"{gettingTheName}\"&limitToFirst=1";
Debug.Log(barCodeValue);
var qrCodeObj = JsonConvert.DeserializeObject<dynamic>(qrCodeString);
try
{
bool parseSuccessful = DateTime.TryParse("ScheduleStartTime", out var scheduledStartTime);
if (results.Contains("Active").Equals(true) &&
results.Contains("Completed").Equals(false) &&
DateTime.Now < scheduledStartTime.AddMinutes(30) &&
DateTime.Now > scheduledStartTime.AddMinutes(-30)) {
var matchingLink = new WebClient().DownloadString(qrCodeString);
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(matchingLink);
var candidateId = obj.First.First["CandiateID"].ToString();
string page = $"https://***********.firebaseio.com/Candidates/.json?orderBy=\"$key\"&startAt=\"{candidateId}\"&limitToFirst=1";
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(page))
using (HttpContent content = response.Content)
{
// Reading the string.
Dictionary<string, Candidates> evaluationDictionary = new Dictionary<string, Candidates>();
string result = await content.ReadAsStringAsync();
evaluationDictionary = JsonConvert.DeserializeObject<Dictionary<string, Candidates>>(result);
Debug.Log(evaluationDictionary);
foreach (Candidates candidates in evaluationDictionary.Values)
{
string evaluationMessage = candidates.FirstName + " " + candidates.LastName;
candidateMessage = GetComponent<Text>();
candidateMessage.text = evaluationMessage;
}
// Getting a reference to the text component.
candidateMessage = GetComponent<Text>();
candidateMessage.text = matchingLink.ToString();
candidateMessage.text = matchingLink.Trim(new char[] {'"'});
}
}
else
{
EditorUtility.DisplayDialog("Incorrect credentials", "Please scan a valid QR code", "OK");
}
}
catch (Exception e)
{
Console.WriteLine(e);
SceneManager.LoadScene("Verify");
throw;
}
});
}
}
嗯。我也被困在那一部分。我试着通过添加 &&qrCodeString.Contains(“ScheduleStartTime”).Equals(我被卡住了 这里) 我将通过将日期字符串解析为DateTime对象来完成这一部分,然后将当前时间与在两个方向上移动的新DateTime进行比较 我可能会这样做:
try {
bool parseSuccessful = DateTime.TryParse( ScheduledStartTimeFromJSON, out var scheduledStartTime );
if ( qrCodeString.Contains( "Active" ).Equals( true ) &&
qrCodeString.Contains( "CompletedMessage" ).Equals( false ) &&
DateTime.Now < scheduledStartTime.AddMinutes(30) &&
DateTime.Now > scheduledStartTime.AddMinutes( -30 ) ) {
var matchingLink = new WebClient().DownloadString( qrCodeString );
...
}
}
试试看{
bool parseSuccessful=DateTime.TryParse(ScheduledStartTimeFromJSON,out变量scheduledStartTime);
if(qrCodeString.Contains(“Active”).Equals(true)&&
qrCodeString.Contains(“CompletedMessage”).Equals(false)&&
DateTime.NowscheduledStartTime.AddMinutes(-30)){
var matchingLink=new WebClient().DownloadString(qrCodeString);
...
}
}
在将日期与任何内容进行比较之前,还应检查以确保已成功解析日期 根据您的JSON示例:
- 首先,我认为您应该检查Completed值,而不是CompletedMessage
- 使用此代码搜索
您只是检查字符串中是否包含Active。我认为你应该寻找它的价值qrCodeString.Contains(“Active”).Equals(true)
var qrCodeObj = JsonConvert.DeserializeObject<dynamic>(qrCodeString);
var qrCodeObj=JsonConvert.DeserializeObject(qrCodeString);
然后检查相应的值
if (qrCodeObj["Active"] == true && qrCodeObj["Completed"] == false)
{
var matchingLink = new WebClient().DownloadString(qrCodeString);
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject (matchingLink);
var candidateId = obj.First.First["CandiateID"].ToString();
string page = $"https://*********.firebaseio.com/Candidates/.json?orderBy=\"$key\"&startAt=\"{candidateId}\"&limitToFirst=1";
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(page))
using (HttpContent content = response.Content)
{
// Reading the string.
Dictionary<string, Candidates> evaluationDictionary = new Dictionary<string, Candidates>();
string result = await content.ReadAsStringAsync();
evaluationDictionary = JsonConvert.DeserializeObject<Dictionary<string, Candidates>>(result);
Debug.Log(evaluationDictionary);
foreach (Candidates candidates in evaluationDictionary.Values)
{
string evaluationMessage = candidates.FirstName + " " + candidates.LastName;
candidateMessage = GetComponent<Text>();
candidateMessage.text = evaluationMessage;
}
// Getting a reference to the text component.
candidateMessage = GetComponent<Text>();
candidateMessage.text = matchingLink.ToString();
candidateMessage.text = matchingLink.Trim(new char[] {'"'});
}
}
else
{
EditorUtility.DisplayDialog("Incorrect Credentials ",
"Please scan a valid QR code. " , "OK");
}
if(qrCodeObj[“活动”]==true&&qrCodeObj[“完成”]==false)
{
var matchingLink=new WebClient().DownloadString(qrCodeString);
var obj=Newtonsoft.Json.JsonConvert.DeserializeObjectI在计划时间内看不到任何验证。是的。我也有点困在这一部分。我尝试通过添加&&qrCodeString.Contains(“ScheduleStartTime”).Equals(*我困在这里*)
另外,我的其余字段的验证是否足够好?但是,如果让我们说如果活动字段为false,而完成字段为true,我不想将它们带到验证窗口scene@Crowcoder我更新了我的代码。现在,即使满足键值,它也只会触发else cause
。请避免,我的else if validation正确吗,还有我的catch子句?我首先这样做了bool parseSuccessful=DateTime.TryParse(“ScheduleStartTime”,out var scheduledStartTime);
但是,我扫描了我的二维码,但它不会触发else if或catch cause?ScheduledStartTimeFromJSON应该是用于访问日期字符串的变量或函数。否则,您的验证看起来是正确的。在catch块中,我建议将e.StackTrace写入控制台或在块中的任何位置添加断点帮助您调试。我不知道SceneManager是什么,我认为您重新抛出的原因是因为您有其他更高级别的错误处理。SceneManager是我加载下一个场景的地方。另外,如何使更高级别的错误处理正常工作?如果在数据库中,我将active更改为false,并将completed更改为true显示我的else原因。您应该在if语句之前设置一个断点,并检查正在比较的变量,以验证它们是否符合预期。只有在其各自的try块中发生错误时,才会执行catch块。应用程序中有数百个教程可处理异常。TutorialsPoint是一个可以启动的位置。Com这段对话不适用于长时间的讨论;这段对话已经结束。
if (qrCodeObj["Active"] == true && qrCodeObj["Completed"] == false)
{
var matchingLink = new WebClient().DownloadString(qrCodeString);
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject (matchingLink);
var candidateId = obj.First.First["CandiateID"].ToString();
string page = $"https://*********.firebaseio.com/Candidates/.json?orderBy=\"$key\"&startAt=\"{candidateId}\"&limitToFirst=1";
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(page))
using (HttpContent content = response.Content)
{
// Reading the string.
Dictionary<string, Candidates> evaluationDictionary = new Dictionary<string, Candidates>();
string result = await content.ReadAsStringAsync();
evaluationDictionary = JsonConvert.DeserializeObject<Dictionary<string, Candidates>>(result);
Debug.Log(evaluationDictionary);
foreach (Candidates candidates in evaluationDictionary.Values)
{
string evaluationMessage = candidates.FirstName + " " + candidates.LastName;
candidateMessage = GetComponent<Text>();
candidateMessage.text = evaluationMessage;
}
// Getting a reference to the text component.
candidateMessage = GetComponent<Text>();
candidateMessage.text = matchingLink.ToString();
candidateMessage.text = matchingLink.Trim(new char[] {'"'});
}
}
else
{
EditorUtility.DisplayDialog("Incorrect Credentials ",
"Please scan a valid QR code. " , "OK");
}