CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2005
    Posts
    159

    Question Confused about references

    Hi all,

    I want to modify the contents of a DataTable cell in a method. I assumed that just passing table.Rows[i]["Value"] would be enough. There is no passing by value in this case. I figured that this is actually a method returning a reference as result and as such I would be able to modify the cell contents.

    This didn't work and I don't understand why. I tried passing a reference explicitly but that didn't work neither becasue you can not reference an indexer.

    In the end I got something working but I got confused about references in this particular instance. This is how I got it working:

    Code:
    Object temp = table.Rows[i]["Value"];
    CheckCellIntegrity(ref temp, table.Rows[i]["Type"].ToString(), i+1);
    table.Rows[i]["Value"] = temp;
    Thanks in advance for the clarification,

    Jef

  2. #2
    Join Date
    Jul 2006
    Posts
    297

    Re: Confused about references

    I don't understand. You're trying to assign table.Rows[i]["Value"] the value of temp inside CheckCellIntegrity() ?

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Confused about references

    Why not just pass in the row item to the check method?

    Code:
     
    Object temp = table.Rows[ i ]["Value"];
    
    if( CheckCellIntegrity( table.Rows[ i ], i+1 ) )
    {
      table.Rows[ i ]["Value"] = temp;
    }

  4. #4
    Join Date
    Nov 2005
    Posts
    159

    Re: Confused about references

    Well, suppose I know that a certain column contains strings and I want to modify a cell of the table inside a method, I do this:
    Code:
    table.Rows[ i ]["Value"] = "test 1";
    Method(table.Rows[ i ]["Value"]);
    with
    Code:
    void Method(Object cell)
    {
        object = "test 2";
    }
    When I check the value of table.Rows[ i ]["Value"] after the execution of method it contains "test 1". Why?

    Jef

  5. #5
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Confused about references

    Code:
     void Method(Object cell)
    {
        object = "test 2";
    }
    what you are assigning is to object but you should assigning it to the instance of it i.e, "cell"

  6. #6
    Join Date
    Nov 2005
    Posts
    159

    Re: Confused about references

    Sorry, I made a mistake in my example, this wouldn't even compile. But with this code I have the same problem

    Code:
    void Method(Object cell)
    {
        cell = "test 2";
    }

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Confused about references

    In general, you'll want to specify a specific type as a method parameter.

    Object is the base type for all objects, so the compiler doesn't know if it's a type that can contain a string or not.

  8. #8
    Join Date
    Nov 2005
    Posts
    159

    Re: Confused about references

    table.Rows[ i ]["Value"] is of the type object so I must pass it that way. The compiler allows assigning a string to object, this seems logical since String is inherited from Object.

    Your reasoning would imply that it is impossible to pass a cell and modify it, I can hardly believe that. I still feel like I'm missing something.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Confused about references

    Quote Originally Posted by Jef Patat View Post
    Your reasoning would imply that it is impossible to pass a cell and modify it, I can hardly believe that. I still feel like I'm missing something.
    You misunderstand. I'm suggesting to pass in a strong type.

    This won't work:
    Code:
     
    void Method(Object cell)
    {
        cell = "test 2";
    }
    these will
    Code:
    void Method(string cell)
    {
        cell = "test 2";
    }
    void Method(int cell)
    {
        cell = 1;
    }
    My reasoning is that you should avoid working with objects as much as possible because the compiler can't help you detect type mismatches.

    If you used strongly typed parameters in your methods, then the compiler can catch mismatches; otherwise, you are going to have runtime errors.

  10. #10
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Confused about references

    Code:
    Object temp = table.Rows[i]["Value"];
    CheckCellIntegrity(ref temp, table.Rows[i]["Type"].ToString(), i+1);
    table.Rows[i]["Value"] = temp;
    the keyword "ref" might be the one which is making it to work as it is actually pointing to the exact memory location where temp is present.

    so try using "ref" keyword in the method function and also at the place where you are calling the method function :

    Code:
     void Method(ref Object cell)
    {
        cell = "test 2";
    }
    Last edited by vcdebugger; August 21st, 2009 at 12:41 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured