C
C#

help

✅ Variables from different classes not adding up

PPetr11/30/2022
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.
Pphaseshift11/30/2022
$static
MMODiX11/30/2022
In C#, static allows you to have members (classes, methods, etc) that are not tied to any particular instance and are therefore always, globally, accessible. When applying static members, take the following considerations:
• If there are to be multiple instances of a class, do not use static
• If you need to track state, do not use static
• If you are going to have multi threaded workflows, do not use static unless you're aware of the caveats

static is best used for stateless methods, extension methods/classes and in areas where you understand the pros/cons. Read more here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static
Pphaseshift11/30/2022
You should understand the difference between objects and classes, and how static plays in to this
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
PPetr11/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.
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!
Okay, it would be like a bad habit to do it through static variables. Ill change it to non-static and try it again
AAccord12/2/2022
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.
Closed!

Looking for more? Join the community!

Recommended Posts
❔ Predefined type is not defined when using dotnet-scriptI'm getting an error for `System.Object` and `System.String` that they're not imported when creating❔ Drawing lines in WPFHow can I adjust the coordinates of my line and make it vertical in the center?❔ confused about Error controllerHi, i was following a tutorial about building APIs and the tutor used a new controller named it Erro❔ Ctor DI vs IServiceProvider.GetServiceHow often and when do you use IServiceProvider.GetService() for retrieving dependency over injecting❔ Address already in use after a kill processMy program keeps getting me this error ```Unhandled exception. System.Net.Sockets.SocketException (4❔ Issue With CodeI have this code ```cs public T Deserialize<T>(IRestResponse response) { ❔ Using Mediator with seperate API Contracts projectSup guys, I've finally decided to use Mediator, and now I have the following problem: 1. I have a AP❔ [Git] How to grab your pushed changes backMaybe it's not home here but I'm curious how I could grab my pushed changes back because I need to m✅ Csharp returns nothing while reading SQL data tableMy current code is here: https://pastebin.com/0kwPVGWm It was supposed to return the whole content ❔ claim type for Id(Primery key) of a Modelwhat claim type i can user to store Id(Primery key) of a Model❔ Any way to make custom border in visual studio resizeable?I'm looking to make my custom border in visual studio resizeable, i already have the outline around Is this the right directory for JwtHandler.cs?https://github.com/PacktPublishing/ASP.NET-Core-6-and-Angular/blob/main/Chapter_13/WorldCities/World✅ Can someone help me figure out how to write the quadratic formula? I keep getting an errorI think the error is because of some double, but all of my values are decimals.❔ Reading data from a text fileI have only just started trying to work with txt files and was wondering if anyone could help me and❔ Winform - How can I make something happen when clicking a word in a textbox?How do i make something happenen when a specific word or phrase like "hello world" is clicked in a t❔ How to pass JS object to function.How do I pass the JS object to the C# function in that it accepts it as an object of a defined ClassWhy does this code have a Required attribute?It uses the same `System.ComponentModel.DataAnnotations` as the Models in the folder above it, but t✅ GRPC Compiler error in DockerHey i have an grpc project and on my local machine it is building everything but when i try to build❔ Multiple Processes communicationhey all, I want to have a few processes communicate between them, my ideas so far is a socket serverCopying files from network path (c, .net)`File.Copy` cannot access the network share because it doesn't have the same access as the user runn