0

I have a service with a function public bool UpdateSubClient(ref SubClient subClient) which updates the database.

Subclient model is as follow

public string SubClientName { get; set; }
public string BtnText { get; set;}
public string HdrText { get; set; }
public string ReturnURL { get; set; }

I am trying to call the the UpdateSubclient as follows

SubClientService subclientservice = new SubClientService();
SubClient currentsubclient = new SubClient();
currentsubclient.SubClientName = form["SubClientName"].ToString();
currentsubclient.ClientID = Convert.ToInt32(form["ClientID"]);
currentsubclient.BtnText = form["BtnText"].ToString();
currentsubclient.HdrText = form["HdrText"].ToString();
currentsubclient.ReturnURL = form["ReturnURL"].ToString();
bool success = subclientservice.UpdateSubClient(currentsubclient);

I am getting invalid argument error and couldn't figure out the way to pass reference. Any help will be appreciated.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Dinesh Devkota
  • 1,417
  • 2
  • 18
  • 45
  • **What does the error say**? – SLaks Apr 19 '15 at 02:45
  • @Slaks the error is `overloaded method has invalid argument`. – Dinesh Devkota Apr 19 '15 at 02:46
  • What is the declaration of SubClientService.UpdateSubClient? Is that the line that is throwing the error? Nevermind, see it at the top, you need to pass by reference, like subclientservice.UpdateSubClient(ref currentsubclient); – Ron Beyer Apr 19 '15 at 02:47
  • By the way, if you made the UpdateSubClient method, there really isn't a need for a "ref" declaration unless you intend to change the reference (create a new instance) of the type being passed in. You can modify the fields all you want and the changes will persist without the ref keyword. – Ron Beyer Apr 19 '15 at 02:51
  • @Rob Beyer the last line ` bool success = subclientservice.UpdateSubClient(currentsubclient);` is throwing error. – Dinesh Devkota Apr 19 '15 at 02:51
  • @DineshDevkota you need to include the `ref` keyword to match the method declaration. I would suggest reading this, though: http://stackoverflow.com/questions/635915/when-to-use-ref-and-when-it-is-not-necessary-in-c-sharp – Tieson T. Apr 19 '15 at 02:54
  • @Rob Beyer the class in which that function is, extends an interface. I really cannot use that hack to make it work. – Dinesh Devkota Apr 19 '15 at 02:55
  • 1
    Are you sure you *need* `ref`? – user2864740 Apr 19 '15 at 02:55
  • @user2864740 as I explained in comment before "the class in which that function is, extends an interface" I cannot really change it to make it work that way. – Dinesh Devkota Apr 19 '15 at 03:00
  • And `bool success = subclientservice.UpdateSubClient(ref currentsubclient)` doesn't "just work"? – user2864740 Apr 19 '15 at 03:15
  • I've removed the other keywords because there is nothing in the question and uses an ASP/MVC feature. – user2864740 Apr 19 '15 at 03:16

1 Answers1

0

I made a change request to other backend developers and changed the

public bool UpdateSubClient(ref SubClient subClient)

to

public bool UpdateSubClient(SubClient subClient)

to avoid any complications. Thanks @user2864740 insisting me to research on usages of ref as parameter.

Dinesh Devkota
  • 1,417
  • 2
  • 18
  • 45