Thread: Local variable Question

  1. #1
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Local variable Question

    This is just a logical problem check. I have the following block of code:
    Code:
                    for (int i = 0; i < deviceCount; i++)
                    {
                        uint pcbSize = 0;
                        DeviceInfo dInfo;   <==== Declared here within the for loop
                        deviceName = string.Empty;
                        RAWINPUTDEVICELIST rid = (RAWINPUTDEVICELIST)Marshal.PtrToStructure(new IntPtr((devList.ToInt32() +
                               (dwSize * i))), typeof(RAWINPUTDEVICELIST));
    
    
                        GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);
                        if (pcbSize > 0)
                        {
                            IntPtr pData = Marshal.AllocHGlobal((int)pcbSize);
                            GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, pData, ref pcbSize);
                            deviceName = (string)Marshal.PtrToStringAnsi(pData);
                            if (rid.dwType == RIM_TYPEKEYBOARD || rid.dwType == RIM_TYPEHID || rid.dwType == RIM_TYPEMOUSE)
                            {
                                dInfo = new DeviceInfo(); <== assignment starts here 
    
                                dInfo.deviceName = (string)Marshal.PtrToStringAnsi(pData);
                                dInfo.deviceHandle = rid.hDevice;
                                dInfo.deviceType = GetDeviceType(rid.dwType);
    
                                string DeviceDesc = ReadReg(deviceName);
    
                                dInfo.deviceName = DeviceDesc;
                            }
                            Marshal.FreeHGlobal(pData);
                        }
                          dInfo.outputDeviceInfo();  <== An attempt to call member function here. (Still within for loop block code)
    
                    }//end for loop
    The attempt to call the member function is generating the the following Error: Use of unassigned local variable, does this mean the assignment is only valid within the logical parentheses block? I know if the if statement within which assignement takes places evaluates to false the variable will never be assigned.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by WDT View Post
    I know if the if statement within which assignement takes places evaluates to false the variable will never be assigned.
    And that's why it generates a compile-time error -- not all code paths have an assignment. The assignment itself is valid both inside and outside the block of code, that's not an issue. The issue is that if the condition is false, the variable never gets assigned at all.

    The simple way to fix this (assuming you're really sure you want to potentially access an uninitialized variable) would be to change the variable declaration to "DeviceInfo dInfo = null;"

    Of course, the *better* fix is to move the use of the variable into the if statement, as there's frankly no reason not to; you don't want that line called unless the condition was true.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I moved it back into the If Statement. I just wanted it separately for formatting reasons.
    Thanks.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. question regarding accessing variable values
    By do_kev in forum C Programming
    Replies: 2
    Last Post: 03-08-2008, 12:55 PM
  3. uninitialized local variable question
    By raist in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2006, 03:00 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM