0

I've been trying to write a program which can scan a raw data file and normalize it for data mining processes, I've trying to read the data from the file and store it in a list this way:

public static List<Normalize> NF()
    {
        //Regex r = new Regex(@"^\d+$");
        List<Normalize> N = new List<Normalize>();
        StreamReader ss = new StreamReader(@"C:\Users\User\Desktop\NN.txt");
        String Line = null;
        while (!ss.EndOfStream) {
            Line = ss.ReadLine();
            var L = Line.Split(',').ToList();
            N.Add(new Normalize { age = Convert.ToInt16(L[0]), 
                                  Sex = L[1], 
                                  T3 = Convert.ToDouble(L[2]),
                                  TT4 = Convert.ToDouble(L[3]),
                                  TFU = Convert.ToDouble(L[4]),
                                  FTI = Convert.ToDouble(L[5]),
                                  RC = L[6],
                                  R = L[7]
            });
        }
        return N;
    }
}
struct Normalize {
   public int age;
   public String Sex;
   public double T3;
   public double TT4;
   public double TFU;
   public double FTI;
   public String RC;
   public String R;
}

At this moment I want to go through the list that I've made and categorize the data , similar to this :

   var X= NF();
   for (int i = 0; i < X.Count; i++) {
            if (X[i].age > 0 && X[i].age <= 5) { // Change the X[i].age value to 1 }
            else if (X[i].age > 5 && X[i].age <= 10) { // Change the X[i].age value to 2 }
            ...
        }

But the compiler says X[i].[variable name] is not a variable and cannot be modified in this way. My question is, what would be an efficient way to perform this operation.

8
  • 1
    Where is X defined? Commented Aug 25, 2013 at 18:51
  • @JeroenVannevel I'll edit my question. Commented Aug 25, 2013 at 18:52
  • 1
    struct Normalize is a value type, not a reference type, therefore you cannot change its fields like that. Change it to class Normalize Commented Aug 25, 2013 at 18:53
  • 1
    Yes @Alex is correct. In case you need to mutate a struct, your design is wrong. Consider redesigning your struct to class Commented Aug 25, 2013 at 18:55
  • 1
    @SriramSakthivel: You are very right, I have to admit I made a very silly mistake. Commented Aug 25, 2013 at 18:56

3 Answers 3

3

struct Normalize is a value type, not a reference type, therefore you cannot change its fields like that. Change it to class Normalize

Sign up to request clarification or add additional context in comments.

Comments

1

Change struct Normalize to class Normalize and iterate with foreach loop. It's way cleaner.
You could also set variables to private and use getters/setters to check/set variable.

foreach (Normalize x in X)
{
    if (x.getAge() > 0 && x.getAge() <= 5)
        x.setAge(1)
    ...
}

Edit: just saw you already got your answer

Comments

1

Modifying struct field is fine as long as it's a single entity (Given its a mutable struct). This is possible -

var obj = new Normalize();
obh.Age = 10;

But in your case you are accessing the struct using indexer from the list.

Indexer will return copy of your struct and modifying the value won't reflect it back to the list which ain't you want.

Hence compiler is throwing error to stop you from writing this out.

As Alex mentioned, you should go for creating class instead of struct if you want to modify it.

On a side note, its always advisable to have immutable structs instead of mutable structs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.