Wrenpo
Wrenpo
CC#
Created by Wrenpo on 11/5/2024 in #help
Safely getting a result from a async method within a sync method
I have an async method that returns a response object. In the calling sync method, what is the best way to get the result and capture any possible exceptions?
public static Task<ResponseObj> AsyncMethod()
{
// ... does stuff
return responseObj;
}

public void SyncMethod()
{
// ... does stuff;
// How to handle this safely?
var response = AsyncMethod();
}
public static Task<ResponseObj> AsyncMethod()
{
// ... does stuff
return responseObj;
}

public void SyncMethod()
{
// ... does stuff;
// How to handle this safely?
var response = AsyncMethod();
}
37 replies
CC#
Created by Wrenpo on 10/6/2024 in #help
Studying singly linked lists and need help understanding pointer functionality
I would love some help understanding something in the following leetcode problem Merge Two Sorted Lists:
void Main()
{
var list1 = new ListNode(1, new ListNode(2, new ListNode(4, null)));
var list2 = new ListNode(1, new ListNode(3, new ListNode(4, null)));

MergeTwoLists(list1, list2).Dump(); // expected output: [ 1, 1, 2, 3, 4 ]
}

public class ListNode
{
public int val;
public ListNode next;

public ListNode(int val = 0, ListNode next = null)
{
this.val = val;
this.next = next;
}
}

public ListNode MergeTwoLists(ListNode list1, ListNode list2)
{
if (list1 == null) return list2;
if (list2 == null) return list1;

ListNode dummy = new();
ListNode current = dummy;

while (list1 != null && list2 != null)
{
int value = 0;

if (list1.val <= list2.val)
{
value = list1.val;
list1 = list1.next;
}
else
{
value = list2.val;
list2 = list2.next;
}

ListNode temp = new(value);
current.next = temp; // This modifies dummy...
current = current.next; // This does not...
}

if (list1 == null) current.next = list2;
else if (list2 == null) current.next = list1;

return dummy.next;
}
void Main()
{
var list1 = new ListNode(1, new ListNode(2, new ListNode(4, null)));
var list2 = new ListNode(1, new ListNode(3, new ListNode(4, null)));

MergeTwoLists(list1, list2).Dump(); // expected output: [ 1, 1, 2, 3, 4 ]
}

public class ListNode
{
public int val;
public ListNode next;

public ListNode(int val = 0, ListNode next = null)
{
this.val = val;
this.next = next;
}
}

public ListNode MergeTwoLists(ListNode list1, ListNode list2)
{
if (list1 == null) return list2;
if (list2 == null) return list1;

ListNode dummy = new();
ListNode current = dummy;

while (list1 != null && list2 != null)
{
int value = 0;

if (list1.val <= list2.val)
{
value = list1.val;
list1 = list1.next;
}
else
{
value = list2.val;
list2 = list2.next;
}

ListNode temp = new(value);
current.next = temp; // This modifies dummy...
current = current.next; // This does not...
}

if (list1 == null) current.next = list2;
else if (list2 == null) current.next = list1;

return dummy.next;
}
When debugging the solution, I can see that during the while loop that setting the pointer current.next = temp modifies dummy.next to equal the temp ListNode. However, setting the pointer current = current.next does not modify dummy. Why is that?
18 replies
CC#
Created by Wrenpo on 8/28/2023 in #help
❔ .razor files intellisense not working - Visual Studio 2022
Declaritives, code blocks, etc are not picking up in razor files. Only the HTML is working with the intellisense. Additionally, I'm not sure if this is related or not, but using the "Add" option in a folder will not let me find or search for razor files. I'm running Visual Studio Community 2022 17.7.2.
2 replies
CC#
Created by Wrenpo on 6/3/2023 in #help
✅ MVC Core: Model Properties Not Passing to Controller Method
I have a model that I'm trying to have passed back to a controller method, but its properties are appearing as null. The correct POST method is being hit. I have listed out each property in the form to POST back via Html.HiddenFor(x => x.PropertyName) as I have done in many places within this project. They are still coming back null. Any ideas as to what may be going on?
6 replies