473,372 Members | 990 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,372 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 4254
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.