using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace OrganizationProfile
{
public partial class frmRegistration : Form
{
private string _FullName;
private int _Age;
private long _StudentNo;
private long _ContactNo;
public frmRegistration()
{
InitializeComponent();
}
private void frmRegistration_Load(object sender, EventArgs e)
{
string[] ListOfProgram = new string[]{
"BS Information Technology",
"BS Computer Science",
"BS Information Systems",
"BS in Accountancy",
"BS in Hospitality Management",
"BS in Tourism Management"
};
string[] ListOfGender = new string[]{
"Male", "Female", "Other" };
for (int i = 0; i < ListOfProgram.Length; i++)
{
try
{
if (! string.IsNullOrWhiteSpace(ListOfProgram[i]))
{
cbProgram.Items.Add(ListOfProgram[i]);
}
else
{
MessageBox.Show("Item at index " + i + " is null or whitespace.");
}
}
catch (IndexOutOfRangeException ex)
{
MessageBox.Show($"An error occurred: {ex.Message}");
}
}
for (int i = 0; i < ListOfGender.Length; i++)
{
try
{
if (!string.IsNullOrWhiteSpace(ListOfGender[i]))
{
cbGender.Items.Add(ListOfGender[i]);
}
else
{
MessageBox.Show("Gender item at index " + i + " is null or whitespace.");
}
}
catch (IndexOutOfRangeException ex)
{
MessageBox.Show($"An error occurred while adding Gender: {ex.Message}");
}
}
}
}
public long StudentNumber(string studNum)
{
try
{
if (string.IsNullOrEmpty(studNum))
{
throw new ArgumentNullException("Student Number cannot be empty");
}
if (!long.TryParse(studNum, out _StudentNo))
{
throw new FormatException("Invalid Student Number Input");
}
if (_StudentNo < 1000000000 || _StudentNo > 9999999999)
{
throw new OverflowException("Student Number is out of valid range.");
}
}
catch (FormatException ex)
{
MessageBox.Show(ex.Message);
}
catch (OverflowException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentNullException ex)
{
MessageBox.Show(ex.Message);
}
return _StudentNo;
}
public long ContactNo(string Contact)
{
try
{
if (string.IsNullOrEmpty(Contact))
{
throw new ArgumentNullException("Contact Number cannot be empty");
}
if (Regex.IsMatch(Contact, @"^[0-9]{10,11}$"))
{
_ContactNo = long.Parse(Contact);
}
else
{
throw new FormatException("Invalid Contact Number Input");
}
if (_ContactNo < 1000000000 || _ContactNo > 99999999999)
{
throw new OverflowException("Contact Number is out of valid range.");
}
}
catch (FormatException ex)
{
MessageBox.Show(ex.Message);
}
catch (OverflowException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentNullException ex)
{
MessageBox.Show(ex.Message);
}
return _ContactNo;
}
public string FullName(string LastName, string FirstName, string MiddleInitial)
{
try
{
if (string.IsNullOrWhiteSpace(LastName) || string.IsNullOrWhiteSpace(FirstName) || string.IsNullOrWhiteSpace(MiddleInitial))
{
throw new ArgumentNullException();
}
if (Regex.IsMatch(LastName, @"^[a-zA-Z]+$") && Regex.IsMatch(FirstName, @"^[a-zA-Z]+$") && Regex.IsMatch(MiddleInitial, @"^[a-zA-Z]+$"))
{
_FullName = LastName + ", " + FirstName + ", " + MiddleInitial;
}
else
{
throw new FormatException();
}
}
catch (FormatException)
{
MessageBox.Show("Invalid First Name and Last Name Input");
}
catch (ArgumentNullException)
{
MessageBox.Show("Empty Input");
}
return _FullName;
}
public int Age(string age)
{
try
{
if (string.IsNullOrEmpty(age))
{
throw new ArgumentNullException("Age cannot be empty");
}
if (Regex.IsMatch(age, @"^[0-9]{1,3}$"))
{
_Age = Int32.Parse(age) ;
}
else {
throw new FormatException("Invalid Age Input");
}
if (_Age < 1 || _Age > 999)
{
throw new OverflowException("Age is out of valid range.");
}
}
catch (FormatException ex)
{
MessageBox.Show(ex.Message);
}
catch (OverflowException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentNullException ex)
{
MessageBox.Show(ex.Message);
}
return _Age;
}
private void btnRegister_Click(object sender, EventArgs e)
{
var frmConfirmation = new frmComfirmation();
frmConfirmation.Show();
StudentInformationClass.SetFullName = FullName(txtLastName.Text,
txtFirstName.Text, txtMiddleInitial.Text);
StudentInformationClass.SetStudentNo = StudentNumber(txtStudentNo.Text);
StudentInformationClass.SetProgram = cbProgram.Text;
StudentInformationClass.SetGender = cbGender.Text;
StudentInformationClass.SetContactNo = ContactNo(txtContactNo.Text);
StudentInformationClass.SetAge = Age(txtAge.Text);
StudentInformationClass.SetBirthDay = datePickerBirthday.Value.ToString("yyyyMM-dd");
frmConfirm frm = new frmConfirm();
frm.ShowDialog();
}
}
} Write, Run & Share C# code online using OneCompiler's C# online compiler for free. It's one of the robust, feature-rich online compilers for C# language, running on the latest version 8.0. Getting started with the OneCompiler's C# compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C# and start coding.
OneCompiler's C# online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
string name;
name = Console.ReadLine();
Console.WriteLine("Hello {0} ", name);
}
}
}
C# is a general purpose object-oriented programming language by Microsoft. Though initially it was developed as part of .net but later it was approved by ECMA and ISO standards.
You can use C# to create variety of applications, like web, windows, mobile, console applications and much more using Visual studio.
| Data Type | Description | Range | size |
|---|---|---|---|
| int | To store integers | -2,147,483,648 to 2,147,483,647 | 4 bytes |
| double | to store large floating point numbers with decimals | can store 15 decimal digits | 8 bytes |
| float | to store floating point numbers with decimals | can store upto 7 decimal digits | 4 bytes |
| char | to store single characters | - | 2 bytes |
| string | to stores text | - | 2 bytes per character |
| bool | to stores either true or false | - | 1 bit |
datatype variable-name = value;
When ever you want to perform a set of operations based on a condition or set of few conditions IF-ELSE is used.
if(conditional-expression) {
// code
}
else {
// code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement) {
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type[] array-name;
Method is a set of statements which gets executed only when they are called. Call the method name in the main function to execute the method.
static void method-name()
{
// code to be executed
}