|
Why don't you look at (and/or show us) the relevant code, starting at the button's Click handler that starts the trouble?
The most popular way to create a StackOverflowException is by having a property call itself, i.e. doing something like:
private int counter;
public int Counter {
get {
if (some_error_condition) MessageBox.Show("Can't comply, however the counter's value equals "+Counter);
return counter;
}
The mistake here is the upper-case Counter inside the Show method.
|
|
|
|
|
It was PEBCAK. Somehow I ended up with this.Close in the FormClosing event - And for some reason even stranger, it worked on some computers and not others!
|
|
|
|
|
OK, I never had that one before, but it basically is the same (although one might hope Windows would include a safeguard against that).
The difference amongst systems might be due to timing differences (they don't play by the book exactly when the process is exiting anyway); or just perhaps some Service Pack just added a safeguard!
If you ever find out, please let us know.
|
|
|
|
|
Hi guys!
I'm dealing with a problem I can't find a solution for. What I'm trying to do is this:
I have a Model which should contain a generic method (generic input and return value).
And furthermore these Models should be merged into a List.
And here it gets complicated and I end up in a dead end.
Here's the code which hopefully makes it more clear:
class MainClass
{
public static void Main (string[] args)
{
List<ContainerBase> containers = new List<ContainerBase>();
containers.Add(new ContainerA());
containers.Add(new ContainerB());
}
}
abstract class ContainerBase
{
public string Name {
get;
set;
}
public abstract TResult Calculate<TResult, TInput>(params TInput[] input);
}
class ContainerA : ContainerBase
{
#region implemented abstract members of Generics.ContainerBase
public override TResult Calculate<TResult, TInput> (params TInput[] input)
{
int[] ints = input as int[];
double result = 0;
return result;
return result as TResult;
}
#endregion
}
class ContainerB : ContainerBase
{
#region implemented abstract members of Generics.ContainerBase
public override TResult Calculate<TResult, TInput> (params TInput[] input)
{
throw new System.NotImplementedException ();
}
#endregion
}
This is needed because I want to be able to combine different algorithms (presented by the Model), e.g. Algo 1: combine 3 integers, Algo 2: divide two integers, Algo 3: get the sort via Drag&Drop (in MVVM - hence the unified approach).
An Alternative would be to store a whole Metod in a Property in the Model, but it needs to be generic as well. And I have no idea how to do that either.
Help would be appreciated!
|
|
|
|
|
Try
return (TResult)result;
|
|
|
|
|
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
|
|
|
|