none
C# An Object Reference is Required

    Question

  • I keep getting the "An object reference is required for the non-static field, method, or property 'POLLING_UTILITY.WorkWithRegistry.Read(string)'" error with the code below.  I've tried a number of things based on what I can find in other posts, but don't seem to be able to quite get it.  Any help would be greatly appreciated!

     

     

     class GatherInformation
     {
    
     
      public static void GatherInformation()
      {
    
       Console.WriteLine("Gathering Information...");
    
       string windows_version = WorkWithRegistry.Read(@"HKLM\Software\Microsoft\Windows NT\CurrentVersion");
    
      }
     }
    

     

    Which calls:

     

    public class WorkWithRegistry
    {
    .
    .
    .
    public string Read(string KeyName)
    		{
    			// Opening the registry key
    			RegistryKey rk = baseRegistryKey ;
    			// Open a subKey as read-only
    			RegistryKey sk1 = rk.OpenSubKey(subKey);
    			// If the RegistrySubKey doesn't exist -> (null)
    			if ( sk1 == null )
    			{
    				return null;
    			}
    			else
    			{
    				try 
    				{
    					// If the RegistryKey exists I get its value
    					// or null is returned.
    					return (string)sk1.GetValue(KeyName.ToUpper());
    				}
    				catch (Exception e)
    				{
    					// AAAAAAAAAAARGH, an error!
    					ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
    					return null;
    				}
    			}
    		}	
    

     

     

    • Edited by MKSwanson Wednesday, April 28, 2010 5:33 AM Formatting
    Wednesday, April 28, 2010 5:15 AM

Answers

  • using POLLING_UTILITY; top of the class where you import your all references..

    Like-

    using System;

    using System.xml;

    using POLLING_UTILITY;

     

    regards

    jayant

    • Marked as answer by MKSwanson Wednesday, April 28, 2010 7:02 AM
    Wednesday, April 28, 2010 6:07 AM

All replies

  • is POLLING_UTILITY  is  a function in another class..?

    if yes then create a object of this class and then call this function

    like-

    ClassName cls = new ClassName();

     string
     windows_version = cls.POLLING_UTILITY.WorkWithRegistry.Read(@"HKLM\Software\Microsoft\Windows NT\CurrentVersion"
    );

     

    regards

    jayant

    Wednesday, April 28, 2010 5:24 AM
  • POLLING_UTILITY is the namespace.  I had the long reference beauce they were originally in different namespaces, but it can probably be removed now.

     

    The formatting in the original post is a little hard to follow, so I just updated it to try to make it easier. The class is actually WorkWithRegistry for the Read function.

    Wednesday, April 28, 2010 5:34 AM
  • ok , have you try my above post-?

    try this-

    using POLLING_UTILITY;

    WorkWithRegistry wrkReg = new WorkWithRegistry();

    and then call function-

    string windows_version = wrkReg.read(@"HKLM...........");

     

    regards

    jayant

     

    Wednesday, April 28, 2010 5:53 AM
  • Make read method static

    public static string Read(string KeyName)

     

    Wednesday, April 28, 2010 5:57 AM
  • I now am receiving "A field initializer cannot reference the non-static field, method, or property POLLING_UTILITY.GatherInformation.wekReg.  

    The updated code is below:

     class GatherInformation
      {
          
        WorkWithRegistry wrkReg = new WorkWithRegistry();
    
        string windows_version = wrkReg.WorkWithRegistry.Read(@"HKLM\Software\Microsoft\Windows NT\CurrentVersion");
          
    
        public static void GatherInformation()
        {
    
          Console.WriteLine("Gathering Information...");
    
    
          string image_version = "";
          using (StreamReader rdr = File.OpenText(@"%SYSTEMROOT%\SYSTEM32\print.dll"))
          {
            image_version = rdr.ReadToEnd();
          }
    
    
    
        }
      }
    When I included using POLLING_UTILITY, it was invalid...unless I'm putting it in the wrong place.

    Wednesday, April 28, 2010 6:00 AM
  • using POLLING_UTILITY; top of the class where you import your all references..

    Like-

    using System;

    using System.xml;

    using POLLING_UTILITY;

     

    regards

    jayant

    • Marked as answer by MKSwanson Wednesday, April 28, 2010 7:02 AM
    Wednesday, April 28, 2010 6:07 AM
  •  

    string windows_version = wrkReg.WorkWithRegistry.Read(@"HKLM\Software\Microsoft\Windows NT\CurrentVersion");

    Above code block has to be put in some method. you cannot call a method in class like this. This is exactly what the errror messsage says

    Try something like 
    public void Do()
    {
    string windows_version = wrkReg.WorkWithRegistry.Read(@"HKLM\Software\Microsoft\Windows NT\CurrentVersion");

    }

    Wednesday, April 28, 2010 6:45 AM
  • You can call a method in the initializer of a field. What you cannot do is use a non-static field in an initializer.

    Keep your code as you had in the first place, but make the Read method static.

    Wednesday, April 28, 2010 1:40 PM