如何使用用户输入创建数组作为数组长度 [英] how to create array using user input as a array length

查看:80
本文介绍了如何使用用户输入创建数组作为数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友。如果这是愚蠢的问题请忽略它。:-)我想要创建数组。如果我将值5输入到文本框中,则数组长度应定义为5.是否有办法执行此操作?

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??

推荐答案

我认为这个问题反映了一个事实:你没有完成C#的基本阅读和试验,任何人在开始学习语言时都需要做。



你需要得到像查尔斯·佩佐尔德的Dot Net Zero这样的好书[ ^ ](读取.NET中的基本数据类型。



您需要了解声明C#中的Type实例并将该实例与一个名称...这是你在创建任何变量时所做的...将根据类型具有不同的默认行为:



int anInt ; // anInt自动设置为值#0



string aString; // null



string [] aStrArray; // null



最好注意你声明的变量是否已经初始化。



虽然类型int的Microsoft文档('int是Sytem.Type.Int32的别名)在他们说你不能使用该类型的变量(或任何简单的数字类型)而没有初始化它时出错了;是的,你可以使用它。



你需要深入研究Type String,并了解它的独特方面(相对于.NET的Heap和Stack内部组件) 。



请记住,这里的每个人都曾经是初学者,并且必须像学习基础算术运算一样学习基础知识。学习更高级数学的进展。
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.


你需要的可能是:

What you need is probably this:
string[] words = null;
int length;
if (int.TryParse(myTextBox.Text, out length)) {
    words = new string[length];
    // ...
} else
    HandleTheCaseOfInvalidContentOfMyTextBox(myTextBox.Text/* , ... */);



-SA


如果您使用

if you used
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

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





喜欢这个并且有效。



like this and it works.


这篇关于如何使用用户输入创建数组作为数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆