【发布时间】:2020-08-19 13:09:02
【问题描述】:
所以我遇到的问题是我有一个循环,循环中有一个 switch 语句。我在循环中声明了变量,但在 switch 语句之外。如何将其导入 switch 语句?
例子:
for (int i = 1; i < 3; i++)
{
var detectionid = detectionResult.Sequence.Items[i];
ulong trackId = detectionid.TrackId;
switch (i)
{
case 1:
Console.WriteLine(trackId);
break;
case 2:
Console.WriteLine(trackId);
break;
default:
Console.WriteLine("Default case");
break;
}
}
所以trackId 是我想使用的变量。但是由于某种原因它不起作用,它一直说我必须声明这个变量。
PS:我是 C# 的初学者,所以我的知识很少。
编辑: 这是我的完整源代码
private void OnDetect(CallbackEventArgument callbackArgument)
{
if (cameraTemp > 35)
{
var detectionResult = callbackArgument.GetDetectionResult();
// var firstDetection = detectionResult.Sequence.Items[0];
foreach (var detection in detectionResult.Sequence.Items)
{
var message = string.Empty;
ImageArgument fullSizeImage = null;
switch (detectionResult.Type)
{
case T3DDetectionType.OBSERVATION:
message = "Track ID: " + detection.TrackId;
fullSizeImage = detectionResult.RGBImage;
break;
case T3DDetectionType.DEPTH_LIVENESS:
case T3DDetectionType.THERMAL_LIVENESS:
message = "Track ID: " + detection.TrackId + " Score: " + (detection as Liveness).Score.ToString("N0");
score = (detection as Liveness).Score.ToString("N0");
fullSizeImage = detectionResult.FullImage;
break;
case T3DDetectionType.TEMPERATURE:
message = "Temperature: " + (detection as Temperature).MeasurementValueCelsius.ToString("N1") + "°C";
temperature = (detection as Temperature).MeasurementValueCelsius.ToString("N1") + "°C";
//float temperature = (detection as Temperature).MeasurementValueCelsius;
fullSizeImage = detectionResult.FullImage;
break;
}
for (int i = 0; i < detectionResult.Sequence.Items.Count(); i++)
{
var detectionid = detectionResult.Sequence.Items[i];
double depth = detectionid.Depth/1000.00 ;
ulong trackId = detectionid.TrackId;
switch (i)
{
case 0:
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
/* https://www.newtonsoft.com/json/help/html/WriteJsonWithJsonTextWriter.htm */
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.WritePropertyName(trackId.ToString());
writer.WriteStartArray();
writer.WriteStartObject();
writer.WritePropertyName("Temperature");
writer.WriteValue(temperature);
writer.WritePropertyName("Depth");
writer.WriteValue(depth);
writer.WritePropertyName("TrackId");
writer.WriteValue(trackId);
writer.WriteEndObject();
writer.WriteEnd();
}
System.IO.File.AppendAllText(@"C:/Users/Kimeru/Documents/Dermalog Noah WPF/data" + trackId + ".json", sb.ToString());
break;
case 1:
break;
case 2:
break;
case 3:
Console.WriteLine("Case 2");
break;
case 4:
Console.WriteLine("Case 2");
break;
case 5:
Console.WriteLine("Case 2");
break;
case 6:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}
}
ClearDetectCanvas(detectionResult.Type);
UpdateDetectImages(fullSizeImage, detection.Portrait);
UpdateDetectCanvas(fullSizeImage, detection);
UpdateLabel(detectionResult.Type, message);
var trackIDs = callbackArgument.GetTrackIDs().ToList();
if (trackIDs.Count > 0)
{
UpdateTrackStatus(callbackArgument.GetDetectionItemsForTrack(trackIDs.First()));
}
}
}
}
【问题讨论】:
-
用
var代替double ulong。 -
我的代码粘贴错误,我改了!
-
现在您已经修复了类型,代码似乎没有任何问题。如果你只做
ulong trackId = 5;,你能重现这个问题吗?你也可以包括你得到的确切错误。 -
还是不行,它说“在声明之前不能使用局部变量'trackId'”
-
您似乎省略了一些重现问题所需的代码。如果您从
ulong trackId = detectionid.TrackId;行向后搜索,您会发现trackId的另一个匹配项吗?
标签: c# variables switch-statement