|
Sorry for the delayed reply. But IMO this really seems like a missing functionality in language. Simple things such as not being able to have a collection of Nullable types makes me wonder if this was intentional or something they overlooked. e.g. List<Nullable> myNullableValuesCollection = new List<Nullable>();
Here's my real requirement:
I have the following classes/interfaces:
- IDomainObject<br />
- DomainObject : IDomainObject<br />
- Employee : DomainObject<br />
- Company : DomainObject<br />
- Project : DomainObject
I've the following UI elements:
- IDataboundView <T> where T:IDomainObject<br />
- BaseView : UserControl<br />
- EmployeeManagementView : BaseView, IDataboundView<Employee><br />
- CompanyManagementView : BaseView, IDataboundView<Company><br />
- ProjectManagementView : BaseView, IDataboundView<Project>
In the BaseView's load method, I'd like to be able to do something like ...
void OnLoad(..)
{
if (this is IDataboundView)
{
Get the generic type parameter for the IDataboudView (e.g. Employee, Company, Project...)
Get the list of IDomainObjects (e.g. EmployeeColl/CompanyColl/ProjectColl etc)
Call the IDataboundView's method for "this" instance and set the collection
}
}
But I cannot achieve the above. Do you have any suggestions?
|
|
|
|
|
What you can do is have an instance of T as a member variable, and use 'is' on that.
C++ templates were certainly more powerful, but, realistically, most people used them in the ways that generics are now used.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi All,
I am working on a MCMS page with a dropdown list of all the existing posting Name. The page will render a "preview" of the page depend on the dropdown value. Could someone tell me how to work this out? Thanks in advance
gsdg
|
|
|
|
|
Good People,
I need to cast from my class to a native data type. For example: int payRate = MyNumberObject. How do I do that? Also, how do I build my class such that it can be interchangeable with the native numeric types.
Any information or a link to some information would be great.
Thanks,
BP
|
|
|
|
|
BlitzPackage wrote: I need to cast from my class to a native data type.
And ?
BlitzPackage wrote: int payRate = MyNumberObject
int payRate = MyNumberObject as int;
od
int payrate = (int)MyNumberObject;
Inherit your class from that type.
BlitzPackage wrote: interchangeable with the native numeric types.
|
|
|
|
|
LongHC wrote: int payRate = MyNumberObject as int;
You can't as a value type.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Sorry, my fault, thanks for correcting me.
|
|
|
|
|
Hey!
LongHC wrote: int payRate = MyNumberObject as int;
you can't use 'as' operator with value type classes :/ only reference type..
life is study!!!
|
|
|
|
|
Hi,
AFAIK this is not possible.
You can implement implicit casting from some value type such as int to your type,
but not the other way around.
But you can provide a method to do the conversion, someting similar to
ToString() which exists for all objects, and ToInt32() which exists for
some classes (e.g. IntPtr).
|
|
|
|
|
yes you can, try this:
public class MyClass
{
private int m_width;
public int Width
{
get { return m_width; }
set { m_width = value; }
}
public static implicit operator int (MyClass source)
{
int output = source == null ? 0 : source.Width;
return output;
}
public void Test()
{
int value = this;
Console.WriteLine(value);
}
}
a peruvian programmer
|
|
|
|
|
You are right of course, dont know what I was thinking here.
|
|
|
|
|
i tend to err on the side of caution and use:
public static explicit operator int(MyClass source)
Russ
|
|
|
|
|
Is there a standard .NET object that abstracts version information (at minimum, major, minor, edit and build numbers)?
/ravi
|
|
|
|
|
You could use System.Diagnostics.FileVersionInfo, it gives you all the information your looking for.
Tarakeshwar Reddy
MCP, CCIE Q(R&S)
There are two kinds of people, those who do the work and those who take the credit. Try to be in the first group; there is less competition there. - Indira Gandhi
|
|
|
|
|
Thanks! I knew there had to be something!
/ravi
|
|
|
|
|
|
Even better - many thanks!
Which begs the question: why didn't it show up in my Google and MSDN search?
/ravi
|
|
|
|
|
Hello,
i pinvoke a function from an com api. The function needs two parameters, an com interface and one out parameter. The prototype from the function is
BOOL WINAPI theFunc(theObject* pObject, LPTSTR szName);
My Call
<br />
[System.Runtime.InteropServices.DllImport("mydll.dll")]<br />
static extern unsafe bool theFunc([MarshalAs(UnmanagedType.Interface)] theItem item, StringBuilder buffer);<br />
<br />
StringBuilder buffer = new StringBuilder(1024);<br />
try<br />
{<br />
bRet = teFunc(_o, buffer);<br />
}...<br />
In a normal Windows Application there is no error. The error occured only when i use the code in a c# Windows service. The exception is that the memory might be damaged... AccessViolationException
Any ideas?
-- modified at 14:54 Monday 16th April, 2007
|
|
|
|
|
realmontanakid wrote: Any ideas?
How is native code going to be able to use a StringBuilder object?
led mike
|
|
|
|
|
Mike, this is standard procedure when passing in strings into functions that are expected to modify the string.
|
|
|
|
|
Yes forgot that, had to go look it up again!
However if the native code will write to it the signature should include a bufferSize argument which is the preallocated StringBuffer.Capacity, yes?
led mike
|
|
|
|
|
Is theItem being marshalled right? From the stuff you posted, I don't see anything that appears to be wrong.
*edit* are you sure you need StringBuilder? I don't think it will hurt, however, you should only use StringBuilder if the native method will be modifying/creating the string.
|
|
|
|
|
I can do it with a StringBuilder or like this,
<br />
byte[] buffer = new byte[512];<br />
<br />
unsafe<br />
{<br />
fixed (byte* pBuffer = buffer)<br />
{<br />
ret = theFunc(_o, pBuffer);<br />
}<br />
}<br />
The second Parameter is an out param, it gives me a path to a file. If i do it with a StringBuilder or with a pointer to a buffer, it is the same result. In a Windows Forms Application, the buffer is NOT empty, in a Windows Service the buffer IS empty.. I try it on different machines and with different user accounts (Local System, User etc.). In a service it won't work.The com interface is marshalled correct.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
-- modified at 17:29 Monday 16th April, 2007
|
|
|
|
|
Hi,
StringBuilder is the right way to go when the function wants to return a string
value; it would be better to have a buffer size too in the argument list
(it would then be set to StringBuilder.Capacity), but if the function does not
take such an input, you should provide a sufficiently large StringBuilder.
For a single file spec, I believe 512 is the Windows maximum.
When I must call a native function that does not take a buffer size, I provide
a StringBuilder[1024].
I am afraid it is the other, the first, argument, that is causing your problems.
|
|
|
|
|
Hi,
no the first argument is not the problem. I'm testing with a StringBuilder with a capacity from up to 10240 to ensure there is enough space.. but it don't work. That makes me crazy. So, i got a little Form Application with one button, on button click event i call the function and it works. When i do it in a ConsoleApp or a service it will not work. I don't think that my code is ugly.. i think here is something wrong with the hole frame. Is ther probably not enough or to much time between call and passing the function? Or could it be a Thread Problem?
|
|
|
|