Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i am trying to return a c# reference type so i can edit the value outside of the class:(kinda like how the java Integer reference type)

what i came up with so far are as follows:

public class test
{
    int i;
    public Object get()
    {
        return i;
    }
}

public void program()
{
    test t = new test();

    t.get() = 1;
}


the place which i underline in the code is where there is a error.

anyone know how?
Posted

Try this one:
C#
public class test
{
    int i;
    public int I
    {
        get{ return i; }
        set{ i = value; }
    }
}
 
public void program()
{
    test t = new test();
 
    t.I = 1;
}
 
Share this answer
 
v2
Problem with your code is that you cant set a function. You have to use a property with both get and set. Then you can create a new instance of your object (in this case TestClass) and set the value of it's properties.

C#
public class TestClass
{
    public int MyNumericValue {get; set; }
}

public void program()
{
    test t = new TestClass();
    t.MyNumericValue  = 8;

   Console.WriteLine(t.MyNumericValue); // Will return 8
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900