C# .NET - Beginner-question: An object reference is required for the non-static field, method...

Asked By Dennis K. on 14-Sep-09 06:48 PM
Hi there,

just to warn you, I am an absolutelly beginner in c# (coding in general), creating most things with learning-by-doing method..
Now I am stuck somehow, as my knowledge has hit the borders and the net couldnt give me clear answers.

I am trying atm to retrieve the DeviceName (and later the ShaderVersion) of a video-adapter/graphics card via directx SDK.

I was able till now to retrieve other stuff, like from registry (OS name,version, framework version), but now I am just stuck and dont get, what the compiler wants from me. Help please or atleast give me some hints where to dig (some tutorial?) about this topic.

Here the code of my get_sys_info class:


public static string OsVersion
{
get
{

string platformId = Environment.OSVersion.Platform.ToString();

int majorVer = Environment.OSVersion.Version.Major;

int minorVer = Environment.OSVersion.Version.Minor;

string osVersion = "#";

switch (platformId)
{

case "Win32Windows": //old crap...
{

if (majorVer >= 4 && minorVer == 0)
{

osVersion = "Windows 95";

break;

}

if (majorVer >= 4 && (minorVer > 0 && minorVer < 90))
{

osVersion = "Windows 98";

break;

}

if (majorVer >= 4 && (minorVer > 0 && minorVer >= 90))
{

osVersion = "Windows Me";

break;

}
else
osVersion = "Unknown Win32Windows OS";

break;

}

case "Win32NT":
{

if (majorVer == 3 && minorVer <= 90)
{

osVersion = "Windows NT3.51";

break;

}

if (majorVer == 4 && minorVer == 0)
{

osVersion = "Windows NT";

break;

}

if (majorVer == 5 && minorVer == 0)
{

osVersion = "Windows 2000";

break;

}

if (majorVer == 5 && minorVer == 1)
{

osVersion = "Windows XP";

break;

}

if (majorVer == 5 && minorVer == 2)
{

osVersion = "Windows 2003";

break;

}

if (majorVer == 6 && minorVer == 0)
{

osVersion = "Windows Vista / 2008";

break;

}

if (majorVer == 6 && minorVer == 1)
{

osVersion = "Windows 7";

break;

}

else
osVersion = "Unknown Win32NT OS";

break;

}

}

return osVersion;

}
}

public static string CPUspeed
{
get
{

RegistryKey RegKey = Registry.LocalMachine;
RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
Object CPUspeed = RegKey.GetValue("~MHz");
return CPUspeed.ToString();
}
}
public static string CPUname
{
get
{

RegistryKey RegKey = Registry.LocalMachine;
RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
Object CPUname = RegKey.GetValue("ProcessorNameString");
return CPUname.ToString();
}
}
public static string Framework2
{
get
{

RegistryKey RegKey = Registry.LocalMachine;
RegKey = RegKey.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727");
Object Framework2 = RegKey.GetValue("Install");
return Framework2.ToString();
}
}
public static string Framework2version
{
get
{

RegistryKey RegKey = Registry.LocalMachine;
RegKey = RegKey.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727");
Object Framework2version = RegKey.GetValue("Version");
return Framework2version.ToString();
}
}

//this is the bugged part, I am getting "An object reference is required for the non-static field, method, or property 'Microsoft.DirectX.Direct3D.AdapterDetails.DeviceName.get' "
public static string VideoAdapter
{
get
{
Object VideoAdapter = Microsoft.DirectX.Direct3D.AdapterDetails.DeviceName;
return VideoAdapter.ToString();
}
}

I am using this reference: http://msdn.microsoft.com/en-us/library/bb322789%28VS.85%29.aspx
My main problem is here just that I really don't get what the compiler wants from me.. If someone could also write a "example" how to fix it, I would be really glad!

Thanks

Best Regards
cracky

The problem is that the DeviceName is not static - Michael Detras replied to Dennis K. on 14-Sep-09 10:11 PM

Hi,

The DeviceName property is not static so you won't be able to get it without an instance of an AdapterDetails structure. So you should first get it. I'm no expert in DirectX but you could get I think you could get the AdapterDetails using Manager.Adapters.Default.Information. Someone, please correct me if I'm wrong. Thanks.