-5

I am trying out this example problem where I have to make two implicit conversion operators to create a Doggy class from a Human and vice versa. The classes need to take into fact that a human year is 7 dog years. I was told that if going from Dog years to human years to make sure the type of age is still an int (of course) and that it may require some explicit converting. I don't know how to define DogToHuman.Name, DogToHuman.Age, HumanToDog.Name and HumanToDog.Age. I am kind of new to programming so I am not used to this format. Any number to start with would work, Like human age of 25.

class Human
{
    public string Name;
    public int Age;
    public Human(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

class Doggy
{
    public string Name;
    public int Age;
    public Doggy(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

class ConversionQuestion
{
    static void Main(string[] args)
    {
        Console.WriteLine("As a Human, {0} would be {1}", DogToHuman.Name, DogToHuman.Age);
        Console.WriteLine("As a Dog, {0} would be {1}", HumanToDog.Name, HumanToDog.Age);
        Console.ReadLine();
    }
}

Sorry about any confusion, I wasn't just fishing for an answer, and was more looking for how to go about it.

Shiro
  • 5
  • 1
  • 5
  • That's where I'm stuck. where could I place the DogToHuman or HumanToDog so that they may be defined? Would it be in another class? – Shiro Apr 23 '14 at 22:38
  • There are many ways of approaching this - you need to try doing it yourself and then ask if you have problems with your code. – Ant P Apr 23 '14 at 22:38

3 Answers3

1

You almost answered the question yourself in your question. If you search for ".net implicit conversion" you'll turn up this page (http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx) that shows you how to create implicit type conversions. Based on that, I modified your classes as follows:

class Human
{
    public string Name;
    public int Age;

    public Human( string name, int age )
    {
        Name = name;
        Age = age;
    }

    public static implicit operator Doggy( Human human )
    {
        return new Doggy( human.Name, human.Age / 7 );
    }
}

class Doggy
{
    public string Name;
    public int Age;

    public Doggy( string name, int age )
    {
        Name = name;
        Age = age;
    }

    public static implicit operator Human( Doggy dog )
    {
        return new Human( dog.Name, dog.Age * 7 );
    }
}

Then you can use it like this:

Human human = new Human( "Fred", 42 );
Doggy dog = human;

Console.WriteLine( "As a Human, {0} would be {1}", human.Name, human.Age );
Console.WriteLine( "As a Dog, {0} would be {1}", dog.Name, dog.Age );
Console.ReadLine();

The whole point is that the conversion is implicit, you just need to assign one type to the other and the conversion happens magically.

Craig W.
  • 17,838
  • 6
  • 49
  • 82
  • Magical conversions aren't always a good thing. Even in this example I don't think it is - there's too much implied knowledge of the code to understand whats going on in that second snippet. If I didn't write this (or read your answer) .. that second snippet would make no sense at a glance - which is bad. – Simon Whitehead Apr 23 '14 at 22:50
  • I think the whole assignment is stupid, but I didn't make up the requirements. If the requirement is to use an implicit conversion, that's the way it's done . – Craig W. Apr 23 '14 at 22:58
  • The requirement is actually `explicit` in the OP's question. – Simon Whitehead Apr 23 '14 at 23:13
  • Nope, the first sentence of his post clearly says, "I have to make two **implicit** conversion operators." He used the `explicit-conversion` tag, but the question stated otherwise. In the post he also mentions, "may require some explicit converting" but in that context he's talking about making sure the age remains an `int` so it could be inferred he's simply talking about datatype converting at that point. Please don't assume I'm an illiterate moron just because I don't have eighty quadrillion points. – Craig W. Apr 24 '14 at 01:32
  • Calm down.. I am clearly the illiterate moron since I read the question wrong. You could have just stated that I was indeed wrong (which I happily accept) without assuming a random on the internet was attacking you. – Simon Whitehead Apr 24 '14 at 01:49
1

Change your ConversionQuestion class to something like this:

class ConversionQuestion
{
    static void Main(string[] args)
    {
        Doggy dog = new Doggy("Max", 3);
        Human human = new Human("Leonardo", 23);

        Console.WriteLine("As a Human, {0} would be {1}", DogToHuman(dog).Name, DogToHuman(dog).Age);
        Console.WriteLine("As a Dog, {0} would be {1}", HumanToDog(human).Name, HumanToDog(human).Age);
        Console.ReadLine();
    }

    public static Human DogToHuman(Doggy dog)
    {
        return new Human(dog.Name, (dog.Age * 7));
    }

    public static Doggy HumanToDog(Human human)
    {
        return new Doggy(human.Name, (human.Age / 7));
    }
}
msmolcic
  • 6,407
  • 8
  • 32
  • 56
0

I'm not completely clear on what you're asking, but it sounds like you just want an approach on how to instantiate your classes and pass them to some methods that can convert their ages.

Without writing all the code for you, here's one approach you could use. Modify it as necessary...

class ConversionQuestion
{
    static void Main(string[] args)
    {
        Human human = ... // create an instance of Human, supply name and age
        Doggy doggy = ... // create an instance of Doggy, supply name and age

        Console.WriteLine("As a Human, {0} would be {1}", doggy.Name, DogToHumanConversion(doggy.Age));
        Console.WriteLine("As a Dog, {0} would be {1}", human.Name, HumanToDogConversion(human.Age));

        Console.ReadLine();
    }    

    private int DogToHumanConversion(int dogAge)
    {
        // convert dog age to human

        // return human age
    }

    private int HumanToDogConversion(int humanAge)
    {
        // convert human age to dog

        // return dog age
    }
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Sorry about that, like I said i'm new to the programming scene and will make my future questions/concerns more descriptive/explained well. – Shiro Apr 23 '14 at 23:02