Passing a Property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bill

    Passing a Property

    Hi

    I'd like to pass a property (say like a row value from a typed dataset) to a
    method so that the method can get/set the property. for instance

    DoSomething(ATy pedRow.StringFi eld)

    where "StringFiel d" is a string property on a typed row.

    however, DoSomething requires a string argument in this case

    private void DoSomething(str ing aField)
    {
    aField = "new stuff"; // throws exception
    }

    Any ideas?

    thanks

    bill


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Passing a Property

    Bill,

    Since a property is in essence a method that is called, you need to pass
    the object that exposes the property and then set the property from inside
    the method.

    I think that a better solution would be to have the method return the
    value somehow (returning, or through a ref/out parameter), and then set the
    property outside of the method.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "bill" <sutphinwb@nosp am.nospam> wrote in message
    news:e8i8DLLeEH A.1644@tk2msftn gp13.phx.gbl...[color=blue]
    > Hi
    >
    > I'd like to pass a property (say like a row value from a typed dataset) to[/color]
    a[color=blue]
    > method so that the method can get/set the property. for instance
    >
    > DoSomething(ATy pedRow.StringFi eld)
    >
    > where "StringFiel d" is a string property on a typed row.
    >
    > however, DoSomething requires a string argument in this case
    >
    > private void DoSomething(str ing aField)
    > {
    > aField = "new stuff"; // throws exception
    > }
    >
    > Any ideas?
    >
    > thanks
    >
    > bill
    >
    >[/color]


    Comment

    • bill

      #3
      Re: Passing a Property

      Well, here is what is happening. I have a typed dataset with a lot of
      fields. Most of them are strings. I am passing info back from a form that
      has to fill the typed row. OK. I want to set the field to the value in a
      control unless it is blank. so what I have is about fifty

      if(c_MyFirstNam e.Text.Lenth > 0)
      {
      aRow.MyFirstNam e = c_MyFirstName.T ext)
      }

      clauses. so I thought I could have a method to shorten this repetitive
      action up

      AssignStringToR ow(string aInput, some_type aProperty)

      where the above code could be generic

      if(aInput.Lengt h > 0)
      {
      aProperty = aInput;
      }

      then I'd have a bunch of

      AssignStringToR ow(c_MyFirstNam e.Text, aRow.MyFirstNam e);

      if I pass the row in, I don't know what field to assign unless I pass a
      string or index in and then the battle is lost as far a type safety. also
      if I return a value, I will set the row value to something every time as in

      aRow.MayFirstNa me = AssignStringToR ow(c_MyFirstNam e.Text);

      what happens when input string is blank? do I return a null? I don't think
      so. You have to use SetMyFirstNameN ull() to do that. Else I could assign
      blank strings? It just seems a waste, or ugly, or something. I'd rather
      have the "if" clauses.


      bill


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:%23TKBjfLe EHA.1000@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > Bill,
      >
      > Since a property is in essence a method that is called, you need to[/color]
      pass[color=blue]
      > the object that exposes the property and then set the property from inside
      > the method.
      >
      > I think that a better solution would be to have the method return the
      > value somehow (returning, or through a ref/out parameter), and then set[/color]
      the[color=blue]
      > property outside of the method.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "bill" <sutphinwb@nosp am.nospam> wrote in message
      > news:e8i8DLLeEH A.1644@tk2msftn gp13.phx.gbl...[color=green]
      > > Hi
      > >
      > > I'd like to pass a property (say like a row value from a typed dataset)[/color][/color]
      to[color=blue]
      > a[color=green]
      > > method so that the method can get/set the property. for instance
      > >
      > > DoSomething(ATy pedRow.StringFi eld)
      > >
      > > where "StringFiel d" is a string property on a typed row.
      > >
      > > however, DoSomething requires a string argument in this case
      > >
      > > private void DoSomething(str ing aField)
      > > {
      > > aField = "new stuff"; // throws exception
      > > }
      > >
      > > Any ideas?
      > >
      > > thanks
      > >
      > > bill
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...