Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 20th, 2011, 11:26 AM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default Textbox to array

I'm trying to figure out the code for parsing the contents of a textbox into an array, and can't seem to get the syntax correct. What I have tried is
string[] values = {inputtextBox.Text} among other things, but am getting a
"A field initializer cannot reference the non-static field, method, or property" Any suggestions would be appreciated.
 
Old July 20th, 2011, 12:02 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

You can't access members of a type in an object initializer. The Text property can only be accessed at runtime, that's too late for the object initializer.
First you need to declare and instantiate the array, then you can pass the value from the Text property:
Code:
string[] values = new string[count];
values[0] = inputtextBox.Text;
Hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old July 20th, 2011, 01:10 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Christian, as always thanks for your reply.
I have

string[] values = new string[200];
values[0] = inputtextBox.Text;


Visual Studio is throwing out the following errors on line two,

Invalid token '='
Array size cannot be specified in variable dec.
Invalid token ';'
 
Old July 20th, 2011, 02:23 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Mark, what's the context where you've this code lines?
If values is a field you need to assign the value to values[0] within the constructor - or the method when the assignment should be done.
Check the compiler result, it should give you the line where the problem is.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old July 20th, 2011, 02:31 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dropped the code into the Form1_Load method, and it looks okay.
 
Old July 20th, 2011, 05:07 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One more quick, but annoying newbee question, with the code in Form1_Load, the values identifier in the Message.Box statement wasn't being recognized, so I decided to code it differently

private string[] values = new string[200];
values[0] = inputtextBox.Text;


private void displaybutton_Click(object sender, EventArgs e)
{
Message.Box("" + values[4].ToString());
}

The values[0] line is still throwing out these errors.
Invalid token '='
Array size cannot be specified in variable dec.
Invalid token ';'
 
Old July 20th, 2011, 05:26 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

I guess previously you had both lines, the declaration of the values variable and the assignment of the first element of the array in the Form1_Load method. Of course then you can't use the values variable within other methods.
Now you have both lines outside of the method within the class. This makes it possible to use the variable values within all methods. However, you can't use values[0] outside of the methods.
Put the line values[0] = inputtextBox.Text within the Form1_Load method and leave the declaration and initialization of the variable values outside. This way the code compiles and you can use the values variable in every instance method of the class.

A few additional notes: within the Form1_Load method do you already have the value from the inputtextBox that should be put into values[0]. It's not the value that the user entered but instead the value that was used to initalize the inputtextBox. The Form1_Load method is invoked on loading the form.
Did you initialize the fifth field of values, values[4] before using it? In the click event of the displaybutton you read the value of values[4]. Maybe you've initialized it in another place, then it's ok.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old July 21st, 2011, 12:21 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Christian, in response to your note questions. I am trying to get the form to accept what is entered in the text box, after loading. Also, I haven't initialized the fifth value. I thought by intializing the array, it also grabbed the elements.
 
Old July 21st, 2011, 12:49 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Hi Mark,

Quote:
Originally Posted by Mark P. View Post
I am trying to get the form to accept what is entered in the text box, after loading.
Not sure what you try to achieve. The user can enter values only after the form is loaded. So the loaded event occurs before some data can be entered by the user. You could use a click event of an button to check the input data.

Quote:
Originally Posted by Mark P. View Post
Also, I haven't initialized the fifth value. I thought by intializing the array, it also grabbed the elements.
Only if the array contains value types, you'll have values in the array.
Declaring and initializing an array with reference types you need to assign objects to every element of the array.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old July 21st, 2011, 01:05 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your patience on this. What I am trying to do is capture five elements, in this case, letters from a keyboard entry, from a texbox so that I can run some expressions against them.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
Textbox value array Stuart010 Excel VBA 2 March 16th, 2007 03:16 PM
control array of textbox greatmanu Excel VBA 1 October 24th, 2003 09:36 AM
Masked TextBox & formatting TextBox melvik C# 1 September 22nd, 2003 11:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.