1

I am writing a program for an SSIS process, and part of what it does is read rows from a data flow and pack them into a List of String Arrays List<string[]>. My main class contains the Updated List, but I have an EmailBuilder Class which needs to access the Updated List field to write an HTML E-Mail message. Right now, my code looks like this:

Fields

public static List<string[]> Updated = new List<string[]>();

ProcessInputRow

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    if (Row.Missing_IsNull)
    {
        Updated.Add(new string[] { Row.Name, Row.PreviousOutstanding.ToString(), Row.PreviousDate.ToString(), Row.CurrentOutstanding.ToString(), Row.CurrentDate.ToString()});
    }
    else
    {
        Missing.Add(Row.Missing);
    }
}

Of course, the problem here is that my field is public, which is a no-no. I tried using the built-in properties:

public static List<string[]> Updated { get; private set; }

But this returns a Null Exception, I assume because at that point I haven't actually created the list. So, how could I implement this code safely? I can't seem to manipulate it such that I can access the field from outside the class without making it a public field. Any input is greatly appreciated.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 1
    `public static List UpdatedSecurities { get; } = new List();`, or stay with `{ get; private set; }` and initialize it in the constructor. When you say "the built-in properties", what do you mean by that? – 15ee8f99-57ff-4f92-890c-b56153 May 10 '19 at 19:35
  • I mean using the auto-generated code that's made with { get; } instead of writing out { get { return updated } set { updated = value } }. My bad on naming that UpdatedSecurities, I meant to call it Updated for simplicity. Could you explain what you mean when you say initialize in the constructor? I'm not familiar. – Philippe Haussmann May 10 '19 at 19:42
  • Ok. Either way, try my suggestions. I wouldn't, by the way, have an absolute existential crisis about making a field public. A read/write public property vs a public field, it's much of a muchness (unless you're in WPF with databinding, but I think you're not). – 15ee8f99-57ff-4f92-890c-b56153 May 10 '19 at 19:44
  • Constructor: I should have said "static constructor". `Updated` is a member of a class. You don't say what its name is, so I'll call it Brian: `public class Brian { static Brian() { Updated = new List(); } public static List Updated { get; private set; } /* much other stuff */ }`. That `static Brian()` method is a static constructor, called the first time anything touches your class. It's for initializing static members such as `Updated`. – 15ee8f99-57ff-4f92-890c-b56153 May 10 '19 at 19:47
  • Possible duplicate of [Initializing C# auto-properties](https://stackoverflow.com/questions/169220/initializing-c-sharp-auto-properties) – Giulio Caccin May 10 '19 at 19:48
  • I'm not really messing with databinding in the script at all (SSIS is handling that). It's good to hear that it isn't as big a deal as i thought it was overall, and it's also great to understand the right way to do it if I had to. Thanks for all the help! – Philippe Haussmann May 10 '19 at 19:56

2 Answers2

1

It's better to hide your fields, even if it's not essential. You can initialize the auto properties like this if you are using C# 6 or later.

public static List<string[]> Updated { get; private set; } = new List<string[]>();
public static List<TheOtherType> Missing { get; private set; } = new List<TheOtherType>();
Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
0

If I understand correctly you want to keep the field private but make changes to it from outside the class. Is it ok to use a public method that does this? Or property?

Like

MainClass
{
  ...
  otherClass.AddToPrivateList("content")
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Erol
  • 1
  • 1