Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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)
C#
string[] ComPortList = new string[3]  { null, null, null };

C#
       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;
                    }
//...

C#
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;
                }
            }
        }
    }
Posted
Updated 17-Sep-12 4:53am
v2
Comments
[no name] 17-Sep-12 10:58am    
Why are you reposting when you have an answer to your question?
Andrei Straut 17-Sep-12 11:06am    
Oh, you say it was repost? Funny, didn't see the first post. I will recast my vote to a 1 then. Thanks for mentioning it
[no name] 17-Sep-12 11:14am    
Yep http://www.codeproject.com/Questions/460827/Syntax-question-why-does-this-work
Andrei Straut 17-Sep-12 11:17am    
Hm, interesting, that question doesn't show up in my results (and I'm using "View all Questions"). Thanks again for linking, downvoted there too
Ashraff Ali Wahab 17-Sep-12 11:01am    
Your ComPortList[0] is null or ComPortList[0] gives the valid port name as COM1 or COM2 or COM3.

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 wrote:
btw, if you know a better title, or can improve my question to make it useful in the future

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)

EDIT: After Wes' repost notice, I gave this a 1 for reposting.
 
Share this answer
 
v3
Comments
pieterjann 18-Sep-12 4:50am    
I saw it was a repost, but could not figure how to delete the post. Sorry for that.

Thanks for your answer this helped :). I always try to understand code and if I don't I'd rather ask than just accept it.
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900