嗨,这个简单的代码不起作用? [英] Hi, this simple code doesn't work?

查看:44
本文介绍了嗨,这个简单的代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 {
if(textBox1.Text == Clipboard.GetText())
MessageBox.Show(yes);
}





因此,如果文本框与剪贴板文本具有相同的文本,我想要一个消息框说是 。似乎没有工作。为什么? :)



我的尝试:



< I>的

解决方案

我们无法分辨:它将取决于剪贴板上的内容 - 我们看不到 - 以及文本框中的内容 - 我们也看不到。



所以,这取决于你。

幸运的是,你有一个可用的工具你将帮助你找出发生了什么:调试器。一个快速的谷歌Visual Studio调试器应该为您提供执行以下操作所需的信息:



在函数的第一行放置一个断点,然后运行你的代码通过调试器。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


为了让初学者更容易,请稍微分解您的代码,以便更容易看到变量内容:

  string  fromTextBox = textBox1.Text; 
string fromClipboard = Clipboard.GetText();
if (fromTextBox == fromClipboard)
{
MessageBox.Show( );
}

现在,如果你在 if 行上设置断点,你将能够通过将鼠标悬停在鼠标悬停在变量名称上。



很抱歉,但我们不能为你做到这一点 - 时间让你学习一种新的(非常非常有用的)技能:调试!


引用:

因此,如果文本框与剪贴板文本具有相同的文本,我想要一个消息框说是。似乎没有工作。为什么? :)



最明显的答案是两个都不一样!

首先,确保测试执行:

< pre lang =c#> {
if (textBox1.Text == Clipboard.GetText())
MessageBox.Show( yes);
MessageBox.Show( 测试已执行);
}



然后如S1所示,使用中间变量,它将允许你看到它们包含调试器。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:运行你的调试器上的代码一步一步,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你显示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



在Visual Studio中调试C#代码 - YouTube [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


{
                 if (textBox1.Text == Clipboard.GetText())
                     MessageBox.Show("yes");
}



So if the textbox has the same text as the clipboards text, I want a messagebox to say "yes". Doesn't seem to work though. Why? :)

What I have tried:

解决方案

We can't tell: it's going to depend on what is on the clipboard - which we can't see - and what is in your textbox - which we can't see either.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. A quick Google for "Visual Studio debugger" should give you the info you need to do the following:

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

To make it easier for a beginner, break up your code slightly so variable contents are easier to see:

string fromTextBox = textBox1.Text;
string fromClipboard = Clipboard.GetText();
if (fromTextBox == fromClipboard)
    {
    MessageBox.Show("yes");
    }

Now if you put a breakpoint on the if line, you will be able to see exactly what you are comparing by hovering the mouse over the variable names.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


Quote:

So if the textbox has the same text as the clipboards text, I want a messagebox to say "yes". Doesn't seem to work though. Why? :)


Most obvious answer is both are not same!
First thing, make sure the test is executed:

{
                 if (textBox1.Text == Clipboard.GetText())
                     MessageBox.Show("yes");
                 MessageBox.Show("Test executed");
}


then as shown in S1, use intermediate variables, it will allow you to see their contain with debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于嗨,这个简单的代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆