Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

c# - I cannot create object that contains arrays of property inside object

I am a new developer to c# MVC3. I have a problem that I cannot create object that contains arrays of property inside object.

For example, I want to create instance ABC.Property[] for 10 arrays.

ABC.Property[0]

ABC.Property[1]

ABC.Property[2] ..... .... ABC.Property[10]

I used debug mode to check and found out that ABC.Property[] is null. So i cannot add the value back to that object's array.

How to crate object with propeties's array?

thank you.

namespace finance3.Models
{
public class Expected_and_Risk
{


    public decimal[] Prop { get; set; }
    public decimal[] Forecast { get; set; }
    public string[] Name { get; set; }
    public decimal[] AxB { get; set; }
    public decimal[] PowAxB { get; set; }


    public decimal  ExpectValue(Expected_and_Risk abc)
    {

        decimal count = abc.Forecast.Count();

        Expected_and_Risk Result = new Expected_and_Risk();

        for (int i = 0 ; i < count ; i++)
        {
            // here is the problem
            // i cannot add new data to array because it has no dimemsion and i tried this
            //
            // Expected_and_Risk[] Result = new Expected_and_Risk[10];
            //
            // but it didn't work

            Result.Name[i] = abc.Name[i];
            Result.Prop[i] = abc.Prop[i];
            Result.Forecast[i] = abc.Forecast[i];
            Result.AxB[i] = abc.Prop[i] * abc.Forecast[i];


            decimal a = Result.AxB[i];
            decimal sumAxB =+ a;

            double temp = (double)(a * a) ;
            Result.PowAxB[i] = (decimal)(temp);

        }

        return Convert.ToDecimal(Result); 
    }
}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to add a Constructor in your class and in that constructor you can define the size for your property

public class Expected_and_Risk
{
//......//your code here
    public Expected_and_Risk()
      {
      this.Prop  = new decimal[10]; // this will define an array of 10 decimal elements for Prop
      }
}

Also read about object oriented programming, Also check out this article An Intro to Constructors in C#


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...