在另一个按钮中使用局部变量
出现错误:“正在设置的类型与标记的值表示形式不兼容。”
string fi = null;
public void reading(object sender, EventArgs e)
{
read_from_folder = folderBrowserDialog1.ShowDialog();
if (read_from_folder == DialogResult.OK)
{
files_in_folder = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
foreach (string fi files_in_folder)
{
string fi_nam = filese_in_folder.ToString();
...
}
}
}
private void button1_Click(object sender, EventArgs e)
{
DicomDirectory cop = new DicomDirectory(fi);
cop.Load(fi);
}
There's an error: "The type being set is not compatible with the value representation of the tag."
string fi = null;
public void reading(object sender, EventArgs e)
{
read_from_folder = folderBrowserDialog1.ShowDialog();
if (read_from_folder == DialogResult.OK)
{
files_in_folder = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
foreach (string fi files_in_folder)
{
string fi_nam = filese_in_folder.ToString();
...
}
}
}
private void button1_Click(object sender, EventArgs e)
{
DicomDirectory cop = new DicomDirectory(fi);
cop.Load(fi);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我同意 Frederik 的观点,本地
fi
隐藏了类级别成员。但尚不清楚您期望按钮单击处理程序中的该变量包含什么内容。因为您正在循环,所以如果您使用类成员
fi
,您将只能引用最后一个文件。这可能没有道理。如果您正在循环中搜索匹配项,并在该匹配项上停止,那么取消隐藏类级fi
就有意义了,并且您拥有的代码将起作用。您具体想用fi
做什么?另外,您的 for 循环不会按列出的方式工作......应该是:
* 更新 *
针对您对问题的更改,您在哪里收到此错误?在按钮点击事件中?在哪条线上?这听起来像是 DicomDirectory 对象的自定义内部错误,无论是什么。
I agree with Frederik, the local
fi
hides the class-level member. But it isn't clear what you expect to be in that variable in the button click handler.Because you're looping, if you use the class member
fi
, you'll only have the last file referenced. This probably doesn't make sense. If you were searching for a match, say, in the loop, and stopping on that match, then un-hiding the class-levelfi
would make sense, and the code you have will work. What specifically are you trying to do withfi
?Also, the for loop you have won't work as listed... should be:
* Update *
In response to your changes in the question, where are you getting this error? In the button click event? On which line? It sounds like a custom internal error to the DicomDirectory object, whatever that is.
fi 是在所示函数之外声明的,因此它应该具有类作用域,而不是一个函数中的局部作用域。这意味着您应该能够在两个函数中使用它。这假设您显示的两个函数位于同一类中(例如“Form1”)。
您可能还想发布这些函数所包含的类声明,这将验证它们是否位于同一个类中。
fi is declared outside of the functions shown so it should have class scope and not local scope in one function. This means you should be able to use it in both of your functions function. This assumes that the two functions you showed are in the same class ("Form1" for example).
You may want to post the class declaration that these functions are contained in also, which will verify that they are in the same class.
fi 不会是本地的,对于该类来说它将是全局的。
你所拥有的将会起作用。
fi won't be local, it's going to be global for that class.
What you have there will work.