Hello, I have a question about set and get, I tried to search the forum for answers but i didnt find what i wanted. Sry if this question exists:$

1. How do i check that the StudentId to be at least 6 characters using the set?
Can i do it with set or I have to create a new method to store the studentId to the variable and check the length there?

2. As you can see for the array of courses I have created methods instead of using get and set. I couldn't figure out how to use get and set with arrays. Can you please tell me if this is possible and how I can do it?

Thanks in advance

public class StudentsInfo
        {
            public string studentId { get; set; }
            public string name { get; set; }
            public string surname { get; set; }
            public string phone { get; set; }
            private string[] courses = new string[4];
           
            /*
             * Add courses.
             * 
             */
            public void addCourse(string[] tempCourse)
            {

                for (int i = 0; i <= 3; i++)
                {
                    courses[i] = tempCourse[i];
                }
            }

            public void getCourses(ref string[] tempCourses)
            {
                for (int i = 0; i <= 3; i++)
                {
                    tempCourses[i] = courses[i];
                }
            }
 
        };

public string studentId { get; set; } is just a no nonsence syntax for difining a property.
If you want to check some values first use the "normal" syntax.

Check this out, and let me know if its helps:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Okt17GetSet
{
    class Program
    {
        List<StudentInfo> studentList = new List<StudentInfo>();

        static void Main(string[] args)
        {
            
            Program pro = new Program();
            pro.GettingStudents();
        }

        private void GettingStudents()
        {
            bool bChecking = false;
            string strID = null;
            string strName = null;

            while (!bChecking)
            {
                Console.WriteLine("Please, insert students ID:");
                strID = Console.ReadLine();
                if (strID.Length >= 6)
                {
                    while (!bChecking)
                    {
                        Console.WriteLine("Please insert student`s name:");
                        strName = Console.ReadLine();
                        if (strName.Contains(" "))
                            bChecking = true;
                        else
                            Console.WriteLine("Student needs to have name and last name...");
                    }
                }
                else
                {
                    Console.WriteLine("Student`s ID is not long enough, it must be at least 6 characters...");
                }
            }
            //when all ok, do an insertion into list of students:
            StudentInfo info = new StudentInfo();
            info.StudentID = strID;
            info.Name = strName;
            studentList.Add(info);

            Console.WriteLine("New student data saved into list!");
            Console.WriteLine("ID: {0}, NAME: {1}", strID, strName);
            Console.ReadLine();
        }
    }

    public class StudentInfo
    {
        private string studentID;
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string StudentID
        {
            get { return studentID; }
            set { studentID = value; }
        }
    }
}

Are you working with console window or forms?

If you are working with forms, in your textbox.text method you could validate the string by:

if (textbox1.Text == 6) // this is to check if the string input is equal to 6 chars.
{
  your code here
}
else
{
  MessageBox.Show("The data you input is not valid or less than expected)
}

if you want to set only 6 digits

then..

private void studentID;

public int StudentID
        {
            get { return detailsID; }
            set 
            {
                if (studentID == 6)
                {
                    studentID = value;
                }
            }

this will definetely help you!

This is an example where is shown everything you need. I did it only for you:
... I hope you like it, note: do a copy/paste to CONSOLE application, and try to run it!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Okt17GetSet
{
    class Program
    {
        List<StudentInfo> studentList = new List<StudentInfo>();

        static void Main(string[] args)
        {
            bool bChecking = false;
            Program pro = new Program();
            Console.WriteLine("Hi there,");
            while (!bChecking)
            {
                Console.WriteLine("Do you want to ADD new or SEARCH for the student?");
                Console.WriteLine("(1 - add, 2 - search, 3 - exit program)");
                string strQuestion = Console.ReadLine();
                if (strQuestion != null)
                {
                    if (strQuestion == "1")
                        pro.AddingStudent();
                    else if (strQuestion == "2")
                        pro.GettingStudent();
                    else if (strQuestion == "3")
                        bChecking = true;
                    else
                    {
                        Console.WriteLine("Wrong answer, please do it again...");
                        continue;
                    }
                }
            }
            Console.WriteLine("bye, bye...");
            System.Threading.Thread.Sleep(1000);
            Environment.Exit(0);
        }

        private void AddingStudent()
        {
            bool bChecking = false;
            string strID = null;
            string strName = null;

            while (!bChecking)
            {
                Console.WriteLine("Please, insert students ID:");
                strID = Console.ReadLine();
                if (strID.Length >= 6)
                {
                    while (!bChecking)
                    {
                        Console.WriteLine("Please insert student`s name:");
                        strName = Console.ReadLine();
                        if (strName.Contains(" "))
                            bChecking = true;
                        else
                            Console.WriteLine("Student needs to have name and last name...");
                    }
                }
                else
                {
                    Console.WriteLine("Student`s ID is not long enough, it must be at least 6 characters...");
                }
            }
            //when all ok, do an insertion into list of students:
            //1. ADDING NEW STUDENT:
            StudentInfo info = new StudentInfo();
            info.StudentID = strID;
            info.Name = strName;
            studentList.Add(info);

            Console.WriteLine("New student data saved into list!");
            Console.WriteLine("ID: {0}, NAME: {1}", strID, strName);
            Console.WriteLine("(press enter to continue...)");
            Console.ReadLine();
        }

        private void GettingStudent()
        {
            bool bChecking = false;
            bool bFound = false;
            string strSearch = null;
            string strValue1 = null;
            string strValue2 = null;

            Console.WriteLine("Find the student!");
            while (!bChecking)
            {
                Console.WriteLine("How do you want to search?");
                Console.WriteLine("1 - by ID, 2 - by name");
                strSearch = Console.ReadLine();

                if (strSearch != "" && (strSearch != "1" || strSearch != "2"))
                {
                    if (strSearch == "1")
                        Console.WriteLine("Please insert student`s ID:");
                    else if (strSearch == "2")
                        Console.WriteLine("Please insert stunden`s name:");
                    strValue1 = Console.ReadLine();

                    if (strValue1 != "")
                    {
                        if ((strSearch == "1" && strValue1.Length >= 6) || strSearch == "2")
                        {
                            foreach (StudentInfo info in studentList)
                            {
                                if (strSearch == "1")
                                {
                                    strValue2 = info.StudentID;
                                    if (strValue1 == strValue2)
                                    {
                                        bFound = true;
                                        Console.WriteLine("Match found!");
                                        Console.WriteLine("Student with ID {0}, has the NAME: {1}", strValue1, info.Name);
                                        break;
                                    }
                                }
                                else if (strSearch == "2")
                                {
                                    strValue2 = info.Name;
                                    if (strValue1 == strValue2)
                                    {
                                        bFound = true;
                                        Console.WriteLine("Match found!");
                                        Console.WriteLine("Student with the NAME: {0}, has the ID: {1}", strValue1, info.StudentID);
                                        break;
                                    }
                                }
                            }
                            if (!bFound)
                            {
                                Console.WriteLine("No match found...");
                                bFound = false;
                            }
                            bool bQuestion = false;
                            while (!bQuestion)
                            {
                                Console.WriteLine("Do you want to search again? (1 - Yes, 2 - No)");
                                string strQueston = Console.ReadLine();
                                if (strQueston == "1")
                                    bQuestion = true;
                                else if (strQueston == "2")
                                {
                                    bQuestion = true;
                                    bChecking = true;
                                }
                            }

                        }
                        else
                        {
                            if (strSearch == "1")
                                Console.WriteLine("ID has to be at least 6 characters long...");
                            continue;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please insert something...");
                        continue;
                    }
                }
                else
                {
                    Console.WriteLine("Wrong answer, please do it again...");
                    continue;
                }
            }
        }
    }

    public class StudentInfo
    {
        private string studentID;
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string StudentID
        {
            get { return studentID; }
            set { studentID = value; }
        }
    }
}
commented: Nice effort. +8

if (textbox1.Text == 6) // this is to check if the string input is equal to 6 chars.
{
your code here
}
else
{
MessageBox.Show("The data you input is not valid or less than expected)
}

if you want to set only 6 digits
then..

private void studentID;
public int StudentID
{
get { return detailsID; }
set
{
if (studentID == 6)
{
studentID = value;
}
}

this will definetely help you!

this will defenately NOT help you!
What you have stated is completely wrong! Please do not reply with such answers.

1. If you want to check the lenght of the string, so how many characters string has you have to do with string.Lengt property, and not with equalizing to the number, like you did (string == 6) - COMPLETELY WRONG; What you did its only comparing if the string has number 6, nothing else.

2.Your set method is again completely wrong. How the hack do you compare if the sting has 6 characters?
btw, do you know what it means 6 characters? This is not a string "6", but its a string that consists 6 different letters, number or signs (12ab,.) - that means 6 characters long.

Anyway, as I said, Get,Set method is not meant to do any checking (even if its possible to do it), but it`s meaning shows it`s name - so to SET some value up, that you can later retive (GET) this value (some where else)! Understood?

EDIT:
to check if the textBox1 has at least 6 characters entered you do:

if(textBox1.Lenght >= 6)
{
    //code if textBox HAS 6 or more characters
}
else
{
    //code if the textBox DOESN`T HAVE 6 charactes (so less then 6)
    //usually here come an error, or info. messageBox.
}

Thats all.
And please, do not post these kind of posts anylonger - you teach ppl wrong!
No offence,
Mitja

commented: I dont see whats wrong with this post it is correct! +1

Anyway, as I said, Get,Set method is not meant to do any chcking (even if its possible to do it), but its meaning shows its name - so to SET something up, that you can later retive (GET) this something! Understood?

And please, do not post that kind of post anylonger - you teach ppl worng!
No offence,
Mitja

Maybe you should take your own advice. get/set is meant to do checking, you can see it in the documentation of properties. From that page "Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code." You can also see it used as a verification tool on the very next page

So, to answer your question, when you call a set method, a variable called value becomes available and is the same type as that of the property. So to check you'd do something like:

private String studentId;
public String StudentId {
    get {
        return studentId;
    }
    set {
        if (value.Length == 6) {
            studentId = value;
        }
    }
}

This was not a question, I know how to do a set method that can do a comparation, but in my opinion its better to use methods for these kind of actions.


Let me quote your question:

"1. How do i check that the StudentId to be at least 6 characters using the set?"

the answer I provided is to CHECK if the student id is == to 6 "CHAR"

of course you can change the comparator to <= 6 or >= or !=.

now I also advised to validation before setting the properties using if == to 6 set the value.

else return exception or mbox.

now whats wrong?

Im not sure whats your question now.

maybe you want to check if the data is an alpha or numeric?

then you have to use regular expression.

you should have said that not the word "CHAR."

and you wasted half of your time to dispute for my reply.

didnt hear thanks anyway.

Wow, thank you very much for alla of your response!! :D

I am working with windows forms, and basically my question was if i can check the variable via set before is set :s. I hope you understood :p

Mitja Bonca thank you very much for your solution/suggestion is exactly what I wanted, but i will convert it to Windows Form implementation and not console. Thanks anyway!

Momerath i tried your suggestion, my question is: What happens if the

if (value.Length == 6) {

studentId = value;

if statement evaluates as false? The execution transfer backs to the main programm?
the studentId gets a value of NULL?

I have read this link I didn't find what happens if the IF statements evaluates to FALSE.:s

I started to implement ddanbe suggestion with "normal syntax". Basically what Mitja Bonca already wrote ;)

As new in C# i am trying to understand how set and get can be used to write efficient programs. :)

What happens? Nothing. It will not set it! Thats why its better to avoid such coding (as I explained up there). Its better to use code, and let user know that the (in your example) id is not long enough.

Im glad I can help,
bye
Mitja

To answer your last question, studentId would have the last set (or default value if nothing has ever been set). You'd need to deal with that situation somewhere in your code (you can deal with it in the set method if you want), just like you'd have to deal with it no matter how you decide to write the code (just like Mitja did in his first post of code in lines 41-44).

(you can deal with it in the set method if you want)

Sorry for the additional post but how can i do that?
I mean lets say i have

set{
if (value==6)
myVariable = value;
}

So if it is not 6 i can say

MessageBox.show("Error");

and stop the program from inserting the student (with the wrong ID) at all by breaking or something else? :s

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.