.NET C# Question

Soldato
Joined
7 Jun 2005
Posts
3,035
Location
SE London
Hey, i've created a basic get/set property method for one of my usercontrols, which takes an integer array in as a parameter, or returns it, like so:

Code:
public int[] Value
{
        get
        {
            .........
         }
         set
         {
            ..........
          }
}

Now i have another page where i'm using this control, and i'm having the following piece of code in my Page_Load event:

Code:
        int[] mySelectedValues = new int[2];
        mySelectedValues[0] = 9;
        mySelectedValues[1] = 8;

        msTitles.Value = mySelectedValues;

when debuggin, at the last line of the above code, the variable "mySelectedValues" has become a 2-dimensional array (with values 8 and 9), but when stepping into the .Value property set/get method, no value has been passed through.

Any ideas anyone?
 
If i remember correctly, copying arrays arent as simple as that. Cant offer much in the way of help as i havent touched them in a while
 
Arrays are as simple as that.

Here is a quick example I knocked up...

In a sample project just create a simple form with a button on it and have the button fire button1_click when clicked
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace TestApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Class1 c1 = new Class1();

            int[] mySelectedValues = new int[2];
            mySelectedValues[0] = 9;
            mySelectedValues[1] = 8;

            c1.Value = mySelectedValues;

            Debug.WriteLine(string.Format("Index 0 is {0}", c1.Value[0]));
            Debug.WriteLine(string.Format("Index 1 is {0}", c1.Value[1]));

        }
    }
}

All Class1 contains is the following

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace TestApp1
{
    class Class1
    {

        private int[] _val;

        public int[] Value
        {
            get { return _val; }
            set { _val = value; }
        }
    }
}

The output is as expected.

Index 0 is 9
Index 1 is 8
 
Dr_Evil said:
when debuggin, at the last line of the above code, the variable "mySelectedValues" has become a 2-dimensional array (with values 8 and 9), but when stepping into the .Value property set/get method, no value has been passed through.

Any ideas anyone?

Out of interest, when the line is highlighted in the debugger it hasnt actually been executed it is just about to be. You would need a breakpoint on the next line before the Value property holds the correct values.
 
The original code I had for the Value property/method:
Code:
public int[] Value
    {
        get
        {
            int[] myIntArray = new int[lbSelectedValues.Items.Count];

            for (int x = 0; x < lbSelectedValues.Items.Count; x++)
            {
                myIntArray[x] = Int32.Parse(lbSelectedValues.Items[x].Value);
            }

            return myIntArray;     
        }

        /*set
        {
            this.arrInitialValues = Value;
        }*/
    }

the "get" method works fine, however for the "set" method i had to write a separate method as this didn't work.

the following method worked fine:

Code:
public void setValue(int[] Value)
    {
        this.arrInitialValues = Value;
    }


WHat's the Difference?

p.s. yes i know about debugging, i actually "step into" the method and can see that the initial array has not been passed in...
 
No it's not that, the code is now commented out and probably the s deleted by accident. there were not compilation errors when i executed the code.
 
Value is your property, but value (with a lower case v) is the data that is sent to the set method. So value is mySelectedValues. If you don't use value the set method won't use mySelectedValues at all.
 
What version of .NET are you working in? I havent tried this recently but I used it extensively in a recent project and cant recall having any problems :confused:

I used v2.0 so perhaps it was a known bug in an earlier version?
 
Thanks - that was it! My code works fine now, as following:

Code:
public int[] Value
    {
        get
        {
            int[] myIntArray = new int[lbSelectedValues.Items.Count];

            for (int x = 0; x < lbSelectedValues.Items.Count; x++)
            {
                myIntArray[x] = Int32.Parse(lbSelectedValues.Items[x].Value);
            }

            return myIntArray;     
        }

        set
        {
            this.arrInitialValues = value;
        }
    }

and the code that calls the .Value code above:

Code:
int[] mySelectedValues = new int[2];
mySelectedValues[0] = 9;
mySelectedValues[1] = 8;
msTitles.Value = mySelectedValues;

MANY THANKS GUYS!
 
Personally, I would avoid properties/methods called things like Value for this very reason. It makes it far too easy to make a mistake in the case of a character and end up with results like you have seen.
 
Agree with the statements above - give your properties proper names that reflect their real use.

'Value' is bad. If you really have to use 'values' then go with what you were using in the rest of the code and have .MySelectedValues. It's just about making things explicit and specific in this case.

That's aside from fact it's caused you to confuse the property name with 'value'!
 
i think my reasoning was to have .Value as most other .net objects have .Value, so to keep method names similar. Yes, the intrinsic "value" property is confusing.
 
Glad its sorted.

On a pedantic note:

[8,9] is a 1D array of length 2, not a 2D array. Just thought you might like to know, so you dont get confused in future :).
 
Back
Top Bottom