C#
C#

help

Root Question Message

multisportz.hq
multisportz.hq1/26/2023
❔ What mistake did I make?

attached is my code, but it doesnt generate anything for the objects and has like default values of 0 and the default time for those DateTime objects
Thaumanovic
Thaumanovic1/26/2023
@501358794161389599 could you post the output of the program?
multisportz.hq
multisportz.hq1/26/2023
Thaumanovic
Thaumanovic1/26/2023
Ahh, I see
Thaumanovic
Thaumanovic1/26/2023
            name = this.name;
            basin = this.basin;
            year = this.year;
            maximumspeed = this.maximumspeed;
            maximumcategory = this.maximumcategory;
            startdate = this.startdate;
            enddate = this.enddate;
Thaumanovic
Thaumanovic1/26/2023
You have this backwards.
multisportz.hq
multisportz.hq1/26/2023
ohh
Thaumanovic
Thaumanovic1/26/2023
It should be this.name = name
multisportz.hq
multisportz.hq1/26/2023
its this first
multisportz.hq
multisportz.hq1/26/2023
ahh
multisportz.hq
multisportz.hq1/26/2023
ok
multisportz.hq
multisportz.hq1/26/2023
yeah i forgot abou thtat
multisportz.hq
multisportz.hq1/26/2023
ill fix that, thanks
multisportz.hq
multisportz.hq1/26/2023
yeah it works now
multisportz.hq
multisportz.hq1/26/2023
@149196898606120960 do you know how to remove the date/time?
multisportz.hq
multisportz.hq1/26/2023
i mean
multisportz.hq
multisportz.hq1/26/2023
just the time
multisportz.hq
multisportz.hq1/26/2023
like i dont want it to include 12:00:00 am
multisportz.hq
multisportz.hq1/26/2023
this is something i can search up actually if you dont know
multisportz.hq
multisportz.hq1/26/2023
@149196898606120960 for some reason i keep getting an erorr of converting from string to system.datetime
multisportz.hq
multisportz.hq1/26/2023
when i try to remove the time
multisportz.hq
multisportz.hq1/26/2023
@446416451230760990 it’s in this thread
multisportz.hq
multisportz.hq1/26/2023
But the code I attached originally does not include the fact that I want to convert the date time to just daye
multisportz.hq
multisportz.hq1/26/2023
But idk what to include to do that
jcotton42
jcotton421/26/2023
can you show that bit of code?
and what line is the error on?
jcotton42
jcotton421/26/2023
also, not sure why you did the properties manually
Ayymoss
Ayymoss1/26/2023
What framework is this? The verbosity in properties is confusing me 😄
Ayymoss
Ayymoss1/26/2023
namespace HurricaneObject;

public static class Program
{
    private static void Main()
    {
        var hurricanes = new List<Hurricane>
        {
            new("Katrina", "Atlantic", 2005, 174, 5, new DateTime(2005, 08, 23), new DateTime(2005, 08, 31)),
            new("Ian", "Atlantic", 2022, 155, 5, new DateTime(2022, 09, 23), new DateTime(2022, 10, 02))
        };

        foreach (var hurricane in hurricanes)
        {
            Console.WriteLine($"Name: {hurricane.Name}");
            Console.WriteLine($"Basin: {hurricane.Basin}");
            Console.WriteLine($"Year: {hurricane.Year}");
            Console.WriteLine($"Maximum Speed: {hurricane.MaximumSpeed}");
            Console.WriteLine($"Maximum Category: {hurricane.MaximumCategory}");
            Console.WriteLine($"Start Date: {hurricane.StartDate}");
            Console.WriteLine($"End Date: {hurricane.EndDate}\n");
        }

        Console.ReadKey();
    }
}

internal class Hurricane
{
    public string Name { get; }
    public string Basin { get; }
    public int Year { get; }
    public int MaximumSpeed { get; }
    public int MaximumCategory { get; }
    public DateTime StartDate { get; }
    public DateTime EndDate { get; }

    public Hurricane(string name, string basin, int year, int maximumSpeed, int maximumCategory, DateTime startDate, DateTime endDate)
    {
        Name = name;
        Basin = basin;
        Year = year;
        MaximumSpeed = maximumSpeed;
        MaximumCategory = maximumCategory;
        StartDate = startDate;
        EndDate = endDate;
    }
}
Ayymoss
Ayymoss1/26/2023
The setters aren't used in your example so the set constraint I think is unnecessary.
For validation of constructor params, you could probably do all that in the constructor. Though, it'll probably get messy, quickly. Not sure what typical convention here is though. For validation, I have a separate method as part of the class to validate its members.
Ayymoss
Ayymoss1/26/2023
this keyword used here is unnecessary also and personally I think is ugly 😄
multisportz.hq
multisportz.hq1/26/2023
Wait so for start date and end date how does it only process date
Ayymoss
Ayymoss1/26/2023
Not sure the question. I just did 1:1 to your original post
Ayymoss
Ayymoss1/26/2023
You're just passing the parameters through via constructor. Personally, I'd take this approach instead...

namespace HurricaneObject;

public static class Program
{
    private static void Main()
    {
        var hurricanes = new List<Hurricane>
        {
            new()
            {
                Name = "Katrina",
                Basin = "Atlantic",
                Year = 2005,
                MaximumSpeed = 174,
                MaximumCategory = 5,
                StartDate = new DateTime(2005, 08, 23),
                EndDate = new DateTime(2005, 08, 31)
            },
            new()
            {
                Name = "Ian",
                Basin = "Atlantic",
                Year = 2022,
                MaximumSpeed = 155,
                MaximumCategory = 5,
                StartDate = new DateTime(2022, 09, 23),
                EndDate = new DateTime(2022, 10, 02)
            }
        };

        foreach (var hurricane in hurricanes)
        {
            Console.WriteLine($"Name: {hurricane.Name}");
            Console.WriteLine($"Basin: {hurricane.Basin}");
            Console.WriteLine($"Year: {hurricane.Year}");
            Console.WriteLine($"Maximum Speed: {hurricane.MaximumSpeed}");
            Console.WriteLine($"Maximum Category: {hurricane.MaximumCategory}");
            Console.WriteLine($"Start Date: {hurricane.StartDate}");
            Console.WriteLine($"End Date: {hurricane.EndDate}\n");
        }

        Console.ReadKey();
    }
}

public class Hurricane
{
    public string Name { get; init; } = null!;
    public string Basin { get; init; } = null!;
    public int Year { get; init; }
    public int MaximumSpeed { get; init; }
    public int MaximumCategory { get; init; }
    public DateTime StartDate { get; init; }
    public DateTime EndDate { get; init; }
}
multisportz.hq
multisportz.hq1/26/2023
yeah i think the code is fine now like everyhting is wroking from original
multisportz.hq
multisportz.hq1/26/2023
and yeah i see that my code isnt the best formatted but because i just started c# i think its fine
multisportz.hq
multisportz.hq1/26/2023
its just the fact that when i use the DateTime object it also prints the time
Ayymoss
Ayymoss1/26/2023
Not saying your code is wrong. It worked. I'm just providing pointers 🙂
multisportz.hq
multisportz.hq1/26/2023
and i cant remove that
multisportz.hq
multisportz.hq1/26/2023
ah yeah thank yuo
multisportz.hq
multisportz.hq1/26/2023
will consider in future
Ayymoss
Ayymoss1/26/2023
You can use the to string function
multisportz.hq
multisportz.hq1/26/2023
yeah but unfortunatley it gives an erorr
multisportz.hq
multisportz.hq1/26/2023
ill pull it up rq
multisportz.hq
multisportz.hq1/26/2023
its like "cannot convert string to system.datetime"
Ayymoss
Ayymoss1/26/2023
Console.WriteLine($"End Date: {hurricane.EndDate.ToString("yyyy-MM-dd"}\n")
multisportz.hq
multisportz.hq1/26/2023
oh maybe ill try that
Ayymoss
Ayymoss1/26/2023
Console.WriteLine($"Start Date: {hurricane.StartDate:yyyy-MM-dd}");
Console.WriteLine($"End Date: {hurricane.EndDate:yyyy-MM-dd}\n");
Ayymoss
Ayymoss1/26/2023
This works using string interpolation
Ayymoss
Ayymoss1/26/2023
If you were doing this outside of string interp, you'd need to use the .ToString("FORMAT_HERE") method
multisportz.hq
multisportz.hq1/26/2023
getting a syntax error with this
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy