Matt
WPF Stack panel visibility within datagrid
I have a datagrid with a templatecolumn containing a stack panel of buttons.
My goal is to have only the selected row's buttons show up but despite numerous attempts, I can't figure out why my current xaml doesn't work.
I have checked with snoop and the visibility of the stack panel is NOT being changed. Changing it manually does cause the buttons to show up so something is wrong in my xaml for sure.
3 replies
❔ Array.IndexOf being weird
I'm very confused. Been trying to use Array.IndexOf to find a null terminator in a byte array
But it fails to find the byte and returns -1
Doing this however, it finds the byte... O_O
Why does Array.IndexOf not work in this situation?
12 replies
❔ MediaPlayer in WPF occasionally fails
I'm using this fire-forget pattern to play a sound through the media player. Sometimes the sound simply refuses to play. After the app has been open for long enough, all sound completely refuses to play no matter what. Any idea why?
2 replies
✅ Exception Handling for NRE without modifying every call.
I have a ReadData function which reads the memory of a process. If the process exits or crashes during the read, an exception is thrown and control is handed to the catch block. However, the calling function (the function that calls ReadData) can sometimes throw an NRE if the ReadData was aborted and returned null instead of a byte[]. How can I avoid the NREs without needing to change every single call of ReadData? Ideally I would abort the calling function as if it had never been called but I know that would require a large scale rewrite. I have toyed with the idea of modifying the ReadData signature to return a tuple of type (byte[], bool) and changing the behaviour of the calling function depending on the success of the ReadData (bool) but this would also require custom behaviour for every call. There are about 20 calls and I figure adding custom behaviour or exception handling for all of them is inefficient and bad practice. Any solutions?
37 replies
❔ WPF Observable Collection
Hallo, working on a window which displays a console pretty much. Made the console using an observable collection of strings and using a write method to add a string to the collection. For some reason, the collection refuses to display anything in the GUI. Any idea why this might be?
(Very beginnery when it comes to wpf as I am not the person doing the front end but we're both stumped)
5 replies
❔ What is the difference between these two pieces of code?
In essence I'm asking what the purpose of the lambda is here? I've used this pattern to help with a loose singleton pattern for a long time but I have no idea what this is even doing or why I use => over =
13 replies
❔ Check if list contains object with a specific property/variable value
I have a list of Players where Player is a simple object with a Name and ID. I need to check to see if a player with a specific ID is on the player list. I know that I could use a dictionary to do this but if there is a way to keep the list, that would be ideal. Here's a rough outline of what I'm trying to achieve
10 replies
❔ Help me fix my byte-bit conversion function
So I'll first explain my problem, then the solution I've implemented and how it appears to be failing.
I am trying to save an object to a byte array where each item is stored as a bit.
So I have a byte array of 38 bytes. This means I have roughly 300 bits. (actually 304 but we won't use half of a byte)
Now let's say I have the input that I want to save item number 3.
In this case, item number 3's bit will obviously be in the first byte and all I need to do is find the value to add to the byte which essentially does the same as flipping one of the bits from 0 to 1.
So if our bit representation of byte 1 is 00000000 and we want to save item number 3, our bit array should end up looking like 00000100
And this ends up meaning we have to add 4 to the byte.
If we need to save item number 10, it'll be in the second byte or byte index 1.
So there's a really neat way of making all of this work.
The quotient on dividing the index of the item to be saved by 8 gives us the byte we want to write to (actually we need to put the quotient into the floor function first)
Then the remainder can actually tell us the position of the bit to write to.
So if the remainder is 0, we add 128 to the value of the byte and if it isn't, we add Math.Pow(2, rem-1)
I am pretty sure my math is correct here, but for some reason, the amounts being written to the bytes and which bytes they're being written to is wrong.
11 replies
❔ Better way of handling this?
Is there a better way of handling this? The objects all inherit from the same base abstract class which contains the HandleUpdate method. The syncMessage.type is a string and I don't think I can change that (not 100% sure)
It feels like the strategy pattern might be what I'm after but this implementation already uses 3 classes per object and I'd rather not add more. Any ideas?
Edit: Cleaned code up to remove information irrelevant to the question
5 replies
❔ abstract class virtual methods vs default implementation interface
Okay so I’m a bit shaky on this but the way I understand it:
An abstract class tells the code that the implementation of the class is incomplete. Therefore, another class must inherit the abstract base class. By using virtual methods within the base class, these methods can be either overridden or implemented directly. These virtual methods are therefore allowed to have a method body. If an override is not defined, the derived class simply inherits the virtual method’s base class definition.
An interface provides a contract between the base and derived class that all methods must have an implementation. In C#8, an interface method is allowed a method body. If the method is not defined in the derived class, the default implementation is used.
So what really is the difference between the two? Are my definitions incorrect? and are fields easier to use in abstract classes than interfaces?
22 replies
Condense Byte[] to BitArray to smaller Byte[]
I have an array of 300 bytes, all either 0 or 1.
I need a function that takes in the byte array and performs a bitwise operation to convert the bytes to bits.
As an example:
If the first 8 bytes of the input array are
Then the BitArray should end up as
Which ends up as a byte 03
Obviously the array will have to be handled in chunks of 8. The resulting byte array should be about 40 bytes long, hence condensing the 300 byte array to a 40 bytes array.
I can't deviate from this structure unfortunately, I wish I could but this is what is required. Any advice on anything I should read up on or a fancy linq statement that will do this all for me would be greatly appreciated. Thank you
17 replies
Count updating before it's supposed to
This function is called in an infinite loop. When run, the code writes to the console with the previous and current collectible counts. For example, let's say I have 2 collectibles. The code writes:
"Current TE Count = 2"
"Previous TE Count = 2"
Every time the loop executes.
When I then collect a collectible, since there is literally nowhere else in the code assigning anything to the previous count other than what I've posted, I would expect to see:
"Current TE Count = 3"
"Previous TE Count = 2"
Then the if statement would be called, updating the previous count and only then should I see:
"Current TE Count = 3"
"Previous TE Count = 3"
In reality, I never see a console write where the current and previous counts are different.
Any idea why? I'm aware this is difficult to answer without any other code but believe me when I say I have checked to see if previous count is being updated somewhere else many times.
11 replies
Tricky networking issue
This is less of a directly c# related question and more of a networking issue but I guess this is still a good place to ask.
So I have two clients, each with a set of data.
Let's say for simplicity that 2 clients have a data set of { 0, 0 } each.
I want to alert client 2 if one of client 1's zeros flips to a one. So I have a function watching this byte array and when it changes, I send a message to a server with the updated byte array. This in turn sends a message to client 2 with the updated byte array and client 2 then applies the updated array. Everything is good so far. But then the issues begin.
Because client 2 is also watching their own byte array and they've just seen that their byte array has been updated. This means client 2 will now send a message to the server with their updated byte array which then sends the array to client 1 and so on. Is there a way around this or am I missing something?
2 replies
Union? of two binary arrays
Let's say we have two byte arrays:
I want to perform some operation on the arrays such that if the first byte in BOTH arrays is 0, then the resulting array's first byte is 0 but if EITHER byte is a 1, then the resulting array's first byte is a 1. (Extend this logic to each byte) Thus, the resulting array should be:
Is there any nice function that will do this for me?
45 replies
get the address from pointer offsets.
Trying to make a function to grab an address from pointer offsets. New to pointers and tried to follow a tutorial using Pymem but write it in c# which I'm far more comfortable with. Hasn't gone so well. The function returns 0 from a correct input. Would really appreciate the help if someone knows what I'm doing wrong
12 replies