views:

26

answers:

2

I know the title sounds a bit strange, but this has been boggling my mind for a little bit. So Intel offers this TurboBoost sidebar gadget with calls using JavaScript, and I want to write a program from scratch in C# that does the same thing. The calls stem from what I believe is an ActiveX DLL which I easily imported. The problem is, whenever I try to call a function, it gives me the error "an object reference is required for the non-static field..." I've found all of the functions e.g. the one I used return a dynamic data structure. I've tried splitting up the functions and made them both static but still no luck. Here's the code(ITurboBoostClient is the interface portion):

namespace TurboBoostMon_CLI
{
    class Program
    {
        public static object GetCPUFreq()
        {
            object n = ITurboBoostClient.GetCurBaseFrequency(); //<---- error
            //return Convert.ToDouble(n);
            return n;
        }

        public static void Main(string[] args)
        {
            object cpubasefreq = GetCPUFreq();
            Console.WriteLine(cpubasefreq); // neglect the output for now
        }
    }
}
+1  A: 

If typical naming conventions are being used, ITurboBoostClient is an interface, and you do not have an instance of an object that implements the interface. Hence, the error.

Without knowing more about the ActiveX DLL, its hard to say exactly what to do, but it would be along the lines of:

{
    ITurboBoostClient myClient = TurboBoostFactory.GetInstance();
    object n = myClient.GetCurBaseFrequencey();
    return n;
}

Note that in the first line, you call a static method that can product the class (with the interface) that is required. Then you can actually use that interface.

Look again through the ActiveX library you imported, and see if you can find a factory method, a CreateInstance method, or some other instantiator that will create the initial object.

abelenky
A: 

If you're getting that error, then you need to declare something as a new object. Assuming your error marker is correct, you need to change that to create a new instance of some object that inherits the ITurboBoostClient, then use that to call the GetCurBaseFrequenct() method.

Something like:

ITurboBoostClient myTurboBoost = new TurboBoostClientObject(); // Making names up here, not familiar with the framework you're working with.
object n = myTurboBoost.GetCurBaseFrequency();

Sorry I don't know what class you need to instantiate there, but a short dig on google will most surely be revealing.

md5sum
Very similar to my answer, with the caveat that new probably won't work, as this program almost certainly does not have the full class definition or knowledge of how to instantiate it. That is why I pushed towards finding a static method in the library.
abelenky
Most of what I've worked with in ActiveX has provided direct access to instantiate a new instance of an object. However, that is somewhat limited, so it may not always be that way.
md5sum