My own datatype

How to define my datatype?

Page 1 of 1

11 Replies - 1627 Views - Last Post: 14 December 2010 - 12:11 PM Rate Topic: -----

#1 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

My own datatype

Posted 14 December 2010 - 03:09 AM

Hi!

I'm using a float in several places to represent speed. Now I'd like to do somthing like:
SpeedType currentSpeed = 0;

instead of
float currentSpeed = 0;

since I'm defining different speeds throughout my program. If I need to use int or doulble for speed instead I don't want to have to find all speed defintions and redefine them. Instead I'd like to change the definition of my SpeedType in one place.

How do I define my SpeedType as <someSimpleType>?

Regards
Jens

This post has been edited by jens: 14 December 2010 - 03:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: My own datatype

#2 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 181
  • Joined: 25-October 10

Re: My own datatype

Posted 14 December 2010 - 04:34 AM

Write a class named SpeedType and give him a property to set the CurrentSpeed.

public class SpeedType {
  public float CurrentSpeed { get; set; }
}


Was This Post Helpful? 0
  • +
  • -

#3 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: My own datatype

Posted 14 December 2010 - 05:10 AM

Quote

public class SpeedType {
  public float CurrentSpeed { get; set; }
}



That's of course possible. But then I'd have to instansiate an object every time I wanted to use the variable e.g. I'd have to do
SpeedType mySpeed = new SpeedType();
mySpeed.CurrentSpeed = 2;
...
otherSpeed = mySpeed.CurrentSpeed;


instead of
SpeedDefintion CurrentSpeed;
CurrentSpeed = 2;
...
otherSpeed = CurrentSpeed;

which I find a bit disturbing.

Besides, that's not how I want to do it. :P

Regards
Jens

This post has been edited by jens: 14 December 2010 - 05:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: My own datatype

Posted 14 December 2010 - 05:50 AM

You need to create your own value type. A reference type needs the "new" declaration. Seems that you don't want to do that, so this should help you.

http://www.stephench...value-type.html
Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: My own datatype

Posted 14 December 2010 - 07:12 AM

You know, float isn't really float at all! It actually an alias to System.Single. If you were to find the .NET declaration hidden somewhere, it would look like:
using float = System.Single;



You may do the same:
using SpeedType = System.Single;
//...
SpeedType currentSpeed = 0;



However, simply knowing that float is truly a struct should convince you not to worry so much about it.
Was This Post Helpful? 4
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4452
  • View blog
  • Posts: 7,753
  • Joined: 08-June 10

Re: My own datatype

Posted 14 December 2010 - 07:45 AM

Quote

However, simply knowing that float is truly a struct should convince you not to worry so much about it.


I don't think that was his issue. I think that he was worried that if he had to change every speed from a single to a double, he would be able to do it from one place.

Baavgai's way is pretty much the only way to do this, and you'll have to do it at the top of every page you want to use that alias for.

I'd personally worry about it less too. I always use doubles. Unless my application somehow has a massive memory footprint, it's really not going to matter. Same for ints. Even if I know my int is going to be a small one, I don't use byte or short, just because int is convenient.
Was This Post Helpful? 3
  • +
  • -

#7 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: My own datatype

Posted 14 December 2010 - 08:11 AM

Thank you.

This is partially on the way, however, some things I'm used to be able to do to simple types like float does not work. The initialization however is fantastic.

(Anyone care to explain HOW the ErrorType class works?????)

So, even though it's looking better it's not complete... More ideas? :santa:

using System;
using System.Windows.Forms;

namespace Special
{
    public partial class Form1 : Form
    {
        ErrorType initialError;
        public Form1()
        { InitializeComponent(); }

        private void Form1_Load(object sender, EventArgs e)
        {
            ErrorType newError = 2;
            initialError = 0;

            for (int i = 0; i < 9; i++)
            {
                initialError = ((float)i/10);

                //Does not work
                //newError = newError + initialError;

                //Does not work
                //initialError++;

                MessageBox.Show(initialError.ToString());
            }
        }
    }

    public class ErrorType
    {
        private readonly float _error;
        private ErrorType(float error)
        { _error = error; }

        public static implicit operator ErrorType(float error)
        { return new ErrorType(error); }

        public override string ToString()
        { return _error.ToString(); }
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: My own datatype

Posted 14 December 2010 - 08:17 AM

Thank you guys! I knew someone would come up with an answer and a correct analysis of my problem.

As usual I'm humbled.

However, could I not put this using SpeedType = System.Single; in a file with other usings?

Would it seem strange to do using SpeedType = float;
EDIT: Yes, since it won't compile. :)

...and finally... I'm talking to a few PLC systems that are not yet in place so I don't know how they will represent numbers at all. This meaning that my scenario of having to change the type that represents numbers in many many places will probably come true.

Regards
Jens

This post has been edited by jens: 14 December 2010 - 09:12 AM

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4452
  • View blog
  • Posts: 7,753
  • Joined: 08-June 10

Re: My own datatype

Posted 14 December 2010 - 08:29 AM

@Jens

In response to post #7...

Quote

Anyone care to explain HOW the ErrorType class works?????


Read this:
http://msdn.microsof...y/z5z9kes2.aspx

It's called "overloading the implicit operator." In C#, you can overload operators, to make them do things other than what they do by default. However, one thing you may not know is that both explicit and implicit casts are considered operators. So you can overload these operators to allow a type to be either explicitly or implicitly cast to another type.

In this case, you've only provided a way for a float to be treated as an ErrorType. But you haven't provided a way for an ErrorType to be cast as a float. Here's a better example of what you want to do:

public class SpeedType
{
    private float f;
    private SpeedType(float f) { this.f = f; }

    public static implicit operator float(SpeedType s) { return s.f; }
    public static implicit operator SpeedType(float f) { return new SpeedType(f); }
}


And an example of using it:
SpeedType st = 5.5f;
SpeedType st2 = 5f;
st++;
Console.WriteLine(st + st2);

Was This Post Helpful? 1
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4452
  • View blog
  • Posts: 7,753
  • Joined: 08-June 10

Re: My own datatype

Posted 14 December 2010 - 09:05 AM

Quote

However, could I not put this using SpeedType = System.Single; in a file with other usings?


Using directives are always scoped to a single file. You can't cascade them or share them across multiple files.

Quote

Would it seem strange to do using SpeedType = float;


Only strange in that it would not compile. You can't alias a keyword.
Was This Post Helpful? 2
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5465
  • View blog
  • Posts: 11,740
  • Joined: 02-June 10

Re: My own datatype

Posted 14 December 2010 - 10:37 AM

View Postjens, on 14 December 2010 - 02:09 AM, said:

Hi!

I'm using a float in several places to represent speed. Now I'd like to do somthing like:
SpeedType currentSpeed = 0;

instead of
float currentSpeed = 0;

since I'm defining different speeds throughout my program. If I need to use int or doulble for speed instead I don't want to have to find all speed defintions and redefine them. Instead I'd like to change the definition of my SpeedType in one place.

How do I define my SpeedType as <someSimpleType>?

Regards
Jens


I guess I read this question differently than everyone else. The key point I saw is that you have definitions for your speeds.
I don't want to have to find all speed defintions and redefine them.

Even though you are calling it 'speeds' this really makes it sound like you have set values of 10,20,30 etc. and not an infinite number of possibilities. In that case I would think an enum would serve you well

public enum Speeds
{
    STOPPED = 0;
    SLOWEST = 10;
    SLOWER  = 20;
    SLOW    = 30;
    MEDIUM  = 40;
    FAST    = 50;
    FASTER  = 60;
    FASTEST = 70;
}


Now you are constrained to your definitions and can easily just use it like this


Speeds CurrentSpeed = Speeds.STOPPED;

// Do some other stuff
// Change your speed
CurrentSpeed = Speeds.FAST;

Was This Post Helpful? 1
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4452
  • View blog
  • Posts: 7,753
  • Joined: 08-June 10

Re: My own datatype

Posted 14 December 2010 - 12:11 PM

See, I don't think that was the direction he was going:

Quote

If I need to use int or doulble for speed instead I don't want to have to find all speed defintions and redefine them.


I think it was more "I'm not sure which data type I'm going to have to use, so I'd rather use my own so I can change what I'm obfuscating."

But if you do have a defined set of constant speeds like that, an Enum is perfect. A larger set, and perhaps a data file like XML would be better.

This post has been edited by insertAlias: 14 December 2010 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1