Change Value in ArrayList

W

wg

I have written a class to contain tags that are loaded from a spreadsheet. I
can load the value ok. But from another class I would like to change a value
inside the arraylist. Here is my code (see Update).

Thanks

wg

class Singleton

{

private static Singleton instance;

// Lock synchronization object

private static object syncLock = new object();

ArrayList TagList = new ArrayList();

iTags Tags = new iTags();

private Singleton()

{

}

public static Singleton Instance()

{

// Use 'Lazy initialization'

if (instance == null)

{

lock (syncLock)

{

if (instance == null)

{

instance = new Singleton();

}

}

}

return instance;

}

public void Update(int index,int aval, bool bval)

{

((iTags)TagList[index]).ival = aval;

((iTags)TagList[index]).bval = bval;

((Tags)TagList[index]).ival = index;

((iTags)TagList[index]).update = true;

}

public string Name(int index)

{

return ((iTags)TagList[index]).name.ToString();

}

public int i_val(int index)

{

return ((iTags)TagList[index]).ival;

}

public bool b_val(int index)

{

return ((iTags)TagList[index]).bval;

}

public void LoadCSVFile()

{

string BaseDir = AppDomain.CurrentDomain.BaseDirectory +
AppDomain.CurrentDomain.RelativeSearchPath;

string delimStr = ",";

char [] delimiter = delimStr.ToCharArray();

string [] split = new string[4];

char[] splitter = {','};

using (StreamReader sr = new StreamReader(BaseDir + "IntTags.csv"))

{

string line;

// Read and display lines from the file until the end of

// the file is reached.

while ((line = sr.ReadLine()) != null)

{

split = line.Split(splitter);

Tags.folder = split[0];

Tags.name = split[1];

Tags.type = split[2];

TagList.Add(Tags);

}

}

}

}

class iTags

{

public string folder;

public string name;

public string type;

public int ival;

public bool bval;

public bool update;

}

}
 
J

Jon Skeet [C# MVP]

wg said:
I have written a class to contain tags that are loaded from a spreadsheet. I
can load the value ok. But from another class I would like to change a value
inside the arraylist. Here is my code (see Update).

Okay, to start with your singleton implementation isn't thread-safe.
See http://www.pobox.com/~skeet/csharp/singleton.html

I'd also *strongly* recommend that you start using the .NET naming
conventions, and that you avoid public fields.

That said - Update looks *mostly* okay except that you're setting iVal
to aVal and then index. Which is it meant to be?

You've also only created one instance of iTags - you *should* be
creating a new one each time you go round the loop in LoadCSVFile,
rather than changing the contents of just a single instance. You don't
want a member variable of type iTags at all - it should be a local
variable in LoadCSVFile.
 
W

wg

Jon,

Thanks for the advice. Could you expand on "I'd also *strongly* recommend
that you start using the .NET naming
conventions, and that you avoid public fields." a little.

Thanks

wg
 
J

Jon Skeet [C# MVP]

wg said:
Thanks for the advice. Could you expand on "I'd also *strongly* recommend
that you start using the .NET naming
conventions, and that you avoid public fields." a little.

Okay - for the naming conventions, look at http://tinyurl.com/2cun

In terms of public fields, don't expose public fields; expose public
properties (where appropriate) backed by private fields. This allows a
much better level of control.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top