Hello all nice coders :) i have a question about a function i have that uses an class as input. hmm prob easier to explain after i show some code!

class customer
{
    public string number, name, phone;

    public void newcustomer(customer custom)
    {
        //write to a database
        //using the strings above
    }
}



public partial class Form1 : Form
    {
        customer Kund = new customerr();
        kund.number="123";
        kund.name="AB";
        kund.phone="0604343";
        customer.newcustomer(customer);
    }

ok so now i input the class and it does its job without problem
but!
cant you do something with the function so it has to take for example "name + phone" to be able to create the customer and "number" can be optional
so i cant create blank customers or just a customer with just an phonenumber

is this possible to make in the function or other i guess i can do an if (kund.number!="") and stuff like that but it seems like not a good way to go

thank you for answers :)

You are mixing some things.
Try this as your Customer class

class Customer
    {
        // three constructors, more if you want to.
        public Customer()
        {
            number = string.Empty;
            name = string.Empty;
            phone = string.Empty;
        }

        public Customer(string EinKunde)
        {
            number = string.Empty;
            name = EinKunde;
            phone = string.Empty;
        }

        public Customer(string EinKunde, string EinTelefoon)
        {
            number = string.Empty;
            name = EinKunde;
            phone = EinTelefoon;
        }

        //more constructors can go here

        //properties of the class to set or get after class construction
        //these are automatic properties, the C# compiler keeps track of the field names
        public string number { get; set; }
        public string name { get; set; }
        public string phone { get; set; }

        //method of this class to write to a database
        public void WriteToDB()
        {
            //code to write number,name,phone to a DB
        }
    }

in your form class you could write something like this:

Customer Kund = new Customer();
            Kund.number = "123"; 
            Kund.name = "AB"; 
            Kund.phone = "0604343"; 
            Kund.WriteToDB();

You could do it in the constructor:

public class Customer
{
    private readonly string Name;
    private readonly string Phone;

    private string Number;

    public Customer(string name, string phone)
    {
        if (string.IsNullOrEmpty(name))
            throw new ArgumentNullException("name");
        Name = name;
        ...
    }
}

FYI, the purpose of readonly is to prevent the value from being changed after the class is initialized (the value can only be changed in the constructor).

Also, the newcustomer() method is redundant, you should either have an method with no parameters, like so:

public void NewCustomer()
{
    ...
}

Or use a static method, like so:

public static void NewCustomer(Customer customer)
{
    ...
}

If you use it the way you currently have it, you would be able to reference two customers (possibly the same one) in the method, which is unnecessary and creates confussion for other developpers.

nmaillet what? if i change to static void i cant use my varibles in the class above or call it in forms? do i need to change stuff or why do i get thoose errors? :O
the "if" you made was good for my taughts :) thank you for that one

ddanbe whats good about haveing 3 constrcutors instead of just sending the values in the function?
and sorry if i didnt write it but the database i need to write to uses ref in the code to write to the database and it didnt allow properties.
im not allow to go into the database specific things i think because where i work they paid for the manual with code examples and stuff.
but its always good to know anyway :)

You could have 100 constructors if you wanted to.
I was just pointing out that if you like to use a constructor with parameters please do so.
If you like to use the default constructor, (the one with no parameters) and set all the fields of your object like I did in my code, please do. Then let the object write itself to the database by using the method WriteToDB.

If you went with your method, you would have something like this:

public void NewCustomer(Customer customer)
{
    ...
    command.Parameters.Add("@name", SqlDbType.NVarChar, 30, customer.Name);
    ...
}

or it could look like this:

public void NewCustomer(Customer customer)
{
    ...
    command.Parameters.Add("@name", SqlDbType.NVarChar, 30, this.Name);
    ...
}

Note that customer.Name references the Customer object you passed in to the method, and this.Name references the Customer object that you are calling the method on. Generally, when you have an instance method (non-static), you perform some action based on the current instance of the class, but when you pass in another object of the same class, which one are you using to update the database with?

I wouldn't necessarily recommend using a static method in this case, but you definately shouldn't be using an instance method with another Customer object as a parameter. Hope that clears things up a bit; if not, let me know.

    class customer
    {
        struct variables
        {   
            public string number, name, phone, etc , many , many , more;
        }

        public void newcustomer(variables customvariables)
        {
            //write to a database
            //using the strings above
            //tex customvariables.name
        }

    }



    public partial class Form1 : Form
    {
        customer.variables customvariables = new customer.variables();
        customer Kund = new customerr();

        customvariables.number = Textbox1.Text;     //input using the windows forms
        customvariables.name = Textbox2.Text;
        customvariables.phone = Textbox2.Text;

        customer.newcustomer(customer);     //use the function to write the data
    }

hmm so yea i changed it abit but dont know it its worth the truble having the struct or if it does any good at all....
and i guess i could have everyting in the Form1.cs
but this will be quite a large program and will quickly become messy i belive
so made one .cs for every subject (customers, orders, etc)

is this any better then before? :) any taughts?
i see what you mean by it being unneccesary now nmaillet sience i could already access the variables like i had it before xD

Haven't we already gone through this. Why are you using nested types? And why are you using structs? Structs are fine for a small number of variables, but as it grows, it starts taking up alot of valuable space on the stack. The heap is there for a reason, to hold large amounts of data. And in either case, string is still a class which allocates its character arrays on the heap. One common recommendation I keep hearing is that a struct should be 16 bytes or under. On most 32-bit architectures, and string is going to be a 4 byte pointer, that's 4 strings in a struct (2 on most 64-bit architectures).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.