Solved

C# string from array wont show up in textbox

Posted on 2008-10-31
12
746 Views
Last Modified: 2013-12-17
Hi! Here is my problem.

I need string "One, Two, Three, Four" to show up in textBox1 but it doesn't.

I use different class files but they are in one namespace.

I would appreciate if anybody could help me correct my code.
Mario.


//Form1.cs

public partial class Form1 : Form

{

    public Form1()

    {

        InitializeComponent();

    }
 

    public void LoadMain1(object sender, EventArgs e) 

    //This is Load Event for Form1

    {   

        this.textBox1.Text = Strings.displayText;

    }
 

}
 
 

//Strings.cs

public class Strings

{
 

    public static string displayText;
 

    public Strings(string[] args)

    {
 

        string[] toppings = new string[4] { "One", "Two", "Three", "Four" };
 

        StringBuilder result = new StringBuilder();
 

        for (int i = 0; i < toppings.Length; i++)

        {

            if (i == 0)

                

            result.Append(toppings[i]);
 

            else
 

            result.Append(", ").Append(toppings[i]);

        }
 

        displayText = result.ToString();
 

     }

}

Open in new window

0
Comment
Question by:X3R
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22854346
Try doing this modification:

this line:

result.Append(toppings[i]);

change to:

result = result.Append(toppings[i]);



and this line:

result.Append(", ").Append(toppings[i]);

change to:

result = result.Append(", ").Append(toppings[i]);
0
 
LVL 13

Expert Comment

by:sm394
ID: 22854357

string s = "";
            foreach (string str in toppings)
                s += str;

displayText=s;
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 22854363
X3R,

The reason that that value is not populated is because you never set it.  It's set when you instantiate the class (in the constructor).  Without that instantiation, it will have no value.

Is there any reason why you need a static variable?  If not you can just make it public (or better yet private and add an accessor function).  Something like this:
Strings myString = new Strings();

this.textBox1.Text = myString.displayText;

Open in new window

0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22854368
Also you have something else wrong. The function that modifies it should be a static function as well.

Do something like this:


    //This is Load Event for Form1
    {
        this.textBox1.Text = Strings.parseArray( ... pass array here)
    }


public class Strings
{
 
    public static string displayText;
 
    public static string ParseArray(string[] args){
       //insert code here
    }
}


0
 
LVL 8

Expert Comment

by:bramsquad
ID: 22854398
By the way, you don't need to pass in params if they're not going to set anything in your constructor.

Something like this:

String myString = new Strings(new string[4] { "Five", "Six", "Seven", "Eight" });

will still return "One,Two,Three,Four" because you never do anything with your args[] variable.
0
 

Author Comment

by:X3R
ID: 22854882
bramsquad,

I tried yor solution but it worked in ConsoleApplication (it returned text that i need into textbox) but not with textbox.

VB2005 gave me an error:
No overload for method 'Strings' takes '0' arguments

when I added this line:
Strings myString = new Strings();
 

//I also changed 

public static string displayText;
 

//to
 

public string displayText;

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Expert Comment

by:RishadanPort
ID: 22854892
>No overload for method 'Strings' takes '0' arguments

That's because you declared that function

Strings(string[] args)

at this line:
Strings myString = new Strings( ... PUT string array here ...);
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22854900

    public Strings(string[] args)
    {
        string[] toppings = new string[4] { "One", "Two", "Three", "Four" };    <-- remove this line
        ...
    }

and put it here:

string[] toppings = new string[4] { "One", "Two", "Three", "Four" };
Strings myString = new Strings(toppings);
string displayText = myString.displayText;
0
 

Author Comment

by:X3R
ID: 22854962
Now it gives me three errors:

The name 'toppings' does not exist in the current context

in this code:
for (int i = 0; i < toppings.Length; i++)

{

   if (i == 0)

                

   result = result.Append(toppings[i]);
 

   else
 

   result = result.Append(", ").Append(toppings[i]);

}

Open in new window

0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 125 total points
ID: 22854964
Change the word "toppings" to the word "args"
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22854967
This is just basic C# syntax. I suggest you to get yourself a C# book to help you.
0
 

Author Closing Comment

by:X3R
ID: 31512217
Thank you. Didn't spot the obvious ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 53
Need a complete list of ASP.NET page load events 3 28
Error in JQuery 5 39
Javascript and Jquery not firing 9 42
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now