Good Day All,

I'm relatively new to programming. And I'm taking a C# course at the local University.
Is there a place I can go on DaniWeb to get help with coding questions?

Thanks,

R

Here :) But we like to see some effort put into solving the problem, not "give me a solution to this" type questions.

Good Day All,

I'm trying to validate that the value entered for the studentID is at 9 digits long. If the value is less than 9 digits, I need to pad the number with zeros before setting the studentID property. The following is what I've come up with so far:

namespace Student
{
    class Student
    {
        // Declare class student data fields, use array to hold the student standing
        private string studentStanding;
        private string firstName;
        private string lastName;
        int studentID;

        // Create property for freshman student standing
        // use the studentStanding array index to get and
        // set the student standing for each student
        public string StudentStanding
        {
            get { return this.studentStanding; }
            set { this.studentStanding = VerifyStanding(value); }
        }

        public string FirstName
        {
            get { return this.firstName; }
            set { this.firstName = value; }
        }

        public string LastName
        {
            get { return this.lastName; }
            set { this.lastName = value; }
        }

        // The studentID will only accepts nine digits
        public int StudentID
        {
            get { return this.studentID; }
            set { this.studentID = value; }
        }

        // Method to verify standing of freshman,
        // sophomore, junior, or senior .If the input
        // is not a standing then an error message is
        // returned.  If any of the strings in the 
        // switch statements is equivalent to the parameter
        // value then the value is returned

        private string VerifyStanding(string value)
        {
            string error = "Invalid Standing";
            switch (value)
            { 
                case "Freshman":
                case "Sophomore":
                case "Junior":
                case "Senior":
                    return value;
                default:
                    return error;

           }
        }

     }
}

Regards,

R

Edited 3 Years Ago by mike_2000_17: Fixed formatting

My advice for you is to use your institution recommended study material, that way you will get to learn quick and relative things, then once you get your grounds on programming you can as fly as you wish.

Please note that I'm not saying there is a standard way of learning programming, but its very normal for people to get introduction at schools after their first hello worlds programs, then they can start getting into details in fields which they are mostly interested at.

First to answer your question on validating your studentID if its less than 9 digists, if not preceding it with zero's.


1. you can add zero's to make its length 9, but doing so wont help because its of type int, and as you know adding 0 to 1 or any other number doesn't make a difference, if its added to its left. C# will remove any leading zero's in front of any number. So my advice for you is to use string as your base type, that way you stdID can be validated to any length you wish to.

in your property at the set part you need to perform those validation using statement like this:

public string studentID
{
[INDENT]get{return studentid;}[/INDENT]
[INDENT]set{
 if( Convert.ToString(value).Length > 9)
{
 //is valid
}
else
{
 //add 0's to the string
}
[/INDENT]
}

also take a look at your method,very...
case statement "must" be ended by break statements otherwise flow will continue to the next case, and eventually reach the default part, meaning none of you verification will be valid.

Good luck

// The studentID will only accepts nine digits
public int StudentID
{
get { return this.studentID; }
set { this.studentID = value; }
}

you above code doesn't validate anything, it just sets your variable to anything implicit variable value comes with.

private string VerifyStanding(string value)
{
string error = "Invalid Standing";
switch (value)
{
case "Freshman":
case "Sophomore":
case "Junior":
case "Senior":
return value;
default:
return error;

}
}

try using at least one break, just before default.

Thanks, mshauny.

I will change the type to string and work from there.

You can use an int to hold your student ID and pad it with zeros when you need to do so:

myStudent.StudentID.ToString("000000000");

What you need to ask yourself is "Do I ever need to perform math on the StudentID?". If not, then storing it as a string makes more sense, since it really isn't a number.

Edited 6 Years Ago by Momerath: n/a

The problems with this is that, no matter how many zero's you add to any integer number, it will always hold that specified value thus it length property when converted to string will always go back to its original. adding adding 7 zeros to 100 wont make its .Tostring().Legnth property become 10. and as for the code you have wriiten :

myStudent.StudentID.ToString("000000000");

your student object field "StudentID" is of type int, so unless its string you should give it values like 10000000, 2589215255, not 012125884, because that will result to Length being 8 not 9 as he wished.

Edited 6 Years Ago by mshauny: n/a

your student object field "StudentID" is of type int, so unless its string you should give it values like 10000000, 2589215255, not 012125884, because that will result to Length being 8 not 9 as he wished.

No, it won't. That's a format specifier for type of int and requires that the int be 9 digits long with leading zeros when displayed.

You should read http://blog.stevex.net/string-formatting-in-csharp/

yes, as an output it will make it 9, but i guess you never understood the question. he wanted to keep it with the length 9, not just for output.

and yes i know about the format specifier.

let me show you how this works:

class Program
    {
        static void Main(string[] args)
        {
            stringTest test = new stringTest();
            test.ID = 10;
            Console.WriteLine(test.ID.ToString("000000000"));
            Console.WriteLine(test.ID);
            
        }
    }

    class stringTest
    {
        private int id;

        public int ID
        {
            get { return id; }
            set { id = value; }
        }
    }
}

and the output is different:
1. 00000001
2. 10

see? those zero's are not permanently stored in your variable.

see? those zero's are not permanently stored in your variable.

No one said they were, you should re-read what I posted. Also, your output is wrong. That's not what you get when you run the code.

Edited 6 Years Ago by Momerath: n/a

yes my output might be wrong, because i never compiled the program, even my code might be wrong also. but at least you get the concept. or what?

and as the tittle tells you, "string formatting", not integer formatting. output formatting is easier because all types in .NET inherits from System.Object class, which defines the .ToString() public method. meaning virtually any type can be formatted accordingly, by all formats supported by the String class.

You need to understand before you post, and don't just assume things.

Good luck

Edited 6 Years Ago by mshauny: n/a

Comments
always check your code and output before posting

You need to understand before you post, and don't just assume things.

You posted untested code, erroneous output and made a blanket (and incorrect) statement about what I wrote without reading it and I'm the one who needs to understand before I post?

yes, as an output it will make it 9, but i guess you never understood the question. he wanted to keep it with the length 9, not just for output.

What Momerath offered was an alternative solution. Daniweb is a place for exchanging ideas and advise..there is no "one correct answer".

Your suggestion of storing the ID as a string is valid, as is the reasoning that if you don't perform math on the variable you needn't store it as a number type.

Momerath's suggestion was also perfectly valid. Rather than pad the value in storage (thus increasing the amount of data stored) you can store the number in its short form and then pad it whenever you need to display it.

It is down to the OP to decide which solution best fits his design. Please try to keep an open mind when posting here; as a community we encourage open discussion rather than shooting down someones idea because you think your own is more appropriate.

My apologies guys.

I accept i was unreasonable, but then we learn as we help each other out.

This article has been dead for over six months. Start a new discussion instead.