lundi 16 novembre 2015

How to use automatic properties in C#? [duplicate]

This question already has an answer here:

I'm new to C# and I'm trying to generate a CSF with random values. This Exception appears even after I gave values for the attributes in my class... any ideas?

main.cs

class CreateCSVFile
{


    private char delimiter = ',';
    private static string filePath = "C:/Users/Laura/Desktop/" + "monitorData.csv";
    Random rand = new Random();
    PatientMonitoring patientData;

    public void writeToCSV()
    {
        using (StreamWriter csvFile = new StreamWriter(filePath))
        {

        patientData = new PatientMonitoring(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), rand.Next(0,40));

        file.Write(patientData.dateTime.ToString() + delimiter);
        file.Write(patientData.heartRate.ToString() + delimiter);


        }

    }
}

patientMonitoring.cs

class PatientMonitoring
{
    private string DateTime;
    private int HeartRate;

    public PatientMonitoring(string dateTime, int heartRate, )
    {
        this.DateTime = dateTime;
        this.HeartRate = heartRate;
    }

    public string dateTime
    {
        get;
        set;
    }

    public string heartRate
    {
        get;
        set;
    }

EDIT: the NullReferenceException is being generated by an error in the declaration of the class, not in it's construction, as I first believed




Aucun commentaire:

Enregistrer un commentaire