Cloudy
Cloudy
Explore posts from servers
CC#
Created by Cloudy on 7/5/2023 in #help
❔ Error for no reason?
I'm trying a leetcode question and I think I've solved it but whenever I run it, I get an error:
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
Yet I can see with my logs that the whole code runs through and works perfectly, nowhere for this error to occur other than when returning the value. But this error wasn't happening earlier in testing with the same return value
8 replies
CC#
Created by Cloudy on 7/5/2023 in #help
❔ Why does factorial stop working at 33?
public static void Main(string[] args)
{
Console.WriteLine(Factorial(33)); // 3400198294675128320
}

public static long Factorial(int i)
{
if (i <= 1)
return 1;
return i * Factorial(i - 1);
}
public static void Main(string[] args)
{
Console.WriteLine(Factorial(33)); // 3400198294675128320
}

public static long Factorial(int i)
{
if (i <= 1)
return 1;
return i * Factorial(i - 1);
}
The correct answer should be 8.6833x10^36
12 replies
CC#
Created by Cloudy on 4/23/2023 in #help
❔ Laptop Lid Close Event?
Anyone know how I can detect when the laptops lid is closed? This is my code so far but it doesn't work, any help is greatly appreciated
static void CreateLidCloseEvent()
{
// Create a new WMI event query to monitor for changes in the state of the lid
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_PowerManagementEvent WHERE EventType = 4");

// Create a new WMI event watcher to watch for the event
ManagementEventWatcher watcher = new ManagementEventWatcher(query);

// Attach an event handler to the watcher
watcher.EventArrived += new EventArrivedEventHandler(LidClosed);

watcher.Start();

Console.WriteLine("Waiting for lid closing event...");
Console.ReadLine();

watcher.Stop();
}

private static void LidClosed(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Lid closing event detected.");
}
static void CreateLidCloseEvent()
{
// Create a new WMI event query to monitor for changes in the state of the lid
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_PowerManagementEvent WHERE EventType = 4");

// Create a new WMI event watcher to watch for the event
ManagementEventWatcher watcher = new ManagementEventWatcher(query);

// Attach an event handler to the watcher
watcher.EventArrived += new EventArrivedEventHandler(LidClosed);

watcher.Start();

Console.WriteLine("Waiting for lid closing event...");
Console.ReadLine();

watcher.Stop();
}

private static void LidClosed(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Lid closing event detected.");
}
5 replies