|
As i saw in msdn, ArrayList holds only String Objects, while List can use any type including custom class, struct and so on
|
|
|
|
|
Saksida Bojan wrote: As i saw in msdn, ArrayList holds only String Objects
you either need a new MSDN or (new) glasses.
ArrayList and List<object> are rather similar; the bad thing about ArrayList is it could contain just all kinds of things, i.e. it isn't type safe.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Please stop asking elementary questions to which a simple MSDN page would provide adequate answers.
you urgently need to learn how to help yourself. with a book, Google and MSDN.
please study and learn, rather than ask and not learn.
thank you extremely much.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Very simple - just the same as you would for anything else:
private void button1_Click(object sender, EventArgs e)
{
ArrayList al = new ArrayList();
LoadList(al, 0, 3, 1);
ShowList(al);
ArrayList[] all = new ArrayList[2];
all[0] = new ArrayList();
all[1] = new ArrayList();
LoadList(all[0], 10, 13, 1);
LoadList(all[1], 0, 300, 100);
foreach (ArrayList a in all)
{
ShowList(a);
}
}
private void ShowList(ArrayList al)
{
for (int i = 0; i < al.Count; i++)
{
Console.Write(" " + al[i].ToString() + " ");
}
Console.WriteLine();
}
private void LoadList(ArrayList al, int start, int end, int increment)
{
for (int i = start; i <= end; i += increment)
{
al.Add(i.ToString());
}
}
However, I would suggest that rather than an ArrayList, use a List<T> - the usage is very similar.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
I Don't know how array list works, but i woud youse List from System.Generic namespace
public List<ArrayList> lst = new List<ArrayList>();
lst.Add(new ArrayList());
also you cant use name of variable with a type of Arraylist as return value for function. If you want to have specific name for return value, you could use STRUCT.
As I said, you can't have the name of variable as return value
public stringArray verification(string path, string path_save)
this is how should be done
public ArrayList verification(string path, string path_save)
{
return stringArray;
}
Edit: Some tags needed to be converted, so that it woudn't brake view
modified on Thursday, October 15, 2009 2:28 PM
|
|
|
|
|
Hello,
I'm writing a C#/.net3.5 application with VS2008 Pro, and I just found today that there is a problem opening a file for writing when the file already exists. I tried to reverse to old revisions with SVN, but the problem is still there.
The user can choose to open a file named, for example 'file.ext'. The file is read through a StreamReader and ReadToEnd() method. Once it's done, I call the Close() method on the StreamReader. I check if a second file, name 'file.rak' (that holds more information) exists, and if so, it's open with the same method as the first file. The name of the first file is stored in a class in the application. Both files have same structure.
Saving the file is made with a StreamWriter. I first open the file, then write my data with the Write() method, and I close it, with Close(). After the first file is written, I call the same method to write the second file. The problem is when I try to open the second file for writing (still with StreamWriter), I got an exception saying access to the file is denied.
I tried to see with Process Monitor the operations on the file, but we can see it's well closed after reading is done.
Any clue ?
Thanks !
Fred
|
|
|
|
|
Try surounding the read file commands with a "using" block, or calling Dispose on the file stream.
Just because you have finished with it, doesn't mean that all access has been released!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
There was already calls to Close(), but I tried with using. It's the same, when I want to write to the second file, I still have the same message ...
Here is the reading code :
private List<Ligne> getLignesFromFile(String sFichier)
{
List<Ligne> oListe = new List<Ligne>();
using (TextReader oTR = new StreamReader(sFichier, Encoding.Default))
{
String sContenu = oTR.ReadToEnd();
try
{
oListe = SrtFileParser.parseString(sContenu);
}
catch (Exception e)
{
throw e;
}
}
return oListe;
}
And writing code :
public bool ecrireFichier(String sNomFichier)
{
using (TextWriter oTW = new StreamWriter(sNomFichier, false, Encoding.Default))
{
try
{
}
catch (Exception e)
{
throw e;
}
}
if (CountTotalWordsTranslated() > 0)
{
String sFichierTemp = TranslationUtilities.generateTempFileName(sNomFichier);
ecrireTraduction(sFichierTemp, MissingTranslation.DoNotExport);
}
return true;
}
public bool ecrireTraduction(String sNomFichier, MissingTranslation mt)
{
using (TextWriter oTW = new StreamWriter(sNomFichier, false, Encoding.Default))
{
}
return true;
}
The problem happens in the ecrireTraduction function. The call to the StreamWriter constructor fails, and throws an exception.
That's the only code in the whole application dealing with those files ...
|
|
|
|
|
Hi,
when you use a try-catch, then look at exception.ToString() to see what goes wrong where.
and tell your IDE to always show line numbers, for Visual Studio see here[^].
if the error says "file locked by another process", that could very well be your own app. So make sure everything related to earlier accesses to the same file is closed and done with.
BTW: why do you post a long textual description rather than the little code it describes?
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Yes, I'm almost sure the file is locked by my program, but when files are opened before, calls to Close function are made so they're unlocked ! Even with 'using', it's the same.
exception.ToString() is :
ex.ToString() "System.UnauthorizedAccessException: Access to the path 'C:\\usr\\ProjetCsharp\\angels.rak' is denied"
( + stack trace)
|
|
|
|
|
I don't see what is wrong; maybe you are nesting things (not shown) or running several file operations on different threads (not shown).
One way to get it to fail would be to have SrtFileParser.parseString call ecrireFichier.
If relevant, all this can be checked by adding some logging, as in Console.WriteLine("Now opening...") and the like, and observing the logs.
Here is a suggestion:
your getLignesFromFile() method reads the entire file at once in a complex manner; you could have used File.ReadAllText() or File.ReadAllLines() which are much harder to get wrong.
So replace all your file reads by those methods.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Thanks for your suggestions. I removed some code lines in the listing I posted earlier, but it was only Write() calls.
There's no other thread in the app dealing with the files. The whole code for files is here.
I added some debug info with Console.WriteLine, but it confirms what I saw earlier.
Anyway, I found something new : if I try to open the second file with the second argument to StreamWriter set to true (so it appends data instead of overwriting), there's no exception thrown.
I tried the code on 2 differents PCs, both VC# 2008 and VS 2008 Pro give me the same problem !
|
|
|
|
|
Still doesn't make any sense to me.
Question: is your app writing the same file more than once?
I mean:
open file for create/overwrite
write file
close file
open file for create/overwrite
write file
close file
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Thanks for your help !
No, it doesn't write the same file several times.
The only code dealing with files is posted above and here's what it does :
At first :
Open file 1 for reading
Read data from file 1
Close file 1
If file 2 exists :
Open file 2 for reading
Read data from file 2
Close file 2
File 1 name is stored between these, so the user can save clicking on a button.
Open file 1 for writing
Write data to file 1
Close file 1
If some data is for file 2 :
Open file 2 for writing <-- that's where it crashes
Write data to file 2
Close file 2
And if I try to delete file 2 before opening it for writing, there's no error after, and the file is actually deleted.
I know it's strange thing, I can't figure why it doesn't work !
|
|
|
|
|
With that sequence of operations, I could explain if and only if the two destination files are the same.
(you didn't show the calls to ecrireFichier and ecrireTraduction)
Suggestions:
1. check your code, make sure both sNomFichier values are different;
2. insert a Thread.Sleep(5000) before opening the second file.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
The only difference in the names is the extension. And the exception shows the name of the second file (with .rak extension).
I tried with Thread.Sleep(5000); it freezes well for 5 seconds, but the exception is still here !
The only way I found to bypass this exception is, before calling the ecrireTraduction function, to delete the file :
FileInfo fInfo = new FileInfo(sFichierTemp);
if (!fInfo.IsReadOnly)
{
fInfo.Delete();
}
The STRANGE thing is that it can't be opened for writing for overwrite, but it's ok for open for append or delete the file !
|
|
|
|
|
why the IsReadOnly check?
are you playing with the file properties? if so, you should have said so from the start!
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I found !!
After writing the data file 2 for the first time, I set its Hidden property to true; The goal was to make things transparent to the user.
If I remove this attribute to the file, and try to write it, it works ! I've never heard of such behaviour ...
Thanks a lot for your help about that !
|
|
|
|
|
good.
Are you running on a Virtual Machine? which one?
and which Operating System?
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
No it's not in a virtual machine.
I tried it both on XP and Seven RC, and it's ok now !
|
|
|
|
|
Thanks for your help,
there is something even more strange : before opening the StreamWriter that throws the exception, if I try to delete the file, there's no error. It can be deleted, but can't be opened for writing.
|
|
|
|
|
Member 4052498 wrote: It can be deleted, but can't be opened for writing.
That doesn't make sense to me.
If that's correct, I would say something is very wrong.
I suggest you delete the file, reboot the system, rebuild the app, and try again.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I'm a newbie in computer programming so please have this in mind.
I have a class called Pinger which does basic pinging of hosts on a network. I'm currently implementing a network scanner so I create an array (254) of Pinger's. The problem occurs when I try to dispose the array after I've done the scan. I dispose every Pinger in the array - the Pinger class contains Dispose methods (GC.SuppressFinalize).
Still, the array of Pinger's still uses a lot of rescources on my system and I can clearly see the memory beeing freed when I close my application.
Does anyone have any idea on how to correctly get rid of the rescources being used (after I'm finished using them)?
I have the complete project, but couldn't find a way to upload it so if anyone want's to see it let me know how I can send you the project.....
private void AllPingsFinished()
{
counterForInsert = 0;
foreach (Pinger str in _pinger)
{
if (str != null)
{
str.Dispose();
}
}
}pre>
|
|
|
|
|
I don't think that you can remove object from Collection while it's looping
In Other word
<br />
<br />
foreach ( string str in MyList)<br />
{<br />
MyList.remove(str);<br />
}
List<string> ListForDetele = new List<string>();<br />
foreach (string str in MyList)<br />
{<br />
ListForDelete.Add(str);<br />
}<br />
<br />
foreach(string str in ListForDelete)<br />
{<br />
MyList.Remove(str);<br />
}
Have a good day ,
I know nothing , I know nothing ...
|
|
|
|
|
I will try this!
Thanks!!
|
|
|
|
|