kn0pee Posted April 11, 2018 Report Posted April 11, 2018 (edited) Hi All. I'm developing a program in C#, which involves dynamic offsets (The user creates settings in the program which are saved to a file). The data the user inputs is The offset address (e.g. 0x02BC), The offset size / length (e.g. 4) The type of the output (e.g. int32, float32 etc.) Adjustments to be made to the end result (e.g / 256) Currently the program creates the offset, using the address and length. This works great and when manually specifying the type I can successfully get the value from the flight sim. Offset myOffset = new Offset("Main", OffsetAddress, OffsetLength); However I am not sure how I can go about using my type variable when getting the information int offsetValue = myOffset.GetValue<MyTypeHere>(); I get the error Type is a variable but used as a type. I have also tried typeof(MyTypeHere), and several other methods however I haven't been able to fix it. I am quite new to C# so apologies if this is a really stupid question. EDIT: I know I can use a switch(MyTypeHere) and then have a case for every single type, however I am trying to avoid this if possible. Thanks, Brad Edited April 11, 2018 by kn0pee
Paul Henty Posted April 11, 2018 Report Posted April 11, 2018 Hi Brad, In C#, the <T> syntax is information for the compiler at compile-time. This means you can't use variables for 'T' because the compiler won't know what the type is. So, the only way to use this syntax is a switch statement with the correct type 'hard-coded': Here's a simple example using a string to hold the type: (Could also be an enum you have created) string variableType = "Int32"; switch (variableType) { case "Int32": int valueInt = myOffset.GetValue<int>(); break; case "Double": double valueDbl = myOffset.GetValue<double>(); break; } Note that the return types are strongly typed. If you need to process these values later, you might consider casting all the values up to 'double's for easier use. (i.e. all the values will be of one type (double)). But this will depend on what your application does with the values. There is another syntax that does take a variable to specify the type: Type variableType = typeof(int); object myValue = myOffset.GetValue(variableType); However, at some point you'll need a switch with this method to generate the real type from the enum or string that you use to hold the offset type. Also note that this method returns an object so you'll also need a switch to cast to the underlying type (unless you're just going to call ToString() on the values). Which method is best comes down to your personal preference and how your application uses the offset values. But I think it's likely that you'll need a switch statement somewhere. It's just down to C# being a type-safe language. Paul
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now