473,657 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Curre ntDomain.BaseDi rectory +
AppDomain.Curre ntDomain.Relati veSearchPath;

string delimStr = ",";

char [] delimiter = delimStr.ToChar Array();

string [] split = new string[4];

char[] splitter = {','};

using (StreamReader sr = new StreamReader(Ba seDir + "IntTags.cs v"))

{

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(spli tter);

Tags.folder = split[0];

Tags.name = split[1];

Tags.type = split[2];

TagList.Add(Tag s);

}

}

}

}

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 4329
wg <wg@somewhere.c om> 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
wg <wg@somewhere.c om> 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.co m>
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
2301
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> <Start>549019.590988 57955.274194</Start> </Line>
3
1530
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 ArrayLists. It seem like a lot of extra work to get value out of the ArrayList. Maybe I am doing it wrong. Thanks for the help.
24
2609
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 you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
13
1516
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 arraylist? Eg... For references. Class myClass {
12
4119
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
3370
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 want is to set the height of the row based on the number of lines in it. The row height of each row in the table may be different from each other. Is there a way to do this?
2
4005
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 simple. When I select a value, I get it, insert something into the database with that value and the page loads again with different data. Imagine a questionnaire and every page load a random question would be load on screen and when the user clicks...
3
2270
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 I compile the above code, it throws error and asked for a return value in the IF part Error 1 : not all code paths return a value Example Code(just to describe)
6
3421
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 with ArrayList, any help? Thanks. Hashtable data = new Hashtable(); if (!data.Contains(key)) {
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7321
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.