-3

Getting errors when I am trying to save my inputs from textboxes to a list. I am using visual studio and a windows form application. What I want to do is to save the inputs from the textboxes to a list and then display the list in other forms. I have created one class with this code:

public class myClass
{
    public List<customer> customerIdInfo= new List<customer>();

    public class customer
    {
        string Id { get; set; }
        int phoneNumber { get; set; }
        string message { get; set; }
    }
}

Then I have this in my form with the textboxes:

private void nextForm_Click(object sender, EventArgs e)
    {
        Form2 Form1= new Form2(); 

        Form1.Show();

        class myClass= new myClass();
        customerIdInfo.Add(new customer{
                Id = txtCustomerId.Text;
                phoneNumber = txtPhonenumber.Text;
                message = txtMessage.Text;
         });

All the code in the form with the textboxes gets error messages anyone knowws how I can solve this problem?

Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
user3076791
  • 5
  • 2
  • 9
  • 6
    class myClass= new myClass(); ??? – dotNET Jan 12 '14 at 15:33
  • 2
    Form2 Form1= new Form2() ??? – Selman Genç Jan 12 '14 at 15:34
  • I am not sure how to write that but don't I have to write something like that to get the list and info from the other class. It might have to have different names like: class myClassA = new myClass(); – user3076791 Jan 12 '14 at 15:35
  • You are storing `txtPhoneNumber` as `int` but it is actually `string`. – Shulhi Sapli Jan 12 '14 at 15:35
  • What kind of error did you get? Please update your question with error messages, otherwise we can guess a lot.. – Alex Jan 12 '14 at 15:35
  • Form2 Form1 = new Form2() is to get to the next form in the program – user3076791 Jan 12 '14 at 15:36
  • Okay I will change it to a string but it doesn't solve all the problems. I get alot of diffferent errors some of them is: Invalid expression term 'class' ;expected syntax error ;; expected – user3076791 Jan 12 '14 at 15:38
  • You can solve the problem by reading and understanding the error messages. If you don't know what they mean, google them... –  Jan 12 '14 at 15:38
  • aren't object initializers expected to separate property assignments using commas, not semi-colons? (apart from the obvious mistakes that have been pointed out) – Wim Ombelets Jan 12 '14 at 15:40

2 Answers2

2

There were quite some errors in your original code. I have fixed them and commented on them, but Stack Overflow requires a certain level of understanding. Try to pick up a good C# book or read MSDN, so you can learn what the expected syntax is and how you can interpret compiler errors. This isn't exactly the last time you'll get them.

class myClass= new myClass();

You can only use class to declare a class. Here you declare a variable, which is done by naming its type or var:

myClass myClass = new myClass();
// Or
var myClass = new myClass();

Then in your object initializer:

new customer 
{
    Id = txtCustomerId.Text;
    phoneNumber = txtPhonenumber.Text;
    message = txtMessage.Text;
}

You use semicolons (;) where commas should be used (,):

new customer 
{
    Id = txtCustomerId.Text,
    phoneNumber = txtPhonenumber.Text,
    message = txtMessage.Text,
}

And you try to assign a string to an int:

phoneNumber = txtPhonenumber.Text

Which you can't do, it needs to be parsed:

phoneNumber = int.Parse(txtPhonenumber.Text)

You will also need to add error checking, because users can type in things that can't be parsed to an integer.

Then you try to access a variable named customerIdInfo in order to Add() a new customer, but that variable doesn't exist:

customerIdInfo.Add(...);

You want the field of the myClass instance:

myClass.customerIdInfo.Add(...);

And consider making it a property and naming things properly.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    Thank you very much. I will take your recommendation and read and try from a good C# book and watch more tutorials. – user3076791 Jan 12 '14 at 15:59
  • @user you should do that, but don't get too focused on reading and watching, you need to code to practice. – CodeCaster Jan 12 '14 at 16:22
  • Thanks! I will do that my plan is to watch tutorials and read from books that take up the same things in C# and code at the same time. Then go on to a new part in coding. Thank you very much for your tips @CodeCaster do you have any tips on any good books in C#? – user3076791 Jan 14 '14 at 20:54
  • @user I'm sorry, I don't have any concrete titles for you as I'm not familiar with your level of expertise and specific interests, but you I encourage you to just type "C#" on a site that sells books and find a book that matches your interests and level of expertise. Be sure to read the reviews, many say whether they found it easy as a beginner's book or steep to learn instead. – CodeCaster Jan 14 '14 at 23:22
  • I want to thank you so much for all of your help. I have found a couple of books and now it is time to become a master of coding ;) – user3076791 Jan 15 '14 at 04:29
1

class is a reserved word in C# used for defining a class, it can't be used to declare a variable. So the line class myClass = new myClass() won't compile, you want

myClass obj = new myClass();

Or if you are using C# 3.0 or later you can use the var keyword i.e.

var obj = new myClass();

With C# you shouldn't use Pascal casing for variables i.e. Form1, use camel casing

Form2 form = new Form2();

Also, customer is a nested class (not sure if that's deliberate) so unless your new customer code is arguably breaking the design here. Sounds like what you really want is something like

myClass obj = new myClass();
obj.AddNew("CustomerId", 01234567, "Some message");

FYI - telephone number as int is not a great idea, means you can't store values like +44 01234 56789 or (0)141 232-4334 etc.

James
  • 80,725
  • 18
  • 167
  • 237
  • Thanks I have changed to this now but I still get a lot of errors :( – user3076791 Jan 12 '14 at 15:44
  • 1
    @user3076791 that's because there are a lot of errors in your code. You should really go and read a book on C# because a lot of the issues you have are basic syntax errors. – James Jan 12 '14 at 15:45
  • Okay thanks I will do that. Might be the big problem when I am learning by myself I think I can do more than I really can so best to study harder :) – user3076791 Jan 12 '14 at 16:00