471,319 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Change Value in ArrayList

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;

}

}
Jan 26 '06 #1
3 3517
wg <wg@somewhere.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 26 '06 #2
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
wg <wg@somewhere.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 26 '06 #3
wg wrote:
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

Jan 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Daniel Lidström | last post: by
13 posts views Thread by Dan Bass | last post: by
2 posts views Thread by Tor Inge Rislaa | last post: by
3 posts views Thread by sony.m.2007 | last post: by
6 posts views Thread by Slickuser | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.