C#
C#

help

Root Question Message

Petr
Petr11/30/2022
✅ Variables from different classes not adding up

Hello my name is Peter and i would like to ask for your help. In my code, i am unable to add variable from one class to variable from another class. I'll show you what i mean:

class Skret : Stvoreni                                 
    {
        private int poskozeniSS { get; set; }
        private int poskozeniU { get; set; }
       

        public Skret() 
        {
            sZdravi = 4 + sObrannaHodnota + sSila;
            sInteligence = 2;
            sSila = 2;
            sObratnost = 2;
            sStesti = 1;
            maManu = false;
            sUtocnaHodnota = 1 + Dyka.poskozeniZbrane;
            sObrannaHodnota = 1 + Latkova.hodnotaBrneni;
        }
}
------------------------------------------------------------------------------------
class Latkova : Zbroje
    {
        public Latkova()
        {
            hodnotaBrneni = 1;
        }
    }
------------------------------------------------------------------------------------
class Program
    {
        static void Main(string[] args)
        {
            Skret skret = new Skret();
            Console.WriteLine(skret.sObrannaHodnota);
        }
        
    }
---------------------------------------------------------------------------------------


So if i try to print the value of
sObrannaHodnota
, it should return 2, since it's 1+1. But unfortunately it returns only 1, as it seems to completely ignore the variable from the other class added to it.
Message Not Public

Sign In and Join Server To See

11/30/2022
phaseshift
phaseshift11/30/2022
$static
phaseshift
phaseshift11/30/2022
You should understand the difference between objects and classes, and how static plays in to this
phaseshift
phaseshift11/30/2022
If you want Skret to initialise based upon the values of a Latkova object, then you first need to create said object and pass it into the Skret constructor
Petr
Petr11/30/2022
I think i understand a little bit. Since hodnotaBrneni is part of Latkova, and is a static variable, i thought that i dont need to create an instance of Latkova object, since the variable belongs to the class itself, if you know what i mean. Is there any solution? Thanks for hearing me out.
Message Not Public

Sign In and Join Server To See

11/30/2022
Message Not Public

Sign In and Join Server To See

11/30/2022
Petr
Petr11/30/2022
Understood, basically it means that static variable cannot be used in this manner and it needs to be non - static and belong to a created object, right? ill try that, thank you very much!
Message Not Public

Sign In and Join Server To See

11/30/2022
Petr
Petr11/30/2022
Okay, it would be like a bad habit to do it through static variables. Ill change it to non-static and try it again
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy