Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi friends.if this is stupid question please ignore it.:-) i want create array. if i enters the value of 5 to a Textbox the array length should be define as the 5. is there were way to do this??
Posted
Comments
deepankarbhatnagar 13-Jan-16 0:55am    
WHat have you tried? please show your code
Member 12235586 13-Jan-16 1:00am    
i used this for create array.= "string[] bbb=new string[5];"
but i want something like this ="int size = 50;
string[] words = new string[size];" or
string[] bbb=new string[textbox1.text];
Sergey Alexandrovich Kryukov 13-Jan-16 1:27am    
Everything will work, except last line which makes no sense at all.
—SA

I think this question reflects a fact: that you have not done the basic reading about, and experimenting with, C# that anyone need to do when starting to study the language.

You need to get a good book like Charles Petzold's "Dot Net Zero" [^] (free), and read about the basic data Types in .NET.

You need to understand that "declaring" an instance of a Type in C# and associating that instance with a "name" ... which is what you do when you create any variable ... will have different "default" behaviors depending on the Type:

int anInt; // anInt automatically set to value #0

string aString; // null

string[] aStrArray; // null

It is good practice to always pay attention to whether or not the variables you declare have been initialized.

While the Microsoft documentation for Type int ('int is an alias for Sytem.Type.Int32) is in error when they say that you cannot use a variable of that Type (or any simple Numeric Type) without initializing it; yes, you can use it.

You need to study Type String in depth, and understand it's unique aspects (relative to the Heap and Stack internal components of .NET).

Remember that everyone here was once a "beginner," and had to go through learning the "basics" in the same way one has to learn fundamental arithmetic operations to progress in learning more advanced mathematics.
 
Share this answer
 
What you need is probably this:
C#
string[] words = null;
int length;
if (int.TryParse(myTextBox.Text, out length)) {
    words = new string[length];
    // ...
} else
    HandleTheCaseOfInvalidContentOfMyTextBox(myTextBox.Text/* , ... */);

—SA
 
Share this answer
 
v5
Comments
BillWoodruff 13-Jan-16 1:57am    
Parens in the 'TryParse test don't match.

Since the 'words variable is declared in the scope of the 'if block, it will effectively not exist outside that block ... I doubt you intended that. cheers, Bill
Sergey Alexandrovich Kryukov 13-Jan-16 2:02am    
Thank you very much.
As to the block, I meant that the variable "words" can be used completely in this block and never outside. It still explains the idea in the shortest way...
—SA
BillWoodruff 13-Jan-16 2:28am    
fyi: I didn't vote on this solution, but I think the decision to leave the variable inside the 'if block is a mistake ... in the context of helping a newcomer understand programming.
Sergey Alexandrovich Kryukov 13-Jan-16 2:33am    
No problem at all. I mean, yes, it looks like a bad sign; and I would not do it in real code, but I hope the reader won't take it for real. I just don't want to make the sample bigger than it has to be. On second though, if you see an issue here, others may see it too or even get mislead, so perhaps now I'll follow your advice...
Thank you again.
—SA
Matt T Heffron 13-Jan-16 12:31pm    
+5(I don't understand why the 3 votes of 1.)
if you used
C#
i used this for create array.= "string[] bbb=new string[5];" 
but i want something like this ="int size = 50;
string[] words = new string[size];" or 
string[] bbb=new string[textbox1.text];



just changed in last line and it's work
you have to convert text box text in to integer

C#
string[] bbb1= new string[Convert.ToInt32( TextBox1.Text)];


like this and it works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900