c# beginner help

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
For sch project . Where do i write the global array so that my array can be link to other forms ? For login and such .
To specify the last part . What i want is to not allow people to sign up using my admin username and password.
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
For sch project . Where do i write the global array so that my array can be link to other forms ? For login and such .
To specify the last part . What i want is to not allow people to sign up using my admin username and password.

C# like Java does not have global variables. You can declare static/class variables inside a Class. However you will want to design where variables belong to the class since you are using a OOPL.

Make use of database key constraint as a way to prevent duplication. When unique constraint are violated, specific error code is returned.
 

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
C# like Java does not have global variables. You can declare static/class variables inside a Class. However you will want to design where variables belong to the class since you are using a OOPL.

Make use of database key constraint as a way to prevent duplication. When unique constraint are violated, specific error code is returned.

what i think yours too chim already :x . Im doing windows form application . I want to like link my array from 1 form to another ?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
what i think yours too chim already :x . Im doing windows form application . I want to like link my array from 1 form to another ?

jiaweiii, I think you better would like to give a deeper thought why you are in this course learning this stuff ? If you think what I have just shared with you is too chim, you will want to reflect on whether you want to pursue into programming further.

To be frank, teaching you to use class variables for classes is any Object Oriented Programming 101. It is the basic among the basics. If this is too chim for you, I'm skeptical how far you can go.

Besides if you are really keen in learning how to do well in IT, you better reflect on your attitude towards things you don't know. The way I look at it, unless you want to be more proactive in learning, have that persistent in learning something, you will be overrun by your peers very quickly and I'm doubtful you will in time to come enjoy IT, because you have already considered the 101s chim.

If you have decided you want to learn something, you let me know and I will answer you more. Otherwise you can go figure it out yourself, which is the first thing you should consider in the first place.

Windows Forms are just the Windows UI components. If you are operating on C#, you are dealing with Classes and Objects as the basic elements of OOPL.
 

walkwaffle

Member
Joined
Feb 22, 2011
Messages
104
Reaction score
0
what i think yours too chim already :x . Im doing windows form application . I want to like link my array from 1 form to another ?

1) like this?
http://www.dotnetperls.com/global-variable

Other not so correct ways (yes i did them before):
- Pass Form1 via constructor to Form2
- Set Form1 in Form2 after instantiating Form1
- Set array in Form2 using Form1.
(when I say Form1, Form2... i do mean instance_of_Form1, instance_of_Form2)


2) depends on what your lessons teach you. Your lecturer should have created a problem that can be solved using what is taught. Though learning about database is also useful :D
 

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
C# like Java does not have global variables. You can declare static/class variables inside a Class. However you will want to design where variables belong to the class since you are using a OOPL.

Make use of database key constraint as a way to prevent duplication. When unique constraint are violated, specific error code is returned.

What you meant by declaring static / class variables inside a class isit the get and set method ?
And also havent really learn database yet .
 

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
1) like this?
http://www.dotnetperls.com/global-variable

Other not so correct ways (yes i did them before):
- Pass Form1 via constructor to Form2
- Set Form1 in Form2 after instantiating Form1
- Set array in Form2 using Form1.
(when I say Form1, Form2... i do mean instance_of_Form1, instance_of_Form2)


2) depends on what your lessons teach you. Your lecturer should have created a problem that can be solved using what is taught. Though learning about database is also useful :D
but i cant write my array at the class am i right ? And if i can how do i call on the array at my other form ?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
What you meant by declaring static / class variables inside a class isit the get and set method ?

Accessor methods mainly your getX and setX methods is not related to static/class variables/members

Read more at https://msdn.microsoft.com/en-us/library/79b3xss3.aspx

Your C# program will definitely start with a Class. That's where your Main method is found

If you want to pass your data structure around, you can do that from one class to another. Pass a reference from your Main class to other of your Classes or Forms

And also havent really learn database yet .

If you are not storing your data in your database, are you only storing them in memory using data structures ? If so, then you will need to perform search for existing userid that has already been created before you allow the new ones to be inserted into the in memory data structure.

There are naive solutions like a linear array or vector, or more complicated ones like hash table, binary tree, heap etc. Each have its good use, hash tables and binary trees are good for fast insert and fast retrieval at the expense of more complicated data structure management.

For starter, you can try the simple vector data structure to store your data. Then perform a linear search each time you attempt to insert new data into it to prevent duplicates.
 

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
Accessor methods mainly your getX and setX methods is not related to static/class variables/members

Read more at https://msdn.microsoft.com/en-us/library/79b3xss3.aspx

Your C# program will definitely start with a Class. That's where your Main method is found

If you want to pass your data structure around, you can do that from one class to another. Pass a reference from your Main class to other of your Classes or Forms



If you are not storing your data in your database, are you only storing them in memory using data structures ? If so, then you will need to perform search for existing userid that has already been created before you allow the new ones to be inserted into the in memory data structure.

There are naive solutions like a linear array or vector, or more complicated ones like hash table, binary tree, heap etc. Each have its good use, hash tables and binary trees are good for fast insert and fast retrieval at the expense of more complicated data structure management.

For starter, you can try the simple vector data structure to store your data. Then perform a linear search each time you attempt to insert new data into it to prevent duplicates.

something like public string pName
{
get { return Name; }
set { Name = value; }
}
public string pBrand
{
get { return Brand; }
set { Brand = value; }
}
for the get and set method ?
im currently storing the data in a array .

Im also wondering everytime after i log out from my form 2 to my form 1 . I cant use the user / pass i signed up with i got to re sign up why is that so ??
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
something like public string pName
{
get { return Name; }
set { Name = value; }
}
public string pBrand
{
get { return Brand; }
set { Brand = value; }
}
for the get and set method ?
im currently storing the data in a array .

That's the start of writing accessors in C#, which you can refer to https://msdn.microsoft.com/en-us/library/aa287786(v=vs.71).aspx if you need more information.

Now having this is not enough to achieve what you want, but this is the start.

Question is where would be the best place to declare your data structure to contain data ? Surely our C# program must have a starting Class with the Main method right ?

Your Windows Forms are just UI diagloues to interact with your user. At the end of the day, you need to visualise that the Main Class is where one would encapsulate the entirety of the Application you are designing

So if you feel the data belongs to the whole program, then declare your array inside the Main Class.

However if you feel that piece of data should belongs to ANOTHER CLASS because another class is playing the role of providing functionality to that piece of data, you should declare it in the other class

Think of Class as an Organisation, as a Person, as a Container, as a Group. In layman, your Family is one big Class, but you are another Class, your Mother is another Class and your Father is another Class. That's one possible way to organising things.

So your Father assuming is the breadwinner earn his salary and then he store this salary with him. He then decided to dedicate a portion of his salary to the Family and hence transfer a portion of this salary to the Family Class. Now your mother require the money for daily household expenses and took a portion into her own Class for storing. And she then assign you another portion out of her own for your daily expenses.

So while the money comes from your Father, surely when you consider how the flow of data and responsibility works, it get scatter all over the different Classes.

Of course, your Father can also when he collected his salary store immediately into the Family class and he does not have the functionality of storing the money himself. So when he need some money, he just draw from the Family Class, which act like a bank to him.

If your Father is a henpeck(nicely put as respect his wife), he might need to use your Mother's functionality to retrieve money because he is only exposed to the SET accessor to put money into the Family, but no GET accessor is made available to him. The only GET accessor to the money in the Family Class is via your Mother's drawMoney method.

This is a very very simplistic manner to look at Classes because it gets more complicated once you put Objects into the whole conversation. But I just want you to think how do you structure your data into which Class responsibility. Thats the whole idea of Object Oriented Design.

Next is if you are at liberality of using better containers, read up on https://msdn.microsoft.com/en-us/library/ybcx56wz.aspx, and these any of these functionalities to manage your data instead of a simple minded Array. Not that Array is not the right way, but it is a lower level data structure which requires more work to define functionalities around it for CRUD operations.
 

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
That's the start of writing accessors in C#, which you can refer to https://msdn.microsoft.com/en-us/library/aa287786(v=vs.71).aspx if you need more information.

Now having this is not enough to achieve what you want, but this is the start.

Question is where would be the best place to declare your data structure to contain data ? Surely our C# program must have a starting Class with the Main method right ?

Your Windows Forms are just UI diagloues to interact with your user. At the end of the day, you need to visualise that the Main Class is where one would encapsulate the entirety of the Application you are designing

So if you feel the data belongs to the whole program, then declare your array inside the Main Class.

However if you feel that piece of data should belongs to ANOTHER CLASS because another class is playing the role of providing functionality to that piece of data, you should declare it in the other class

Think of Class as an Organisation, as a Person, as a Container, as a Group. In layman, your Family is one big Class, but you are another Class, your Mother is another Class and your Father is another Class. That's one possible way to organising things.

So your Father assuming is the breadwinner earn his salary and then he store this salary with him. He then decided to dedicate a portion of his salary to the Family and hence transfer a portion of this salary to the Family Class. Now your mother require the money for daily household expenses and took a portion into her own Class for storing. And she then assign you another portion out of her own for your daily expenses.

So while the money comes from your Father, surely when you consider how the flow of data and responsibility works, it get scatter all over the different Classes.

Of course, your Father can also when he collected his salary store immediately into the Family class and he does not have the functionality of storing the money himself. So when he need some money, he just draw from the Family Class, which act like a bank to him.

If your Father is a henpeck(nicely put as respect his wife), he might need to use your Mother's functionality to retrieve money because he is only exposed to the SET accessor to put money into the Family, but no GET accessor is made available to him. The only GET accessor to the money in the Family Class is via your Mother's drawMoney method.

This is a very very simplistic manner to look at Classes because it gets more complicated once you put Objects into the whole conversation. But I just want you to think how do you structure your data into which Class responsibility. Thats the whole idea of Object Oriented Design.

Next is if you are at liberality of using better containers, read up on https://msdn.microsoft.com/en-us/library/ybcx56wz.aspx, and these any of these functionalities to manage your data instead of a simple minded Array. Not that Array is not the right way, but it is a lower level data structure which requires more work to define functionalities around it for CRUD operations.

So from what i understand now i should put my register array into the main class . And put a get set so i can retrieve the array later on . But how do i put array in the main class do i do it like string[] username = new string[100];
or is there other way ?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
So from what i understand now i should put my register array into the main class . And put a get set so i can retrieve the array later on . But how do i put array in the main class do i do it like string[] username = new string[100];
or is there other way ?

https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

For naming, you will want to use "usernames" since it is a plural data structure. Next you will need a counter since you need to know how much of the array has been filled. The job of incrementing the counter can be performed inside the set assessor method.

As you start thinking in encapsulating and delegating manner, you might want to design a simple Vector container which can allow you to add an element, remove an element, search for an element, replace an element etc.

So then your other parts of your program can create as many vector array as possible for other purposes.
 

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

For naming, you will want to use "usernames" since it is a plural data structure. Next you will need a counter since you need to know how much of the array has been filled. The job of incrementing the counter can be performed inside the set assessor method.

As you start thinking in encapsulating and delegating manner, you might want to design a simple Vector container which can allow you to add an element, remove an element, search for an element, replace an element etc.

So then your other parts of your program can create as many vector array as possible for other purposes.
By counter you those loops right?
What do you mean by encapsulating and delegating?
As of right now I got a search function and a add function I believe thats the element you mean?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
By counter you those loops right?
What do you mean by encapsulating and delegating?
As of right now I got a search function and a add function I believe thats the element you mean?

It means you create a Class called Vector with all the methods that operates on the array itself

Meaning if you intend to have a Car, you create a Car object. Since a Car can have more than 1 person(driver), you can have method called getIntoCar(Person p) method for the car along with other methods including like drive() method.

The OO idea is to have functionalities attached to the data structure. You wrap everything into an entity. This action is called encapsulation. While delegation in computing terminology is slightly different, I would consider you delegate the tasks of acting upon directly on the internals of the Class to its methods. Hence this form of software development methdolology creates modularisation and compartmentalisation. It also creates reusability which can result in productivity and efficient in your work.

You may have these methods, but who own these methods ? In your design, can you say your design have very distinct Classes and Objects concept where each entity has functionalities that operates upon itself and the data within in ?

Below I will show you a fragment of Java codes to give you the idea of a data structure that is self fulfilling

Code:
public class MyList {
  
  int[] data = new int[100];
  int last = 0;
  
  public int getFirst() { return data[0]; }
  public int getLast() { return data[last - 1]; }
  public int size() { return last; }
  public void insertLast(int i) { data[last++] = i; }
  public void insertFirst(int i) { shiftToRight(); data[0] = i; }
  private void shiftToRight() { /* perform shifting of elements from pos[i] to pos[i+1] */ };
}

class RunningProgram {
  public static void main(...) {
    MyList list1 = new MyList();
    MyList list2 = new MyList();
    
    list1.insertLast(1);
    list2.insertLast(list1.getFirst());
  }
}

See how the data structure of an array is all encapsulated into a Class where the methods it offers works on it's own data structure.

Of course there are a lot of other techniques and design patterns, no matter which type of OOPL you use, the concepts works using different syntax and approaches.
 

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
It means you create a Class called Vector with all the methods that operates on the array itself

Meaning if you intend to have a Car, you create a Car object. Since a Car can have more than 1 person(driver), you can have method called getIntoCar(Person p) method for the car along with other methods including like drive() method.

The OO idea is to have functionalities attached to the data structure. You wrap everything into an entity. This action is called encapsulation. While delegation in computing terminology is slightly different, I would consider you delegate the tasks of acting upon directly on the internals of the Class to its methods. Hence this form of software development methdolology creates modularisation and compartmentalisation. It also creates reusability which can result in productivity and efficient in your work.

You may have these methods, but who own these methods ? In your design, can you say your design have very distinct Classes and Objects concept where each entity has functionalities that operates upon itself and the data within in ?

Below I will show you a fragment of Java codes to give you the idea of a data structure that is self fulfilling

Code:
public class MyList {
  
  int[] data = new int[100];
  int last = 0;
  
  public int getFirst() { return data[0]; }
  public int getLast() { return data[last - 1]; }
  public int size() { return last; }
  public void insertLast(int i) { data[last++] = i; }
  public void insertFirst(int i) { shiftToRight(); data[0] = i; }
  private void shiftToRight() { /* perform shifting of elements from pos[i] to pos[i+1] */ };
}

class RunningProgram {
  public static void main(...) {
    MyList list1 = new MyList();
    MyList list2 = new MyList();
    
    list1.insertLast(1);
    list2.insertLast(list1.getFirst());
  }
}

See how the data structure of an array is all encapsulated into a Class where the methods it offers works on it's own data structure.

Of course there are a lot of other techniques and design patterns, no matter which type of OOPL you use, the concepts works using different syntax and approaches.

After creating a class and all the get , set i got this

private void CreateStock(int id, String pName, String pBrand, double pPrice, double pQuantity)
Phones[id] = new Stock(pName);
Phones[id].pBrand = pBrand;
Phones[id].pPrice = pPrice;
Phones[id].pQuantity = pQuantity;

But at my checkout how do i link so i can show the price at my messagebox?

string item = comboBoxItem.SelectedItem.ToString();
if (numericQuantity.Value == 1)
MessageBox.Show("One \n" + item);
else if (numericQuantity.Value == 2)
MessageBox.Show("Two \n" + item);
else
MessageBox.Show("Maximum only 2 per customer.");
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
After creating a class and all the get , set i got this

private void CreateStock(int id, String pName, String pBrand, double pPrice, double pQuantity)
Phones[id] = new Stock(pName);
Phones[id].pBrand = pBrand;
Phones[id].pPrice = pPrice;
Phones[id].pQuantity = pQuantity;

But at my checkout how do i link so i can show the price at my messagebox?

string item = comboBoxItem.SelectedItem.ToString();
if (numericQuantity.Value == 1)
MessageBox.Show("One \n" + item);
else if (numericQuantity.Value == 2)
MessageBox.Show("Two \n" + item);
else
MessageBox.Show("Maximum only 2 per customer.");

Override the ToString method of your Stock class. Read more at https://msdn.microsoft.com/en-us/library/system.object.tostring(v=vs.110).aspx

You obviously need to fetch out the Stock object from your array of Phones. Question for you, you used "id" to identify a stock object of name "pName". How would your "id" helps in the process of retrieving the Stock object of a particular type ? Obvious if id = 123, it doesn't really have any relationship with a particular type of phone, isn't it ?

So seriously you don't need an ID, at least from the standpoint of a data structure to store a list of things. The way you are doing things now is to throw everything into a dark box, which you can't see into it, except to take out one by one and see if it is the one you wanted. This is a searching process.

For a simplistic search across a list, this will be what you want to use https://msdn.microsoft.com/en-us/li...?cs-save-lang=1&cs-lang=csharp#code-snippet-1
 

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
Override the ToString method of your Stock class. Read more at https://msdn.microsoft.com/en-us/library/system.object.tostring(v=vs.110).aspx

You obviously need to fetch out the Stock object from your array of Phones. Question for you, you used "id" to identify a stock object of name "pName". How would your "id" helps in the process of retrieving the Stock object of a particular type ? Obvious if id = 123, it doesn't really have any relationship with a particular type of phone, isn't it ?

So seriously you don't need an ID, at least from the standpoint of a data structure to store a list of things. The way you are doing things now is to throw everything into a dark box, which you can't see into it, except to take out one by one and see if it is the one you wanted. This is a searching process.

For a simplistic search across a list, this will be what you want to use https://msdn.microsoft.com/en-us/li...?cs-save-lang=1&cs-lang=csharp#code-snippet-1

But my id is use to identify my the CreateStock(0, "Iphone 6 Plus", "Iphone", 1000, 8); the 0
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
But my id is use to identify my the CreateStock(0, "Iphone 6 Plus", "Iphone", 1000, 8); the 0

Sure I can understand what that ID does, but is it used as a reference anywhere else ? Is there somewhere in your program that gives a list of different phones type available along with the ID that correspond to it. If so, yes you can surely use it. But then if you ask yourself this question

If X refers to Y and Y refers to Z, that means X refers to Z. Am I right ? You are not doing things wrong, but I want you figure out why you are doing things this way.

Suppose I display a list of phones for your user to choose
"iPhone 6"
"Samsung S4"
"Google Nexus"

User choose an item from the list, how am I going to find the actual object that describe all the properties of that item from a list ?

If the list is in the following manner

"iPhone 6" -> 1
"Samsung S4" -> 0
"Google Nexus" -> 3

Will it helps in your search for the object from a list ? In fact, it seems straight forward to just without searching, the number on the right is the index into the object, isn't it ? This is what we call indexing or mapping.

Are you doing things this way now ? If not, you might as well remove that ID because it's not useful. Because at the moment, your phone name is as unique as the ID you have assigned which as no meaning on its own except to establish a transitive relation in the following manner

PHONE_NAME --> ID --> PHONE_OBJECT

Coming back to
Code:
string item = comboBoxItem.SelectedItem.ToString();
if (numericQuantity.Value == 1)
MessageBox.Show("One \n" + item);
else if (numericQuantity.Value == 2)
MessageBox.Show("Two \n" + item);
else
MessageBox.Show("Maximum only 2 per customer.");

Should your "item" be a the ID that reference to where the phone is ? Think about what is the item you are putting into the ComboBox ? Just a simple string ? Or perhaps the index matters more ? Or perhaps it could be a simple object that store a mapping ?
 
Last edited:

jiaweiii

Senior Member
Joined
Sep 3, 2013
Messages
812
Reaction score
0
Sure I can understand what that ID does, but is it used as a reference anywhere else ? Is there somewhere in your program that gives a list of different phones type available along with the ID that correspond to it. If so, yes you can surely use it. But then if you ask yourself this question

If X refers to Y and Y refers to Z, that means X refers to Z. Am I right ? You are not doing things wrong, but I want you figure out why you are doing things this way.

Suppose I display a list of phones for your user to choose
"iPhone 6"
"Samsung S4"
"Google Nexus"

User choose an item from the list, how am I going to find the actual object that describe all the properties of that item from a list ?

If the list is in the following manner

"iPhone 6" -> 1
"Samsung S4" -> 0
"Google Nexus" -> 3

Will it helps in your search for the object from a list ? In fact, it seems straight forward to just without searching, the number on the right is the index into the object, isn't it ? This is what we call indexing or mapping.

Are you doing things this way now ? If not, you might as well remove that ID because it's not useful. Because at the moment, your phone name is as unique as the ID you have assigned which as no meaning on its own except to establish a transitive relation in the following manner

PHONE_NAME --> ID --> PHONE_OBJECT

Coming back to
Code:
string item = comboBoxItem.SelectedItem.ToString();
if (numericQuantity.Value == 1)
MessageBox.Show("One \n" + item);
else if (numericQuantity.Value == 2)
MessageBox.Show("Two \n" + item);
else
MessageBox.Show("Maximum only 2 per customer.");

Should your "item" be a the ID that reference to where the phone is ? Think about what is the item you are putting into the ComboBox ? Just a simple string ? Or perhaps the index matters more ? Or perhaps it could be a simple object that store a mapping ?

the items im putting in the combobox is just a simple string.

but i dont get how do i link my createstock the quantity to my check out button .
so whenever checkout the quantity -1 .
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
the items im putting in the combobox is just a simple string.

but i dont get how do i link my createstock the quantity to my check out button .
so whenever checkout the quantity -1 .

If that's the case, then you will be having a problem finding your actual object among your array right ?

Why not consider the most easiest mapping. Let the selected index in your combo box correspond to the actual array index. That means if "iPhone 6" is at element index 0, then it will be the first item in the combo box when selected would gives the index 0 ? If "Samsung S4" is in the combo box list at element index 5, then it should correspond to the array at element index 5 too.

This approach is simple, but have an issue with ordering. That means I can't sort based on the combo box list without preserving the same order as with the array.

You can use this technique for start. Then explore on how you can insert a non-string object into the ComboBox. Read from http://stackoverflow.com/questions/...g-text-and-value-to-an-item-no-binding-source
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top