Hello, i am programming an API so far i've coded so i can create new customers but my taughts here is that i have a function (i think that is the right word for it)

public void writeCustomer (string CustomerNumber, string CustomerOrgNumber, string CustomerName, string PostAdr, string PostAdr2, string GLN, string VisitingAdr ) 
{
    //write to the API code code
}

but is this an good way to go? before im finnished i will have about 10-15 or more parameters that need to be in there aswell.
is it good to use struct to save memory? with all the strings?
or do you know some other solution to it?

and how to i code so i make some of the inputs optional and some needed?
like post Address 2 can be optional
and name + cutsomernumber is needed to make the customer?

If you really need all those, why not use a class for the customer data. Then you can use a single parameter, and the class can handle additional checks if needed (checking required fields before saving).

Edited 4 Years Ago by pritaeas

You will usualy have a list of custumers, so you'll most often want to have a Custumer class as pritaeas pointed out.

A struct wouldn't save any "memory", it would pretty well be equivalent. Depending on the architecture of your system and how the compiler wants to handle it, all data in a struct would be passed on the stack, so it would probably be better to use a class. A class would only require a pointer to be passed, while the class members would be allocated on the heap. FYI, passing a string only passes a pointer, not the entire character array.

Just so you are aware, optional parameter were implemented in C# 4.0 (that is, C# with .NET Framework 4.0). All optional parameters must follow all required parameters and requrie a default value (which can be null for ref types). For instance (not good practice, but shows how it's done):

int Sum(int a, int b, int c = 0, int d = 0)
{
    return a + b + c + d;
}

Then, optional parameters are determined by there order:

Sum(1, 2);       //a = 1, b = 2
Sum(1, 2, 3);    //a = 1, b = 2, c = 3
Sum(1, 2, 3, 4); //a = 1, b = 2, c = 3, d = 4

and you can explicitly reference an optional parameter:

Sum(1, 2, d: 4); //a = 1, b = 2, d = 4

but how can i use a single parameter i still need to write in all thoose from my windows forms? havnig a class for the customers is prob best but i will still need a function in that class similiar to what i already have?

nmaillet thank you! can you do that with functions that dont need a return value? because my function only needs the input not change anything ot give back

its hard to explain all this but... i dont know exactly how i will build the program structure in a good way i know how to do all the functions the API need like add data to customers and stuff.
but need to think about how to manage it all
the program will create,read, change and delete customer data or articles and suppliers

Main.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Test
{
    class test
    {
        struct variables
    {
            public int errtype;
            public int pData;
            public string errortext;     
    }
        public void FunAdkOpen()
        {
            //opens the database via the program
        }
        public void SkrivaKund (string KundNummer, string KundOrgNummer, string KundNamn, string PostAdr, string PostAdr2, string GLN, string besokAdr )   
        {
           //write write to the database and errorcheck 
        }
   }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        test myObject = new test();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myObject.OpenDatabase();
            myObject.SkrivaKund(
                tb1KundNum.Text, tb2OrgNum.Text, 
                tb3KundNamn.Text, tb4PostAdr.Text, 
                tb5PostAdr2.Text, tb6GLN.Text, 
                tb7BesokAdr.Text);
            myObject.CloseDatabase();
        }

        private void tb1KundNum_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb2OrgNum_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb3KundNamn_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb4PostAdr_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb5PostAdr2_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb6GLN_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb7BesokAdr_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

You think its good to ahve a class for customer and for articles and for suppliers and all thoose and call them in form1? and no Struct? the one i worked for likes struct xD so i wont overload so much he thinks its messy
thank you for your taughts

Edited 4 Years Ago by Dendei

but how can i use a single parameter i still need to write in all thoose from my windows forms? havnig a class for the customers is prob best but i will still need a function in that class similiar to what i already have?

You would start with a class like so:

public class Customer
{
    public string CustomerNumber;
    public string CustomerOrgNumber;
    ...
}

Your method would look something like this:

public void writeCustomer(Customer customer)
{
    ...
}

Then create, update and pass your customer data as a single parameter like this:

Customer customer = new Customer();
customer.CustomerNumber = "123-45";
customer.CustomerOrgNumber = "67-890";
...
writerCustomer(customer);

The example Customer class I gave is pretty simple, and you should probably use properties, add logic, etc.

nmaillet thank you! can you do that with functions that dont need a return value? because my function only needs the input not change anything ot give back

Yeah, it doesn't matter if there's a return type.

Comments
Well said!

thank you again that is very good start i dont know much about properties but the functions from the API that i need to use so it can write to the database only do one thing at a time but if i change the textbox fields and point them to the strings i think it will do the job fine
just need a good bedrock to build upon so i dont have to remake if we need to add more stuff the program will do ^^

i will mark this thread as solved thank you all if you have tips on how i could structure the program feel free to post anyway :) if get into truble again I think it will be more specific so i wont ask it here

ohh one last question about this subject, struct vs class in this case with the all the strings?
should i do i struct with get set? or just public strings in the class and use them?

classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

dont understand fully but i will use it to store ints and strings? so its good to use it like that but not hold methods?

namespace Test
{

    class Customer
        {
            public struct varibales
            {
                string abc;
            }
        }
    public void writecustomer ()  
    {
        //do stuff
    }
}

Form.cs

namespace test
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            Customer ClassCustomer = new Customer();
            Customer.variables.abc="hello";
        }
    }
}

cant this work?

No, that example you gave shows nested classes, which is completely different from what we have been discussing. Let's simplify a bit, here's a class:

public class Customer
{
    public string str;
}

Here's an equivalent struct:

public struct Customer
{
    public string str;
}

They act the same (for the most part) and the differences are most obvious to developers from a C/C++ or some other low-level language background.

First, a struct is completely stored on the stack (you may want to read up on the stack and heap). A class is stored on the heap, and a pointer to the class object is stored on the stack. You generally want to avoid putting large amounts of data on the stack, as it is much more limited than the heap. That said, there are a lot of benifits when using a struct, but you really need to understand the difference before making that call.

There are quite a few differences other than where they are stored (most of which root from there location in memory) and that is all discussed in the referenced article.

Alright thank you nmaillet :) i'll study up on nested classes heap and stack

This question has already been answered. Start a new discussion instead.