8 Replies - 897 Views - Last Post: 06 May 2014 - 05:54 AM Rate Topic: -----

#1 johnOnline   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-May 14

Method with class group argument

Posted 03 May 2014 - 07:02 AM

Hi guys.
I have a set of point classes that derive from abstract POINT.
I would like the end user to be able to define a point eg pointndimention : POINT.

it would be nice for the static method random to be able to know which kind of point to randomize by defining a type that implements POINT without being able to pass any other type at all and without passing an object of type POINT.
eg.

    POINT12D p=new POINT12D;
    p=POINT.random(POINT12D);



public abstract class POINT
	{
		public abstract POINT _Add(POINT a,POINT B)/>/>;

		...

		public abstract POINT _Random();
		public abstract POINT _Norm(POINT p);
		public abstract POINT _Inverse(POINT p);
		public abstract POINT _Mean(POINT[] p);[/indent]
		
		public static POINT Add(POINT a, POINT B)/>/>
		{
			return a._Add(a,B)/>/>;
		}
		public static POINT Minus(POINT a, POINT B)/>/>
		{
			return a._Minus(a,B)/>/>;
		}

		...

		public static POINT Random(POINT Type t)
		{
			return typeof(t).Random();//<-----A BIT LIKE THIS
		}

		...
 }

        Point2d: POINT
        {
                ...
                public override POINT _Random()
		{
			return new point2d(r.next(),r.next());
		}
}




I have goggled alot and keep coming across generic classes but not what Im trying to do.
Any help would be very appreciated.
Thankyou

Is This A Good Question/Topic? 0
  • +

Replies To: Method with class group argument

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Method with class group argument

Posted 03 May 2014 - 09:45 AM

Frankly I'm not sure I follow what you have here. Meaning I'm not sure if I'm confused or if you are.

Your point constructor takes two points as arguments? Then how do you ever make the points that are the arguments for making a point? Do you see my point?

Also... I suggest you stay away from reusing existing keywords. Point and Random are already defined in C#. Reusing those terms for your own constructs while legal is going to cause you and the compiler some confusion down the line.

I'm lost in the idea of types of points. A point is a point. It has an x and y value. And a z if its in 3d space. If you want to make a randomly generated point then you assign randomly generated x,y,z values. What is a 'mean point'? One with an angry look on its a face? You could get the mean from a group of points, but that would be done by calculation not assigning a point to be the mean.

I guess I'm just really not following your plan/intent/thinking? Anyone else?
Was This Post Helpful? 0
  • +
  • -

#3 johnOnline   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-May 14

Re: Method with class group argument

Posted 03 May 2014 - 10:10 AM

Right. The POINT is an abstract class and i will be working with n dimensional maths, so i've created an interface where i can perform math on a point of any dimension. i will be creating a matrix class where i can work with points. so if i create a 15 dimensional point it makes no scene to have to add to my matrix classes to be able to deal with them when not every project using the POINT will need the 15,16 dimensional matrix implementation. I have no constructors in the abstract class as the derived point classes will implement there own. i will also have a COMPLEX which will be derived from point and higher order complex numbers.
The reason is to save on memory and complexity by making the classes reusable for 1D 2D 3D 4D......nD and complex numbers. i don't want to have to add implementation for every n within my matrix implementation as it would be easyer to write scalable classes for the long run.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Method with class group argument

Posted 03 May 2014 - 10:16 AM

Sorry... The math is over my head. I can code what I can envision and understand. But I can't envision what you're doing. I'm sure someone here will get it though.
Was This Post Helpful? 0
  • +
  • -

#5 johnOnline   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-May 14

Re: Method with class group argument

Posted 03 May 2014 - 10:30 AM

Actualy the main question is can i create a method that takes a TYPE as an argument and then be able to access a static method of that type from within the static method eg
POINT foo(type t)
{
   return new (t)object.fee()
}



it is important though that the type be a derived type from POINT

sory not access a static method but foo would be the static method that can deal will all derived types and access its derived fee method

This post has been edited by tlhIn`toq: 03 May 2014 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

#6 johnOnline   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-May 14

Re: Method with class group argument

Posted 03 May 2014 - 10:50 AM

The best way i can figure out is
POINT foo (POINT P)
{
  return p.fee();
}

// and use like

Point5D p1= POINT.foo(new Point5D P());




which would take a little more time to run that just passing a type. and these methods and others like it will be used very often in simulations that will take days to complete so im trying to trim as much time as possible
Was This Post Helpful? 0
  • +
  • -

#7 johnOnline   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-May 14

Re: Method with class group argument

Posted 03 May 2014 - 11:20 AM

unless someone can find a faster way im going to do it like this

POINT foo (POINT P)
	{
	  return p.fee();
	}
	 
	// and use like
	 
	Point5D p1= POINT.foo(p1);



its hard to explain why i want to avoid just going
Point5D p1 new Point5D();
p1=p1.fee();



but i wanted to implement other stuff that i haven't quite worked out yet as the classes I'm working on are not for 1 type of project. eg AI, evolution, learning and fractals. some projects i have planned will generate code and compile on the fly so i wanted to simplify things to avoid complicated code correction algorithms.

Thanks all that took time to read my question and if anyone has anything interesting along these lines, eg fractals and simulated abstract life/learning, i would be interested to see your results.
Was This Post Helpful? 0
  • +
  • -

#8 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Method with class group argument

Posted 03 May 2014 - 02:00 PM

You could try something like
POINT CreatePoint(Type t) {
    return (POINT)Activator.CreateInstance(t);
}


This will throw an exception if you try to pass anything that can't be cast into a POINT.

This post has been edited by Momerath: 03 May 2014 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#9 Charles:)   User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: Method with class group argument

Posted 06 May 2014 - 05:54 AM

I'm not really sure if I've understood what you want to do, but does this help?

namespace PointTest
{
    public abstract class PointBase<T> where T : PointBase<T>, new()
    {
        public abstract T Random();

        public static T RandomPoint()
        {
            return new T().Random();
        }
    }

    public class Point2D : PointBase<Point2D>
    {
        public override Point2D Random()
        {
            return new Point2D();
        }
    }

    public class Point3D : PointBase<Point3D>
    {
        public override Point3D Random()
        {
            return new Point3D();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var p2d = PointBase<Point2D>.RandomPoint();
            var p3d = PointBase<Point3D>.RandomPoint();
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1