Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:34:21+00:00 2026-06-17T10:34:21+00:00

I have a Rectangle property in my class, which I’d like to make accept

  • 0

I have a Rectangle property in my class, which I’d like to make accept Width and Height correctly, and also return them, as if I was dealing with a normal Rectangle.

Vector2 position;
Rectangle rectangle;
public Rectangle Rect
    {
        get { return rectangle;  }
        set { rectangle = value; position.X = value.X; position.Y = value.Y; }
    }

Seems fine, works great. Except for when you want to get or set Width or X specifically.

How do I make that possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:34:22+00:00Added an answer on June 17, 2026 at 10:34 am

    If the semantics of your class are such that you can be 100% certain that you’re never going to care when outsiders adjust Rect, just expose it as a field and callers will be able to set its fields directly. If you cannot make that guarantee, then you might consider offering a method which passes rectangle to a callback method:

    delegate void ActionByRef<T1>(ref T1 p1, ref T2 p2);
    delegate void ActionByRef<T1,T2>(ref T1 p1, ref T2 p2);
    delegate void ActionByRef<T1,T2,T3>(ref T1 p1, ref T2 p2, ref T3 p3);
    void ActOnRect(ActionByRef<Rectangle> proc)
      { proc(ref rectangle); position.X = value.X; position.Y = value.Y; }
    void ActOnRect<TP1>(ActionByRef<Rectangle,TP1> proc, ref TP1 p1)
      { proc(ref rectangle, ref p1); position.X = value.X; position.Y = value.Y; }
    void ActOnRect<TP1,TP2>(ActionByRef<Rectangle,TP1> proc, ref TP1 p1, ref TP2 p2)
      { proc(ref rectangle, ref p1, ref p2); position.X = value.X; position.Y = value.Y; }
    

    That approach will avoid anyone having to make a copy of rectangle just to change one of its members. Probably not worth it with a 16-byte structure, but maybe worthwhile if a larger structure is necessary.

    A third approach would be to simply require your callers to do something like:

    var r = myThing.Rect;
    r.X = 23;
    myThing.Rect = r; // Or perhaps myThing.SetRect(R);
    

    The final approach I’d suggest would be to follow the pattern of Control.SetBounds, which includes an overload that takes the Rectangle members as separate parameters, but takes an additional parameter which specifies which parameter or parameters should be copied to the appropriate members of Bounds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rectangle class which has properties width and height. It also has
I have a rectangle which has known width and height.Now devide rectangle diagonally to
I have one rectangle. The width of the rectangle is deciding based on the
I have a rectangle that I've created and set its individual properties like so
I have defined a rectangle drawable like this: <shape xmlns:android=http://schemas.android.com/apk/res/android android:shape=rectangle> <corners android:radius=4dip/> <solid
I have created a Rectangle inside of a ScrollViewer like this <ScrollViewer ManipulationMode=Control x:Name=songScrollViewer
I have a INotifyPropertyChanged derived class with a Volume property (float, 0.0f - 1.0f)
I have a problem with a class which extends ListBox in Windows Phone 7
I've written a wrapper class around RaphaelElement. It has a property elem which stores
Let's say in some abstract ViewModel base-class I have a plain-old property as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.