为什么我的代码有效? [英] Why does my code work?

查看:82
本文介绍了为什么我的代码有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题在下面的代码中,有人可以解释我的问题所在的行的确切工作方式吗? (或提供说明的链接)

谢谢:)

(顺便说一句,如果您知道一个更好的标题,或者可以改善我的问题以使其将来有用,请这样做)

My question is in the code below, could someone explain the exact working of the line where my question is? (or give a link to where it is explained)

thank you :)

(btw, if you know a better title, or can improve my question to make it usefull in the future, please do so)

string[] ComPortList = new string[3]  { null, null, null };


       private void autoConnect()
        {
            string tmpport;

            if (driverOK == true && need2PlugUSB == false)
            {
                updateComportList();
                tmpport = ComPortList[0]; //Why is this working if it's fixed to 0?

                if (tmpport != null)
                {
                    try
                    {
                        ser1.PortName = tmpport;
                        ser1.Open();
                        clearBuffer();
                        connected = true;
                    }
//...


private void updateComportList()
{
    int k = -1;
    RegistryKey Machine = Registry.LocalMachine; ;
    RegistryKey comKey;
    String mainKey = @"HARDWARE\DEVICEMAP\SERIALCOMM";

    try
    {
        comKey = Machine.OpenSubKey(mainKey);
        string[] subkeysNames = comKey.GetValueNames();
        if (subkeysNames.Length > 0)
        {
            for (int i = 0; i < subkeysNames.Length; i++)
            {
                if (subkeysNames[i].Contains("labser"))
                {
                    k++;
                    string value = (comKey.GetValue(subkeysNames[i]).ToString());
                    ComPortList[k] = value;
                }
            }
        }
    }

推荐答案

嗯,这很简单.在updateComportList方法中,设置ComPortList数组的值.然后,获得数组的第一个元素(记住,它刚刚被设置),并检查它是否为null.
由于刚刚设置了位置0处的数组元素(不再为null),并且将其值分配给了字符串变量tmpPort,因此该字符串不再为null.这就是为什么您的条件失败的原因.
Well, it''s pretty simple. In updateComportList method you set the values of your ComPortList array. Then you get the first element of the array (which remember, has just been set) and you check it for null.
As the array element at position 0 has just been set (is not null anymore) and you assign its value to your string variable tmpPort, that string is no longer null. That''s why your condition fails.

pieterjann写道:
pieterjann wrote:

顺便说一句,如果您知道更好的标题,或者可以改善我的问题以使其将来有用

btw, if you know a better title, or can improve my question to make it useful in the future


老实说,我对这个标题有些吃惊.这里的大多数问题都问为什么代码不起作用起作用,所以我认为它是一种错字(那是一个有趣的错字).
但是,在充分阅读您的问题之后,似乎措辞恰当并且很切合实际.因此,我给你5分(这种情况很少发生)

在Wes重新发布通知后,我给它一个1作为重新发布.


To be honest, I was a bit taken aback at the title. Most questions here ask why the code doesn''t work, so I thought it a typo (and a funny one at that).
After fully reading your question however, it seems properly phrased and to the point. So I will give you a 5 for it (which happens quite rarely)

After Wes'' repost notice, I gave this a 1 for reposting.


数组是从零开始的,所以当您访问数组时,代码中的第一个是返回该数组中的三个字符串元素.但是,还有其他值得注意的事情:填充ComPortList数组的代码不会检查subkeysNames.Length是否大于三.如果是这种情况,则代码将中断,因为它将尝试访问数组中不存在的第四个元素.

问候,

—曼弗雷德(Manfred)
Arrays are zero based so when you access the array the way it is depicted in your code the first of the three string elements in that array is returned. Something else noteworthy is going on though: The code that populizes the array ComPortList does not check if subkeysNames.Length is greater than three. If that were the case the code would break because it would try to access a fourth element which is not present in the array.

Regards,

— Manfred


这篇关于为什么我的代码有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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