|
|
I am trying to put to gether a BaseTable helper class. This class is expected to be inherited by the TableClass (each table in the database has a class)
I have the feeling I may have the wrong handle on this but here goes:
T is the table class and oTable is the datatable with the rows I want to turn into a List<> of T
RowToObject<T>(oClass1, oTable);
This takes an empty copy of the TableClass and loads the datarow in, works well
My problem is trying to create a new instance of oClass
public List<T> ToList<T>(T oClass, DataTable oTable)
{
try
{
Type t = oClass.GetType();
List<T> ClassList = new List<T>();
foreach (DataRow oRow in oTable.Rows)
{
T oClass1 = new T();
RowToObject<T>(oClass1, oTable);
ClassList.Add(oClass1);
}
return ClassList;
}
catch (Exception)
{ throw; }
}
I get the following compile error that wants a new() constraint on T but this helper class should not know who is using it, indicating I am screwing it up.
Cannot create an instance of the variable type 'T' because it does not have the new() constraint
Enlightenment would be appreciated!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
What the compiler is telling you is that nowhere in your generic type definition have you decided that type T must have a default constructor. What you need to do is change your class definition such that it has a new constraint[^] on the generic parameter. This will do two things: first, it will allow you to use new T() within your class (because you're specifying that type T must have a parameterless constructor), and second, it will only allow you to instantiate your generic class with types that have said default constructor. Without the new constraint, you would potentially be able to use a generic type that doesn't have a parameterless constructor, which would make new T() invalid.
Adam Maras | Software Developer
Microsoft Certified Professional Developer
|
|
|
|
|
Yup, got that from MSDN, so I trundled off to add a paramaterless constructor to the class (clsApp) that calls this one, made no difference to THIS class (TableBase) what I did to the calling class.
At which point I concluded I was screwing it up. TableBase , the class containing this method has no knowledge of the calling class clsApp and therfore how can it know there is a new() constraint on it.
So how should I put together a generic helper method that takes a table populates a List<> and can be inherited by all my table classes, I am considering passing the class instance into this method but it feels wrong.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I think you got it wrong: you have to add the new constraint to you generic TableBase class.
Something like this:
public class TableBase<T> where T : new()
{
}
That tells TableBase<T> that T must have a parameterless constructor.
|
|
|
|
|
Your TableBase class will need to have the new constraint applied:
public class TableBase<T> where T : new()
{
}
Edit] note to self: refresh page to check if anyone else has responded before posting [/Edit]
|
|
|
|
|
Dave and Mirko (ignore the refresh, redundancy is appreciated)
Hah, cool, completely changed the way I was utilising the base class.
Thanks guys, have a 5 for the solution (as opposed to the identification of the problem)
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Adam
Thanks for the reply but why do I feel this is an MS/MSDN type response, technically correct and absolutely right but does not solve the problem. The responses from Davey and Mirko solved the problem rather than defined the problem, while they may not have been able to supply the solution without your input, your input did not go the 1 small step further to actually be useful.
I wonder if this is not the biggest issue developers have with MSDN and are so disparaging of MS support.
Please note someone else gave you the 5 as I did not consider this a solution to the problem.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I realized that I had slightly misread the question after I saw Davey and Mirko's solution and reread your post.
No hard feelings.
Adam Maras | Software Developer
Microsoft Certified Professional Developer
|
|
|
|
|
Adam Maras wrote: No hard feelings
Certainly not, I appreciate the effort, your answer took more work than I am usually willing to invest. But then I answer a lovwer level of question
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I want to make an application that when I'm with the mouse on it ... it sets the opacity to 100 ... and when the mouse is .. outside the form.. it sets the opacity to 50
THE PROBLEM IS that the code...is working when the mouse is over the content of the form... it does not include the border of the form or the form controlbox(min,max,close) ... don't know why
I've managed to make something like that with a timer set to 100 and with this piece of code:
If Cursor.Position.X >= Me.Left And Cursor.Position.X <= Me.Right Then
If Cursor.Position.Y >= Me.Top And Cursor.Position.Y <= Me.Bottom Then
mouseover = True
Else
mouseover = False
End If
Else
mouseover = False
End If
Please help with an idea .. Thanks
|
|
|
|
|
...and do NOT spam multiple forums with the same question. You posted VB.NET code, so stick with the VB.NET forum. In fact, posting VB.NET code in a C# forum is pretty close to a cardnial sin. Be prepared to get flamed.
|
|
|
|
|
VbCodeDetectedException, thread aborting
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I am migrating a C++ application to .NET.
In C++ ,I have an template class which uses some ATL library classes
for eg.
template< class T >
class MyBaseClass:
public IConnectionPointContainerImpl<t>,
public IPersistStorageImpl<t>,
public IPersistPropertyBagImpl<t>,
{
}
Now i need to use this call in my new C# .net project where my new .NET call will get derive from above MyBaseClass .
Please let me know how can i archive this ???
Is this possible to use a template class which uses some ATL libraray classes directly in C# ?
`chiman
|
|
|
|
|
You can't.
You'd have to write a C++/CLI wrapper aroudn your class and use that in your C# app.
|
|
|
|
|
Hi,
I need to control my progressbar inside a thread by invoking it on GUI thread, otherwise the GUI may lock for a short time which is really disturbing. How can I change the Value property of a progressbar with invoking it.
Thanks in advance.
|
|
|
|
|
the thread-and-control stuff is the same for all kinds of Controls.
This[^] may help a bit.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Thank you, that article is perfect.
|
|
|
|
|
I'll make sure and tell the author.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I implemented the solution to my project easily, but there is only one problem. I need to change the enabled property of a ToolStripMenuItem inside a thread and ToolStripMenuItem is not a Control. Therefore, I can't use this method. What can I do?
|
|
|
|
|
Use the control that owns the item, so either the ToolStrip or the form. Set up a delegate there that invokes a method that sets the enabled state, and invoke that delegate as you would any other.
|
|
|
|
|
I came up with this, is this a good solution?
public delegate void ControlTSBoolConsumer(ContextMenuStrip cms, ToolStripMenuItem tsmi, bool choice);
private void SetTSEnabled(ContextMenuStrip cms, ToolStripMenuItem tsmi, bool choice)
{
if (cms.InvokeRequired)
cms.Invoke(new ControlTSBoolConsumer(SetTSEnabled), new object[] { cms, tsmi, choice });
else
tsmi.Enabled = choice;
}
|
|
|
|
|
I expect that to work fine.
I am aware the MSDN documentation doesn't say much about this, and neither does my article. I probably should add the following paragraph:
There are several items that Visual Designer can add to a Form although they aren't Controls; they are either parts of a Control (e.g. MenuItem, ToolStripMenuItem, ...) or Components (Forms.Timer, SerialPort, ...); for all of these I think a good approach is to choose a Control on which InvokeRequired/Invoke can be used; the Form itself is a good candidate for solving threading unsafety issues.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
|
hi guys
when i run this code it throw an exception
but it run well inside SQL Server 2008
ALTER proc [dbo].[back_up_database]
as
ALTER DATABASE [vvvvvvvvvvv]
SET OFFLINE
go
BACKUP DATABASE [vvvvvvvvvvv]
TO DISK = 'D:\SQLServerBackups\vvvvvvvvvvv.Bak'
with NO_COMPRESSION
go
ALTER DATABASE [vvvvvvvvvvv]
SET ONLINE
go
so what is the problem
thanks,
Mohamed El-Wehishy
|
|
|
|