C
C#9mo ago
Vedrit

✅ Questions about casting

Hi all, I'm looking to simplify some network code I've got. Networking doesn't really play too large a role in this particular case, but it does add a level of separation between scripts sending information and scripts receiving information. I have this one class that is used to serialize/deserialize the data for transmission. Currently, I have separate Dictionaries and counters for each data type it has to manage, so that when the data is read, I know exactly how much to cast as which type, which I do in order it was written. My question is, do I have to use separate Dictionaries for each type, or could I use one generic Dictionary? Is there any ambiguation in the typical data types? int, string, float, etc, that would be a problem if I try to cast the data on-the-fly?
8 Replies
WEIRD FLEX
WEIRD FLEX9mo ago
why would you need a dictionary? i guess we need some code how is the type recognized, is there a discriminator? also the counters things i don't understand
Vedrit
Vedrit9mo ago
I used dictionaries mainly for organization. The keys and values are read from the dictionary I don't know the details behind the deserialization, but to my understanding it's a byte array. You iterate through, telling it to cast the bytes as a specific type, then move to the next index. The counter is so that when I'm iterating through, I know exactly how many are of which type, and since they were written in groups, I can read them in groups. I guess, long story short, if I serialize an int, a string, and a float to a byte array, do I have to know the correct type when casting?
Angius
Angius9mo ago
Well, ideally you'd serialize an object with an int, a string, and a float property... and deserialize to that same type
Jimmacle
Jimmacle9mo ago
do you have code examples? normally your messages should include a header that provides necessary information like what type of message it is, how long it is, etc
WEIRD FLEX
WEIRD FLEX9mo ago
so wait instead of having a straight (isolated) deserializer, with a sequence of fields types and order, you "cycle" a dictionary to rebuild an object? because that would be probably the more difficult option i don't know if we are talking about nested types or what
Jimmacle
Jimmacle9mo ago
also, before rolling your own binary serializer/deserializer can you use something more standard like json, protobuf, etc?
Vedrit
Vedrit9mo ago
It's not mine, it's part of a networking library. That's why I said I didn't know the details. I'll get a code example in a bit, just woke up. Sorry, after putting my thoughts together, I've realized that this is a question better suited to the community for the network library I'm using. Thanks for trying, everyone.
Accord
Accord9mo 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
More Posts
❔ c# wpf change background color of whitespace characterI've been at it for a few hours and i just cant seem to get it to work, i've tried keypress events a❔ .NET installation not recognized, but command worksI am trying to install .NET Core 7. The installation goes smoothly and I can run the command, but li❔ Need a hand on creating something i am doing on fiddler which involve web traffic editingI've never done anything dealing with web traffic programmically so this is somewhat new to me but h❔ Why is Pop() necessary here??So I found a sample solution for the LeetCode decode string puzzle https://leetcode.com/problems/decI don't really get how to create new methods for my code. Yesterday people told me it's got no body.Didn't really understand anything when i did research. It's probably a quick fix : ] I'm talking ab❔ MC3050 compiler errorI added a new project with some custom WPF controls to my project's dependencies. I then added the ✅ Function For Division Game Showing Same Number Twice???```cs public void StartDivGame(string username, DateTime date, int totalQuestions, string difficultygoto vs recursionHello, I have a little question about "good habit" in this block of codes. So a little context is, t❔ What's the best way to test a URL that's it's valid?I have a situation where the user can enter a URL if they so choose, but if they do, I want to ensur❔ How to Package Multiple Versions of a Library Targeting Different Dependency Versions into a NugetI am working on a C# library (`mylib`) which has a dependency on another library (`thatlib`). Now,