blah blah blah is here! blah blah » Close

up0down
link

I'm trying to create a custom data type for a project I'm working on. The idea is that the datatype will be a "serial" number that looks something like: 0x010000305 ...you get the idea.

I want to be able to say Serial mySer = 0x010000305; Not sure how to do this. Help?

last answered 2 years ago

1 answers

link

This should get you started:

using System;

class Test
{
static void Main()
{
Serial mySer = 0x010000305;
Console.WriteLine(mySer);
Console.ReadKey();
}
}

public struct Serial
{
uint number;

public uint Number
{
get {return number;}
}

public Serial(uint number)
{
this.number = number;
}

public override string ToString()
{
return "0x" + number.ToString("X");
}

public static implicit operator Serial (uint i)
{
return new Serial(i);
}

}

Serial mySer = 0x012345; prints: 0x12345

vulpes
17279

There's no difference between 0x012345 and 0x12345 - they're the same hex number. However, if you want to always print the number with a certain minimum number of digits (say 6), zero-filled to the left, then change "X" to "X6".

Feedback