|
Hello,
Looking for some help in case of generics comparison:
I'm using a derived Type inside a Generic. This would be generated at run time, and so, I have to compare only the object type as shown below (in the IF Condition):
The Project Structure is as shown below:
class ClassType
{
}
class IByte:ClassType
{
}
class IInt:ClassType
{
}
class Class1<T> where T:ClassTpe
{
}
public class MainClass
{
public MainClass()
{
Class1<IByte> str = new Class1<IByte>();
Class1<IInt> insd = new Class1<IInt>();
if (insd is Class1) //NEED HELP TO IMPLEMENT THIS
{
}
}
}
-- modified 21-May-13 5:50am.
|
|
|
|
|
Try if (insd.GetType() is Class1.GetType())
|
|
|
|
|
Hello Abhinav
the point is we cannot use "Class1.getType()" It asks for the generic type like.. we must specify - Class1<type>.getType(0 instead. This is the problem I'm struck.
|
|
|
|
|
I can think of two ways to solve this problem. In the first the generic type definition is extracted from the object (see GenericTypeDefinition method in the example) and tested against actual definition e.g. typeof(Class1<>) .
In the second, which I prefer (see Class2 in the example), the generic class must inherit a non generic abstract base class. Any Class2<T> may then be tested against the base class using the is operator.
namespace GenericTypeTest {
class Class1<T> { }
abstract class BaseClass { }
class Class2<T> : BaseClass { }
internal class App {
static void Main() {
IsClass1(new Class1<int>());
IsClass1(new Class1<string>());
IsClass1(new Class2<string>());
IsClass1(42);
IsClass1("Tiger");
Class2<string> strObj = new Class2<string>();
if (strObj is BaseClass) {
Console.WriteLine("{0} is derived from BaseClass", strObj);
}
Console.ReadLine();
}
static void IsClass1(Object obj) {
if (GenericTypeDefinition(obj) == typeof(Class1<>)) {
Console.WriteLine("{0} was constructed from {1}", obj.GetType(), typeof(Class1<>));
} else {
Console.WriteLine("{0} was NOT constructed from {1}", obj.GetType(), typeof(Class1<>));
}
Console.WriteLine();
}
static Type GenericTypeDefinition(Object obj) {
Type result = null;
Type test = obj.GetType();
if (test.IsGenericType && !test.IsGenericTypeDefinition) {
result = test.GetGenericTypeDefinition();
}
return result;
}
}
}
|
|
|
|
|
Thanks a lot Alan, This approach has resolved my issue!
|
|
|
|
|
You can do this:
if (insd.GetType() is Class1<IByte>) {} But, using a temporary variable and the "as" operator is more performant than using "is," and will not throw an error:
Class1<IInt> testObject = insd as Class1<IInt>;
if (testObject != null) Console.WriteLine("it's Class1<int>"); Is your goal here to test an "unknown" object's type at run-time, and then take action based on the underlying type? If so, there are other strategies you can use.
yours, Bill
“Humans are amphibians: half spirit, half animal; as spirits they belong to the eternal world; as animals they inhabit time. While their spirit can be directed to an eternal object, their bodies, passions, and imagination are in continual change, for to be in time, means to change. Their nearest approach to constancy is undulation: repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis
|
|
|
|
|
Hi All,
I am trying to create a function that passes the cursor's starting and ending x,y coordinates as parameters. For instance I would like to set the cursor's starting x,y coordinate when the user clicks the left mouse button. Then as the user holds down the left mouse button and drags the cursor to a desired location, the ending x,y coordinate is dynamically set and updated. The final ending coordinate is set when the left button is released. I am currently working with the following code:
private void Cursor_Coord(out Cursor C1, out Cursor C2)
{
Cursor C1;
Cursor C2;
if(Mouse.LeftButton == MouseButtonState.Pressed)
{
C1 = new Cursor(Cursor.Current.Handle);
C1.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
}
if(Mouse.LeftButton == MouseButtonState.Released)
{
C2 = new Cursor(Cursor.Current.Handle);
C2.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
}
}
I am planning to send the changing cursor coordinates to the Drawline method to be used as the first and second point. The effect I have in mind is that the line is drawn as the mouse is in motion. The problem is it's not working. Any help will be greatly appreciated, thanks in advance.
modified 21-May-13 4:02am.
|
|
|
|
|
You don't need to use a cursor to do this. As you've rightly noted in your code, the part you are actually interested in is the Position - that's all you need to keep track of.
|
|
|
|
|
Hi, you've replied to my post as I was in the process of making changes to it.
|
|
|
|
|
This is pretty easy to do, but I think you are trying to do it the wrong way. You don't say what your environment is, but I'll assume WinForms.
First off, you probably don't need to use Cursor at all for this, a simple Point is enough.
Try doing this by adding a few class level variables, and handling a few events:
private Point pointStart;
private Point pointEnd;
private bool drawing = false;
private void frmMain_MouseDown(object sender, MouseEventArgs e)
{
pointStart = e.Location;
pointEnd = pointStart;
drawing = true;
Invalidate();
}
private void frmMain_MouseUp(object sender, MouseEventArgs e)
{
drawing = false;
}
private void frmMain_MouseMove(object sender, MouseEventArgs e)
{
if (drawing)
{
pointEnd = e.Location;
Invalidate();
}
}
private void frmMain_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Blue, pointStart, pointEnd);
}
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Hi, thanks for your reply. I have used your code but VS2010 is complaining that the Invalidate method does not exist in the current context. I assumed that Invalidate is a built-in function of C# but it doesn't appear to be the case.
modified 21-May-13 22:59pm.
|
|
|
|
|
|
At a guess, you have added the word "static " in there somewhere - static methods cannot access non-static class methods, fields or properties.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Hi, I need the coding for multiple object selection in propertygrid.
Please reply as soon as possible
|
|
|
|
|
vvino2020 wrote: Hi, I need the coding for multiple object selection in propertygrid. We "help" with coding-questions; if you need someone to write code for you, then this is the wrong place.
vvino2020 wrote: Please reply as soon as possible I wouldn't "wait" with the answer just for the fun of it, and this question isn't more important than the other questions.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Have a play with the MultiSelect property on the grid
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
Assign an array of objects to PropertyGrid's SelectedObjects[^] property. Note the plural-s.
PropertyGrid will then sort out what properties to display and what values to display for shown properties in the usual way.
Ciao,
luker
|
|
|
|
|
Hi,
I am trying to use Modern UI Charts for my Windows Store app but I am not able to get it working.
A similar issue was logged by another person here along with the code
Can anyone please help??
|
|
|
|
|
Does this third party support WIndows 8? Check if they have documentation that tells you what versions they support.
|
|
|
|
|
Yes it does.. Clearly stated in the documentation
|
|
|
|
|
Raghavendra Reddy C wrote: Can anyone please help??
Given how little information you've given us, no. There's no source for us to view, and there's precious little detail about not being able to get it working in there.
|
|
|
|
|
|
|
|
Raghavendra Reddy C wrote: A similar issue was logged by another person here along with the code And also a link to the solution.
Use the best guess
|
|
|
|