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 581409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:34:31+00:00 2026-05-13T14:34:31+00:00

I have a method private object SetGrid(IGrid grid) { grid.PagerHelper.SetPage(1, 10); grid.SortHelper.SetSort(SortOperator.Ascending); grid.PagerHelper.RecordsPerPage =

  • 0

I have a method

    private object SetGrid(IGrid grid)
    {
        grid.PagerHelper.SetPage(1, 10);
        grid.SortHelper.SetSort(SortOperator.Ascending);
        grid.PagerHelper.RecordsPerPage = 10;

        return grid;
    }

which returns an object of type object.

Then I cast the object back to the previous type.

    var projectModel = new ProjectModel();

    projektyModel = (ProjectModel)SetGrid(projectModel);

The gain of this is, the method SetGrid can be reused across the app.

Is this a common practice or should I avoid doing this ?

  • 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-05-13T14:34:32+00:00Added an answer on May 13, 2026 at 2:34 pm

    You could use a generic method instead, and constrain the type argument to your IGrid interface:

    private T SetGrid<T>(T grid) where T : IGrid
    {
        grid.PagerHelper.SetPage(1, 10);
        grid.SortHelper.SetSort(SortOperator.Ascending);
        grid.PagerHelper.RecordsPerPage = 10;
    
        return grid;
    }
    

    You should still be able to call the method in exactly the same way, just without the cast. Type inferencing should be capable of automagically figuring out the required generic type argument for you:

    var projectModel = new ProjectModel();
    projektyModel = SetGrid(projectModel);
    

    EDIT…

    As other answers have mentioned, if your IGrid objects are reference types then you don’t actually need to return anything at all from your method. If you pass a reference type then your method will update the original object, not a copy of it:

    var projectModel = new ProjectModel();  // assume that ProjectModel is a ref type
    projektyModel = SetGrid(projectModel);
    bool sameObject = object.ReferenceEquals(projectModel, projektyModel);  // true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the C# method private static string TypeNameLower(object o) { return o.GetType().Name.ToLower(); }
I have a method that returns an object: private object myObjectMethod(){ //... return myObject;
I have a method: private void DeletePuzzle(object param) { } param is a System.Windows.Controls.SelectedItemCollection
I have this method: private void listView1_DoubleClick(object sender, EventArgs e) { if (listView1.SelectedItems.Count >
If I have a method declared like this: private void someFunction(object[] param1) When I
Here's the problem: I have a CollectionChanged Handler Method: Private Sub LayersChanged(sender As Object,
I have declared the following method: private void mockInvokeDBHandler(Map<String, Object>... rows) { List<Map<String, Object>>
I have the following method: private static object[] GenerateParameters(MethodBase executingMethod) { var parameterInfoList =
I have a method private void textBoxPilot_TextChanged(object sender, TextChangedEventArgs e) { ... } where
i have this method: private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar ==

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.