C
C#2y ago
Petr

❔ Variables from different classes not adding up 2

Hello there once again. Basically, i'm trying to make variable from one class equal to a variable from other class based on users input. Same with couple of methods using simple delegates. There's no error, but after trying to print out chosen variable it always returns 0 (or null), which is not right. In case of methods, it returns null and throws an exception error. All code needed to solve this should be included in attachment photos, i hope i added everything necessary. Thanks for your time and help, Petr
14 Replies
hiyosilver
hiyosilver2y ago
Couple of things $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
hiyosilver
hiyosilver2y ago
Second, I would strongly urge you to get used to writing code in english if you ever intent to share it, because my god is it painful to try and read this when it might aswell be written in klingon as far as I'm concerned.
hiyosilver
hiyosilver2y ago
hiyosilver
hiyosilver2y ago
you are creating an instance of your class, but into the TryParse you pass a static property of the class that doesn't seem to exist anywhere according to your screenshot. Considering what you have posted, I can't see how this even compiles so you must have missed some bits. Or I'm blind, also a possibility. Google tells me that means "your character" This whole setup is really strange tbh. Having a separate class for "your character" with a static byte property that you set based on user input and then initialize the fields from other instances of specific races based on that bytes' value is just... a bit whack, no offense. If you're going to to take input in this way, you should just store it in a temporary byte variable, since you only use it to switch on once anyway, it seems. Don't make it a property of your character class. Concerning your error, you need to be a bit more specific about which line/lines throw exception/s and of what type. Rather than creating an instance of each race just to read the default values from, to begin with your character should probably be just a field of type "Creature" or whatever it might be, which you then assign an instance of a specific race to directly. In your case I guess
byte raceChoice;
while(!byte.Tryparse(Console.ReadLine(), out raceChoice)) {
//plus bounds checks
//"please enter valid number blah blah blah"
}
Rasa myCharacter = raceChoice switch {
1 => new SomeRace(),
2 => new SomeOtherRace(),
etc...
}
byte raceChoice;
while(!byte.Tryparse(Console.ReadLine(), out raceChoice)) {
//plus bounds checks
//"please enter valid number blah blah blah"
}
Rasa myCharacter = raceChoice switch {
1 => new SomeRace(),
2 => new SomeOtherRace(),
etc...
}
Very rough, but something to that effect. Or make it so race is just a field of a broader "Character" class, but the way you have it atm is just not very good.
Petr
PetrOP2y ago
Thanks for the advice, this is my very first c# project, so im just figuring things out. I'll try to follow your advice to make everything work properly. Thank you:)
hiyosilver
hiyosilver2y ago
@Petr I'm not saying those things to be unpleasant, but I really think it would be for the best to rethink and simplify your concept a bit , rather than just trying to fix the immediate issue. Good luck!
Petr
PetrOP2y ago
Actually, i have one more question. VasePostava (YourCharacter) should be combination of Rasa (Race/Specie) and Povolani (Class, as warrior, mage...) So in this snippet, i tried to make YourCharacters stats equal to the stats of chosen Race. I would like to then make that stat
+=
+=
with the Class (warrior etc.) stats. So YourCharacter would be combination of Class and Race. Does that still work with your solution, or do i need to adjust something else? Thanks.
hiyosilver
hiyosilver2y ago
That's a little different than what I thought. There are a ton of ways you could do this, I'd have to think about it for a second. Right now it seems like race and class aren't really meaningfully part of your character after setting the stats, is that right? As in, you only use the initial choice to set stats, but you wouldn't be able to query the character afterwards to check "hey, what race/class combo are you again?" If you have some kind of Player class, you would probably want it to have a field for both class and race then. In whatever way that is represented. They wouldn't necessarily need to be their own classes even. You could just have them be two enums Then have some sort of SetPlayerStats functions, pass in your player and have that set the stats based on the race/class. Or just do it in the Player constructor.
Petr
PetrOP2y ago
Thats exactly what i want. Sadly multiple inheritance isn't supported in c#, so i have to found another way. Yes that sounds very good actually, it would simplify the code a lot. I'm not ready to use enums yet (not sure how they work). But yes, player (YourCharacter) is supposed to be a combination of Race and Class, in terms of stats and functions. And even though im referencing the classes and creating objects of all races, Race's variables and functions aren't visible, and i know i've done it wrong.
hiyosilver
hiyosilver2y ago
Do you have a minute? I don't want to force my help on someone, if you'd prefer to do something else atm I could probably illustrate this easier like this: https://codeshare.io/eV1Xvx If you can see what I type
Petr
PetrOP2y ago
Yeah i do, of course, i'm on school break.
hiyosilver
hiyosilver2y ago
aight
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