|
won't work without type limitations, you can't convert just anything to double.
And I'm afraid the whole thing cannot be done in C#.
|
|
|
|
|
Yeah, I've just looked at his sample and noticed that he declared his value as a double. Too busy looking at the comments that sat "this doesn't work".
|
|
|
|
|
Thanks for the hint but no, this doesn't solve my problem:
public override TResult Calculate<TResult, TInput> (params TInput[] input)
{
int[] ints = input as int[];
double result = 0;
foreach (int item in ints) {
result += item;
}
return (TResult)result;
}
|
|
|
|
|
I'm not a generics specialist, however IMO what you want can't be handled by C# (or any other .NET language) at the moment. IIRC what you want falls under covariance and contravariance and its support is quite limited, see e.g. here[^].
The easiest statement on the subject is this:
List<TextBox> textboxes=new List<TextBox>();
List<Control> controls=textboxes;
will not be acceptable: even though a TextBox is a special Control, a list of them can't be "generalized". Same for method return values.
|
|
|
|
|
As an alternative, is there a chance of storing different kind of functions in a property?
Something like Func<int,int> and Func<double,double,double>?
Guess it's the same problem,but better asking before throwing the idea over board.
Do you have an approach on how this can be achieved? Something that didn't come into my mind?
Thank you!
|
|
|
|
|
This works for me... not sure if casting to object will cause you any issues or not.
public TResult Calculate<TResult, TInput>(params TInput[] input)
{
int[] ints = input as int[];
double result = 0;
byte test = 4;
result = test << 2;
return (TResult)(object)result;
}
|
|
|
|
|
Your Calculate method is seriously flawed. You want to use a generic parameter and return type but then fix the types inside the method implementation to int and double!
You can use type constraints to ensure that any properties/methods that you need (for conversion or whatever) are supported by the types specified.
|
|
|
|
|
I agree, unfortunately you can't restrict to double, int etc. as far as I know. Not sure why he needs it to be generic, suppose he could validate the types being passed in to verify if the generic values coming in are of type int, double etc.
|
|
|
|
|
On the rare times I've needed to restrict to a few types, I've created an interface with the required properties/methods and then created concrete wrapper classes around the required type - with implicit conversions - that implement the interfaces. The interface can then be used as a type constraint and all is well in the world
Edit: Just read your replies below - pretty much where you're heading too I think
|
|
|
|
|
If you are not absolutely required to use generics, another option would be to create a custom class that has implicit operators to all of the value types you want to support (if you added implicit cast to\from double, then double already can be cast to from int implicitly).
just a snippet example
public class testtype
{
private testtype()
{
}
public Type ValueType { get { return _type; } }
public object Value { get { return _value; } }
private Type _type = typeof(int);
private object _value = int.MinValue;
public static implicit operator testtype(int m)
{
testtype t = new testtype();
t._value = m;
t._type = typeof(int);
return t;
}
public static implicit operator int(testtype m)
{
if (typeof(int).Equals(m._type))
return (int)m._value;
return int.MinValue;
}
}
example usage
testtype v = 1;
double b = v;
Food for thought...
|
|
|
|
|
I am having way to much fun now...
This is the closest I could come up with to what you want and somewhat providing a safe implementation of it. Obviously you would need to implement some checks to avoid crashing if incorrect values are used and I don't know exactly what kind of calculations you are performing but here is what I got.
public override T Calculate<T, P>(params P[] input) where T : IConvertible where P : IConvertible
{
TypeCode mode = Type.GetTypeCode(typeof(P));
switch (mode)
{
case TypeCode.Double:
double[] args = (double[])Convert.ChangeType(input, typeof(double[]));
double r = 0;
r = args[0] + args[1];
return (T)Convert.ChangeType(r, typeof(T));
case TypeCode.Int32:
case TypeCode.Decimal:
default:
break;
}
throw new NotSupportedException("Generic type value [" + typeof(T).ToString() + "] is not supported.");
}
My test usage:
double y = Calculate<int, double>(new double[] { 1.5, 2.25 });
Console.WriteLine(y.ToString());
try
{
Calculate<string, string>(new string[] { "no", "gonna error!" });
}
catch (NotSupportedException ex)
{
System.Diagnostics.Trace.TraceError(ex.Message);
}
The trace output from throwing in a string value
Quote: UnitTest.vshost.exe Error: 0 : Generic type value [System.String] is not supported.
|
|
|
|
|
That's pretty cool - thanks your your support.
That and your first sample helped me to got it solved.
First I used a few reflections for the type passing, but now I think I'm going to make use of your implementation.
Thank you again!
|
|
|
|
|
A former employee in my office wrote a piece of software that involves using COM+ components for Message Queueing and Loosely Coupled Events. There are a couple different pieces that are to be registered by the installer, but the EventClassLogger assembly can not be registered on Windows 7, it does work on Windows XP which is where the program was originally written, I am trying to update it to Visual Studio 2010 and Windows 7. See this post:
Using the COM+ Event System service in the .Net Application.[^]
for more information.
The error I receive is:
Error 4 Cannot register assembly "C:\Delco.Hmi.Logging 2012\Delco.Hmi.Logging\Delco.Hmi.Logging.EventClass\bin\Debug\Delco.Hmi.Logging.EventClassLogger.dll" - access denied. Please make sure you're running the application as administrator. Access to the registry key 'HKEY_CLASSES_ROOT\CLSID\{4E14FBA2-2E22-11D1-9964-00C04FBBB345}' is denied.
I am running Visual Studio 2010 as Administrator. It not only fails when registering from Visual Studio but also when registering when try to install the version compiled on Windows XP. Oddly enough another person in the office created a Windows 7 install disk totally by fluke that will allow me to install the software from the Windows XP compiled version. Seeing how the install disk was made with a third party program I figured it must be making some change to the Operating system that I don't know about it. So what I did was compared registry keys on both systems. On the normal Windows 7 install the registry key in the error above is owned by Trusted Installer, on the install done using the special install disk the owner of that key is Administrators. I tried changing the owner and the permissions but with no luck.
UAC is totally disabled on all of the machines, and I am stumped on what to do to allow this component to register properly.
Does anyone have any suggestions?
Thanks
|
|
|
|
|
I would suggest to enable UAC and try it again.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I tried this but it failed. Normally enabling UAC should restrict things more not less, but it was worth a try.
Still looking for a solution.
Thanks,
Brett
|
|
|
|
|
Anyone else with suggestions, if I create a Windows install disk with RT 7 Lite then I am able to install my software so it is some kind of Windows setting stopping me.
Thanks
|
|
|
|
|
how can i get primary key of a record on datagridview
modified 29-Jun-12 16:14pm.
|
|
|
|
|
Don't you remember where you put it?
The question doesn't make sense, and it doesn't match the title either.
|
|
|
|
|
I need to check for the Microchip USB to Com Port driver, I am looking over the
System.Management route however this appears to give you the complete list of all the drivers attached at this point I can't get a list of the installed drivers from the
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPSignedDriver");
I have made use of the Managment header with
using System.Managment;
I am in the process of Googling , dinglinging to try and find out why it does not appear properly
Help!!
Note I am using Vis Studio 2008 C# Net 3.5
modified 26-Jun-12 10:11am.
|
|
|
|
|
I've used System.ServiceProcess.ServiceController for this in the past.
For everything it's this:
using System.ServiceProcess;
private static void Devices() {
ServiceController[] scDevices = ServiceController.GetDevices();
Console.WriteLine("Device driver services on the local computer");
foreach (ServiceController scTemp in scDevices) {
Console.WriteLine("{0, -25} {1, -20} {2}",
scTemp.ServiceType, scTemp.ServiceName, scTemp.DisplayName);
}
}
And to hone in on a single target:
private static void SpecificService(String name){
try {
ServiceController sc = new ServiceController(name);
Console.WriteLine("{0, -25} {1, -20} {2}",
sc.ServiceType, sc.ServiceName, sc.DisplayName);
} catch (InvalidOperationException) {
Console.WriteLine(name + " service not found");
}
}
I have the Prolific driver installed, for which SpecificService("ser2pl") gives
KernelDriver ser2pl Prolific Serial port driver
Alan
|
|
|
|
|
Thanks for that I will give it ago! Mmmm is the boss watching it's nearly 5pm & I was in early.....
Glenn
Just tried it and get
Error 1 The type or namespace name 'ServiceProcess' does not exist in the namespace 'System' (are you missing an assembly reference?)
However I have the using System.ServiceProcess; which is not recognised by the intelisense however its late I'm off home more playing tomorrow.
Thanks Glenn!
|
|
|
|
|
Ah, I thought I'd catch you out with that! You'll need to add System.ServiceProcess to the project's references. It should be on the .NET tab of the Add Reference dialog.
Alan.
|
|
|
|
|
Yup you got me! Project -> Add System.ServiceProcess is good however it lists all the drivers time for some searching of the list!
|
|
|
|
|
hi every one
I'm really sorry if my question is quite simple
what I'm trying to reach is keycode.
that is in the code below:
foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
{
int x = KeyLogger.Methods.GetAsyncKeyState(i);
and then if any key is pressed i want to use MapVirtualKey to map and after all getkeynametext
to access to it's name.
what i'm trying to accessed is the keycode which I access to it's Enumvalues to use it in the code bellow
unit key=MapVirtualKey(m,MAPVK_VK_TO_VSC);
what i need here is M which is a virtualkeycode that i have it's enumvalues in i;
thank you all in advance for helping me.
|
|
|
|
|
as mention in the subject,
im currently trying to do a system that can scan the network and list out all the actives computer within the domain network.
i check some of the sample project in this website,
but the link are dead..
|
|
|
|