在另一个按钮中使用局部变量

发布于 2024-09-24 05:00:07 字数 700 浏览 6 评论 0原文

出现错误:“正在设置的类型与标记的值表示形式不兼容。”

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玉环 2024-10-01 05:00:07

我同意 Frederik 的观点,本地 fi 隐藏了类级别成员。但尚不清楚您期望按钮单击处理程序中的该变量包含什么内容。

因为您正在循环,所以如果您使用类成员 fi,您将只能引用最后一个文件。这可能没有道理。如果您正在循环中搜索匹配项,并在该匹配项上停止,那么取消隐藏类级 fi 就有意义了,并且您拥有的代码将起作用。您具体想用 fi 做什么?

另外,您的 for 循环不会按列出的方式工作......应该是:

//  Missing the 'in'
foreach (string fi in files_in_folder)

* 更新 *
针对您对问题的更改,您在哪里收到此错误?在按钮点击事件中?在哪条线上?这听起来像是 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-level fi would make sense, and the code you have will work. What specifically are you trying to do with fi?

Also, the for loop you have won't work as listed... should be:

//  Missing the 'in'
foreach (string fi in files_in_folder)

* 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.

梦巷 2024-10-01 05:00:07

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.

蓝礼 2024-10-01 05:00:07

fi 不会是本地的,对于该类来说它将是全局的。

你所拥有的将会起作用。

fi won't be local, it's going to be global for that class.

What you have there will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文