❔ 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
25 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
multisportz.hq
multisportz.hqOP2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
multisportz.hq
multisportz.hqOP2y ago
ohh
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
multisportz.hq
multisportz.hqOP2y ago
its this first ahh ok yeah i forgot abou thtat ill fix that, thanks yeah it works now @Th'm do you know how to remove the date/time? i mean just the time like i dont want it to include 12:00:00 am this is something i can search up actually if you dont know
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
multisportz.hq
multisportz.hqOP2y ago
@Th'm for some reason i keep getting an erorr of converting from string to system.datetime when i try to remove the time @jcotton42 it’s in this thread But the code I attached originally does not include the fact that I want to convert the date time to just daye But idk what to include to do that
jcotton42
jcotton422y ago
can you show that bit of code? and what line is the error on? also, not sure why you did the properties manually
Amos
Amos2y ago
What framework is this? The verbosity in properties is confusing me 😄
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;
}
}
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;
}
}
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. this keyword used here is unnecessary also and personally I think is ugly 😄
multisportz.hq
multisportz.hqOP2y ago
Wait so for start date and end date how does it only process date
Amos
Amos2y ago
Not sure the question. I just did 1:1 to your original post 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; }
}
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.hqOP2y ago
yeah i think the code is fine now like everyhting is wroking from original and yeah i see that my code isnt the best formatted but because i just started c# i think its fine its just the fact that when i use the DateTime object it also prints the time
Amos
Amos2y ago
Not saying your code is wrong. It worked. I'm just providing pointers 🙂
multisportz.hq
multisportz.hqOP2y ago
and i cant remove that ah yeah thank yuo will consider in future
Amos
Amos2y ago
You can use the to string function
multisportz.hq
multisportz.hqOP2y ago
yeah but unfortunatley it gives an erorr ill pull it up rq its like "cannot convert string to system.datetime"
Amos
Amos2y ago
Console.WriteLine($"End Date: {hurricane.EndDate.ToString("yyyy-MM-dd"}\n")
multisportz.hq
multisportz.hqOP2y ago
oh maybe ill try that
Amos
Amos2y ago
Console.WriteLine($"Start Date: {hurricane.StartDate:yyyy-MM-dd}");
Console.WriteLine($"End Date: {hurricane.EndDate:yyyy-MM-dd}\n");
Console.WriteLine($"Start Date: {hurricane.StartDate:yyyy-MM-dd}");
Console.WriteLine($"End Date: {hurricane.EndDate:yyyy-MM-dd}\n");
This works using string interpolation If you were doing this outside of string interp, you'd need to use the .ToString("FORMAT_HERE") method
multisportz.hq
multisportz.hqOP2y ago
getting a syntax error with this oh wait i forgot parenthesis i think
Amos
Amos2y ago
yep
multisportz.hq
multisportz.hqOP2y ago
ok thank you it works! ill keep the code u provided for reference so i can make my code look neater in futuire
Amos
Amos2y ago
👍
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server