473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 4278
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Daniel Lidström | last post by:
Hi, I would like to know the cleanest way to change the serialization of my Line class from: <Line staStart="2327.02" length="10.00000003390744"> <End>549016.570965 57945.741122</End>...
3
by: mb | last post by:
If an ArrayList has an object named "myTest" which has properties ".Name" and ".Number". now how do I change the .Name of the object in the arraylist? I have had bad experiences so far with...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
13
by: Dan Bass | last post by:
Say I've got an ArrayList, which contains a list of integers. Is it possible to get a reference to one of the integers contained within the collection so that changes to it are reflected in the...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
2
by: Tor Inge Rislaa | last post by:
How to change row height in a DataGrid I have DataGrid that is filled with data from a table in a DataSet. The content of the cells is text of more than one line (as a note field). What I...
2
by: ricardo.sobral.santos | last post by:
Hello, I am trying to get the value of the radiobuttonlist. The problem is that I need it to be unselected at every page load. The radiobuttonlist is set to autopostback (true). My idea is...
3
by: sony.m.2007 | last post by:
Hi, I’m new to ASP.NET I have written a function with a return value. If the arguments to the functions are invalid means I’m giving exit(0) Else means do some process and return a value. When...
6
by: Slickuser | last post by:
Hi, I am picking up C#.net and I'm trying to add many values to one single key at different time in a loop. If the key already exist, append new value to previous? I'm not sure how to do that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.