|
-
Nov 22nd, 2001, 05:16 PM
#1
Private variable problem
ok I have declared a variable as Private Static.
Code:
public class tp3
{
private static String allInformation[];
...
but in a SUB I do that :
Code:
allInformation = new String[10000];
for (i = 0; i < 10000; ++i)
allInformation[i] = new String();
//After that code I start to pu DATA in the ARRAY.
//All is great since that state.
but now I want to use the DATA in the ARRAY in an other sub and I can not find it because it's empty ?
Why?
-
Nov 22nd, 2001, 05:32 PM
#2
Usually when I put my declaration Private Static it work everywhere but not now why?
-
Nov 22nd, 2001, 05:46 PM
#3
Member
Variable scope problem because your mind has been tainted by VB. Try deallocating it (BTW, do i++, not ++i) or doing this:
Code:
String[] toReturn;
// do stuff on toReturn
return toReturn;
Last edited by filburt1; Nov 22nd, 2001 at 05:55 PM.
-
Nov 22nd, 2001, 05:52 PM
#4
Member
Re: Private variable problem
Originally posted by DaoK
I want to use the DATA in the ARRAY in an other sub and I can not find it because it's empty ?
Why? [/B]
what do u mean by " can't find it"?
-
Nov 23rd, 2001, 04:45 PM
#5
-
Nov 24th, 2001, 11:16 AM
#6
Dazed Member
It looks like a scope problem. The String array that you are creating within your method is scoped to that method. Once the method terminates the garbage collector starts to reclaim memory. Try creating a method that takes a String array as an arguement and returns a String array.
-
Nov 24th, 2001, 11:50 AM
#7
OK but how ? It's very important I need to be able to use all data in that array everywhere in the project. Please tell me how to declare it.
-
Nov 24th, 2001, 12:55 PM
#8
Dazed Member
Im sure you could use getters and setters and declare your String array which is a member of your class private. This would probably be good if your application has multiple classes.
-
Nov 24th, 2001, 02:59 PM
#9
you could use getters and setters and declare your String array which is a member of your class private.
Ok but how...they didnt learn us to do that and I need to be able to that array in all my class please.
-
Nov 24th, 2001, 09:58 PM
#10
ANy one can give me a piece of code ?
-
Nov 25th, 2001, 11:41 AM
#11
Question is :
Give me the way to declare a simple Array variable who will be able to access the data anywhere in the class.
THANK YOU
-
Nov 25th, 2001, 11:59 AM
#12
Dazed Member
It depends on the way the variable is declared and in what context you are trying to access your array. For instance if your array is declared non-static and you are trying to refrence it from a static context then it wont work. The code that you posted.
Code:
allInformation = new String[10000];
for (i = 0; i < 10000; ++i)
allInformation[i] = new String();
//After that code I start to pu DATA in the ARRAY.
//All is great since that state.
you said that in a sub, so i am assuming that you have a method which you are trying too access the String array. You also said that the array is empty or i guess padded with null values, but the block of code you provided only creates (as far as i can see)
empty strings.
-
Nov 26th, 2001, 09:22 PM
#13
Yes that empty then I put data on it but when I am in another sub after have put some data on it I can not access to it why?
-
Nov 26th, 2001, 09:58 PM
#14
Dazed Member
Post your code in it's entirety. This was we can get a better picture of whats going on.
-
Nov 27th, 2001, 07:57 PM
#15
well thx you but i changed the code and remake one who work now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|