0

I run out of ideas i don't know what to do down here I need to make string Course Name outputs a message when it's longer than let's say 100 characters based in input of user

public string CourseName
    {
        get { return courseName; }
        set
        {
            if (courseName>value)//I can't fix this one
            {
                Console.WriteLine("You have typed more than 50 characters");
            }
            else
            {
                courseName = value;
            }
        }
    }
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
d4shm1r
  • 11
  • 6
  • In what context are you using this property? Because `Console.WriteLine` will do nothing to help the user if this is being used in a web application or a WPF app... – Heretic Monkey Nov 07 '19 at 22:00
  • I tried to convert string to int, i tried to do it with arrays, please if you tell me. As for that part I am sure that I didn't do it right if it was right why would I be here – d4shm1r Nov 07 '19 at 22:02
  • 1
    @d4shm1r - You don't want to convert the string to an int... you want to use the `Length` property. – Broots Waymb Nov 07 '19 at 22:03
  • @HereticMonkey Oh it's just a console app in visual studio – d4shm1r Nov 07 '19 at 22:04

3 Answers3

0

To check how many characters long a string is you check its .Length -property.

So value.Length > 100 to check if the incoming value is over 100 characters long.

Tomas
  • 3,384
  • 2
  • 26
  • 28
0
public string CourseName
{
    get { return courseName; }
    set
    {
        if (value != null && (value.Length > 50))
        {
            Console.WriteLine("You have typed more than 50 characters");
        }
        else
        {
            courseName = value;
        }
    }
}
tymtam
  • 31,798
  • 8
  • 86
  • 126
0

To check if a string's length exceed the max, you will need to use the Length property of the string class, it contains the length of the string in characters, e.g.:

if (value.Length > 50)
{
    Console.WriteLine("You have typed more than 50 characters");

    return;
}
courseName = value;

Note that you will also need to handle null input, sins string is a nullable type.

So you might want to do something like:

if (value == null)
{
    Console.WriteLine("The course name can not be null.");
}
else if (value.Length > 50)
{
    Console.WriteLine("You have typed more than 50 characters");
}
else
{
    courseName = value;
]
Vincent Bree
  • 425
  • 4
  • 10