|
unless the .NET class is sealed, you could derive from it and make that implement the interface of your choice.
|
|
|
|
|
Yes, a very good note.
I am pretty sure that the author of the question understands why his provided idea is impossible. So, something must be done with inheritance, or maybe if we knew what exactly he is trying to do, we could find a workaround for the problem.
|
|
|
|
|
You could try to simulate this with inheritance. You could do:
class MyGenericBase<T>{
public virtual T DoSomeStuff() { ... }
}
class MyGeneric<T> : MyGenericBase<T>
where T : MyClass1
{
public override T DoSomeStuff { ... }
}
class MyGeneric1<T> : MyGenericBase<T>
where T : IMyInterface
{
public override T DoSomeStuff() { ... }
}
I'm pretty sure, that you wan't be able to give derived class with the same identifier (but check it, compiler will tell you )
And question is: "Why would you want to do it?". Seems like your design isn't perfect.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
In some situations I also think it would be nice to have this possibility.
But take this example:
public void MyGeneric<T>(T tValue) where T : InterfaceA | InterfaceB
{
tValue.<what>();
}
I hope you see where your logical mistake lies.
Greetings
Covean
|
|
|
|
|
That's a very bad idea, but if you need it you can simulate it by providing your own checks.
|
|
|
|
|
What do you mean is a very bad idea, to have this 'OR' constraint?
I don't think so. Take a look at this example:
Lets say you want to add an extention to the values types short, int, long.
public static class ValueTypeExtention
{
public static ulong ToUnsignedLongUnchecked<T>(this T signedValue) where T : IConvertible
{
return signedValue.ToUInt64();
}
public static ulong ToUnsignedLongUnchecked<T>(this T signedValue) where T : struct { ... }
public static ulong ToUnsignedLongUnchecked(this short signedValue) { return signedValue.ToUInt64(); }
public static ulong ToUnsignedLongUnchecked(this int signedValue) { return signedValue.ToUInt64(); }
public static ulong ToUnsignedLongUnchecked(this long signedValue) { return signedValue.ToUInt64(); }
public static ulong ToUnsignedLongUnchecked<T>(this T signedValue) where T : short | int | long
{
return unchecked((ulong)signedValue);
}
public static ulong ToUnsignedLongUnchecked<T>(this T signedValue) where T : (short | int | long) as IConvertible
{
return signedValue.ToUInt64();
}
}
A, B and C are versions how you could solve this problem nowadays.
But lets have a deeper look:
- A allows bool bValue=false; ulong ulVal = bValue.ToUnsignedLongUnchecked(); . It allows it for
every class/struct that implements IConvertible.
- B needs type checking and offers ToUnsignedLongUnchecked() to every struct even BITMAPINFO.
- C is the only way to solve the condition completely but needs multiple implementations.
Using where T : struct, IConvertible as constraints is the best way to solve this problem with generics so far.
The 'pseudo' constraint D just allows T to be one of this 3 value types. And I think E could be the best way.
Greetings
Covean
|
|
|
|
|
I'm not sure what you mean.
MyClass1 doesn't need to implement IMyInterface -- as long as the provided class does and derives from MyClass1.
public class Base
{
}
public class Derived : Base , System.IDisposable
{
public void
Dispose
(
)
{
}
}
public class Generic<T> where T : Base , System.IDisposable
{
}
var x = new Generic<Derived>() ;
|
|
|
|
|
Hi
When I do the installation from my windows app, it installs and finishes but my program throws errors...
If I go into the folder where the sqlexpress.exe file is and click on it then it installs and my app works!!
Here is my code I'm using :
Process myProcess = new Process();
myProcess.StartInfo.FileName = @"C:\folder\sqlexpr32.exe";
myProcess.StartInfo.Arguments = "start /wait " + vlNew_SQLExpressFilePath.ToString() + " /qn username=test companyname=testany addlocal=ALL disablenetworkprotocols=0 instancename=MyInstance SECURITYMODE=SQL SAPWD=test123";
myProcess.StartInfo.UseShellExecute = false;
myProcess.Start();
thanks in advance
|
|
|
|
|
You shouldn't force the install of any particular version of a third-=party product -- the user may alreday have a better one installed.
Simply document the requirements and let the user install what he chooses.
|
|
|
|
|
that was quick thanks...
I'm doing checks to see if there is an older or new version on there and if there is a version on there then it wont install.
The user cant select what they want to install, I need to install that somewhere in the background so that the user does not know about it, silent install...
-- Modified Wednesday, August 11, 2010 9:24 AM
|
|
|
|
|
Your design assumes that your users all have permissions to install software, or has admin rights to their own machines. This is not always the case and is rare to find in a corporate environment. You should not be installing anything from your application at all. This should be done from your installer for your app instead.
|
|
|
|
|
Thanks, I know but I didnt have any other solutions. How can I install sql express when they install the click once app?
is there settings that I must set before publishing?
|
|
|
|
|
All you have to do is select it as a prerequisite in the ClickOnce publishing properties and tell it where to get the SQL Server Express installation from, usually the vendors website. ClickOnce handles the rest.
Though, the problem with the user needing to be an admin to install it still exists.
|
|
|
|
|
i want to copying array values to another array.
my vaue is store the following array:
string[] path = Directory.GetFiles(filedate);
and I want to copy, stored value in path into path1.
string[] path1= ???
thanx
|
|
|
|
|
You can do this by using Array.Copy[^] method.
Example:
string [] path = Directory.GetFiles(filedate);
if(path.Length > 0)
{
string [] path1 = new string[path.Length];
Array.Copy(path, path1, path.Length);
}
|
|
|
|
|
|
hi!
I have a directory, which contain some .txt files, .doc files and .xml files. I want to move only .xml files to another directory.
I had already done one file but i couldn't move multiple xml files.
please do my favour.
thanx & regards
|
|
|
|
|
You can move your files at once by using SHFileOperation API.
This[^] is a very good article that describes how you can use this API in C#.
One of the examples in the article shows exactly how to move multiple files.
|
|
|
|
|
Here is how i would do it:
foreach (string f in Directory.GetFiles(@"C:\MyFolder"))
{
if (f.EndsWith(".xml"))
{
string currentf = f.Substring(f.LastIndexOf('\\') + 1);
File.Move(f, @"C:\MyFolder\MyXmlFolder\" + currentf);
}
}
It iterates through every file in MyFolder and if it's an xml then move it to MyXmlFolder.
|
|
|
|
|
Thank you vey much! Islorvat
but if my file already exist in destination,then...
with regards
modified on Wednesday, August 11, 2010 7:35 AM
|
|
|
|
|
Thanx, I resolved my problems
|
|
|
|
|
you may want to read up on the System.IO.Path[^] class, which offers methods to deal with file paths.
|
|
|
|
|
Hi,
I made my custom editor for my object using listdictionary type where I store the collection of objects.
I have an ancestor form where I am trying to create the property with binded designer. That will be used for specification
of datagridview columns in inherited form. But main methods must be in an ancestor.
[Browsable(true)]
[Category("BrowserForm")]
[EditorAttribute(typeof(DisplayColumnsEditor), typeof(UITypeEditor))]
public ListDictionary DisplayColumns
{
get { return displaycolumns; }
set { displaycolumns = value; }
}
There is the code for the editor:
using System;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.Collections;
namespace mynamespace
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
class DisplayColumnsEditor : UITypeEditor
{
public DisplayColumnsEditor()
{
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc == null)
{
return null;
}
ListDictionary editval;
if (value == null)
{
editval = new ListDictionary();
} else
{
editval = (ListDictionary)value;
}
using (DisplayColumnsEditorDialog form = new DisplayColumnsEditorDialog(editval))
{
if (edSvc.ShowDialog(form) == DialogResult.OK)
{
context.OnComponentChanged();
return (object)form.Current;
}
}
return value;
}
}
}
there is the class for the listdictionary objects:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
namespace mynamespace
{
[Serializable()]
public class DisplayColumn : ISerializable
{
private string _name;
private string _headertext;
private string _datapropertyname;
private int _width;
public DisplayColumn()
{
this._name = "";
this._datapropertyname = "";
this._headertext = "";
this._width = 100;
}
public DisplayColumn(string colname)
{
this._name = colname;
this._datapropertyname = colname;
this._headertext = colname;
this._width = 100;
}
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
public string HeaderText
{
get
{
return this._headertext;
}
set
{
this._headertext = value;
}
}
public string DataPropertyName
{
get
{
return this._datapropertyname;
}
set
{
this._datapropertyname = value;
}
}
public int Width
{
get
{
return this._width;
}
set
{
this._width = value;
}
}
public DisplayColumn(SerializationInfo info, StreamingContext ctxt)
{
Name = (String)info.GetValue("Name", typeof(string));
DataPropertyName = (String)info.GetValue("DataPropertyName", typeof(string));
HeaderText = (String)info.GetValue("HeaderText", typeof(string));
Width = (int)info.GetValue("Width", typeof(int));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("Name", Name);
info.AddValue("DataPropertyName", DataPropertyName);
info.AddValue("HeaderText", HeaderText);
info.AddValue("Width", Width);
}
}
}
The editor uses classical windows forms(IWindowsFormsEditorService) and returns a listdictionary collection. If it will be needed I can include the code for it. But I can ensure it returns really listdictinary collection.
Tha main trouble consist of storing value into resource file of inherited form. When I use the designer
and open the resource file there is a key named: $this.DisplayColumns and not just DisplayColumns.
This name of key is problematic, because value is available only for the designer, but not present in run-time.
in inerited form's resource file it creates following node:
<data name="DisplayColumns" mimetype="application/x-microsoft.net.object.binary.base64">
<value>
AAEAAAD cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5BQEAAAAtU3lzdGVtLkNvbGxlY3Rpb25z
LlNwZWNpYWxpemVkLkxpc3REaWN0aW9uYXJ5BAAAAARoZWFkB3ZlcnNpb24FY291bnQIY29tcGFyZXIE
AAADPFN5c3RlbS5Db2xsZWN0aW9ucy5TcGVjaWFsaXplZC5MaXN0RGljdGlvbmFyeStEaWN0aW9uYXJ5
Tm9kZQIAAAAICBxTeXN0ZW0uQ29sbGVjdGlvbnMuSUNvbXBhcmVyAgAAAAkDAAAABgAAAAMAAAAKDAQA
AABHU09QUlZDb3JlLCBWZXJzaW9uPTEuMC4zODcyLjI1NTAwLCBDdWx0dXJlPW5ldXRyYWwsIFB1Ymxp
Y0tleVRva2VuPW51bGwFAwAAADxTeXN0ZW0uQ29sbGVjdGlvbnMuU3BlY2lhbGl6ZWQuTGlzdERpY3Rp
b25hcnkrRGljdGlvbmFyeU5vZGUDAAAAA2tleQV2YWx1ZQRuZXh0AgQEIlNsaW0uU09QUlYuU09QUlZD
b3JlLkRpc3BsYXlDb2x1bW4EAAAAPFN5c3RlbS5Db2xsZWN0aW9ucy5TcGVjaWFsaXplZC5MaXN0RGlj
dGlvbmFyeStEaWN0aW9uYXJ5Tm9kZQIAAAACAAAABgUAAAAFR2NfSUQJBgAAAAkHAAAABQYAAAAiU2xp
bS5TT1BSVi5TT1BSVkNvcmUuRGlzcGxheUNvbHVtbgQAAAAETmFtZRBEYXRhUHJvcGVydHlOYW1lCkhl
YWRlclRleHQFV2lkdGgBAQEACAQAAAAJBQAAAAYJAAAAC2RhdGExMDBrX2lkBgoAAAACSURkAAAAAQcA
AAADAAAABgsAAAAHR2NfVEVYVAkMAAAACQ0AAAABDAAAAAYAAAAJCwAAAAYPAAAABXRleHQxBhAAAAAE
VEVYVGQAAAABDQAAAAMAAAAGEQAAAAhHY19DSVNMTwkSAAAACgESAAAABgAAAAkRAAAABhQAAAAFY2lz
bG8GFQAAAAfEjMOtc2xvZAAAAAs=
</value>
</data>
in inherited form's designer.cs it creates a row, where it loads its value:
this.DisplayColumns = ((System.Collections.Specialized.ListDictionary)(resources.GetObject("$this.DisplayColumns")));
but I would need just:
this.DisplayColumns = ((System.Collections.Specialized.ListDictionary)(resources.GetObject("DisplayColumns")));
Is there anyone, who has an idea what does this cause? Is this caused by using serialization? Or am I doing anything else wrong, beacuse I did not implement any TypeConverters? Is this normal "store key" with $ prefix of collections for resource files?
JvR
|
|
|
|
|
Sir i need to extract ECG wave from an ECG strip i m able to do it using MATLAB.. is there any way i can do it in c#.. if yes kindly brief me on the possible ways.
Thank you
|
|
|
|
|
The only way I can think of is to write some code.
Alternatively, search the web - somebody might already have done it.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|