SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing an object to method

    This method is inside of a .cs file.

    I am trying to access an object that I have passed a parameter, and then access the properties. I have tried everything from instantiation the object within the method to trying to pass an object parameter. However, all I get are the default constructor values.

    What would be the proper syntax for passing an object as opposed to a string or int to a method??

    BTW,

    Here is an example of the method.

    public string buildHref(string sesVar, string userID, string threadID, string catID)
    {
    if (sesVar == userID)
    {
    return "[<a href=\"forum_edit.aspx?ThreadID=" + threadID + "&catID=" + catID + "&editID=1\">edit</a>]&nbsp;";
    }
    else
    {
    return "";
    }
    }

    It works but I want to add a parameter that passes an object.


    Thanks

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The syntax is exactly the same. Value types (integers, DateTime, etc) and reference types behave differently. Value types are passed by value, meaning:

    Code:
    public int AddOne(int addTo)
    {
         addTo++;
         return addTo;
    }
    
    int i=1;
    addTo(i);
    Will not change the value of i--it will still equal 1, but rather create a new integer internally and return that.

    Whereas, with a reference type, it will change the value:

    Code:
    public class IntContainer
    {
        public int Value=0;
    }
    
    public int AddTo(IntContainer c)
    {
          c.Value++;
          return c.Value;
    }
    
    IntContainer ic=new IntContainer();
    ic.Value=1;
    AddTo(ic);
    In this case, ic.Value will equal 2.

    Strings are slightly odd--they are immutable reference types. They generally behave like value types, but they can be null.

    Hope this helps.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that helped.

    Norbert


Bookmarks

Posting Permissions

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