2

This method supposed to print the following:

Creating r1
-> Assigning r2 to r1
-> Changing values of r2
String = This is new info!, Top = 10, Bottom = 50, Left = 10, Right = 50
String = This is new info!, Top = 10, Bottom = 4444, Left = 10, Right = 50

but instead of printing string value it prints the class in which the string is located

Creating r1
-> Assigning r2 to r1
-> Changing values of r2
String = Csharp_Projects.Program+ShapeInfo, Top = 10, Bottom = 50, Left =  10, Right = 50
String = Csharp_Projects.Program+ShapeInfo, Top = 10, Bottom = 4444, Left = 10, Right = 50

this is the code:

 using System;

 namespace Csharp_Projects
{
  static class Program
{
    static void Main(string[] args)
    {
        ShapeInfo.Rectangle.ValueTypeContainingRefType();
   }

   // this class which takes an string as parameter

    public class ShapeInfo
    {
        public string infoString;

        public ShapeInfo(string info)
        {
            infoString = info;
        }

      // this struct has two fields, one is int type  and the other is ShapeInfo (above) and a constructor which set the values

        public struct Rectangle
        {
            public ShapeInfo rectInfo;

            public int recTop, rectleft, rectBottom, rectRight;

            public Rectangle(string info, int top, int left, int Buttom, int Right)
            {
                rectInfo = new ShapeInfo(info);
                recTop = top;
                rectBottom = Buttom;
                rectRight = Right;
                rectleft = left;
            }
            // this method print the results
            public void Display()
            {
                Console.WriteLine("string={0},top={1},Bottom={2},"+"left={3},Right={4}",rectInfo,recTop,rectBottom,rectRight,rectleft);
            }

            // this method make an object and assign it to second variable and then change the values of second variable .

            public static void ValueTypeContainingRefType()
            {
                Console.WriteLine("Creating r1");
                Rectangle r1 = new Rectangle("First Rec", 10, 10, 50, 50);
                Console.WriteLine("Assigning r2 to r1");
                Rectangle r2 = r1;
                Console.WriteLine("Change Values of r2");
                r2.rectInfo.infoString = "This is new info!";
                r2.rectBottom = 4444;
                r1.Display();
                r2.Display();
            }


        }
    }
}
  } 

I really couldn't make out why it happened maybe my knowledge about C# system is not enough, what cause this?

2 Answers 2

2

You don't override .ToString() in your ShapeInfo class, so the system has no way of knowing how you want to print an instance of that class as a string. By default all objects print their class name (since that's pretty much the only thing every class is guaranteed to have).

Just override the method:

public override string ToString()
{
    return infoString;
}
Sign up to request clarification or add additional context in comments.

3 Comments

which method should take override? because it gives me an error . and btw I inserted the code from very famous book why it didn't mention it there? I mean he got the first print without using override
@Mohsen: I can't really help with an error you haven't described. As for your "very famous book", I also can't comment on code I haven't seen.
thanks anyway for your time. it seems I should study the override concept.
1

You need to override the ToString method in ShapeInfo to return the infoString.

public override string ToString()
{
    return infoString;
}

Or change the your DisplayMethod to use rectInfo.infoString instead of rectInfo

        public void Display()
        {
            Console.WriteLine("string={0},top={1},Bottom={2},"+"left={3},Right={4}",rectInfo.infoString,recTop,rectBottom,rectRight,rectleft);
        }

1 Comment

Thanks, I notice now that in the book in mentioned your second solution, I just forgot infoString. oh... it was not that hard then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.