C
C#2y ago
ERR404

❔ Is it possible to use variables from file a in file b?

I don't know how to do this and I have tried a variety of different words to search google but I have come back empty.
66 Replies
mg
mg2y ago
It depends on what you mean by "variable"
ERR404
ERR404OP2y ago
a decimal
JakenVeina
JakenVeina2y ago
also file
ERR404
ERR404OP2y ago
or int or string
mg
mg2y ago
Local variables - the ones you define as var x = 0; - are scoped to the method in which they're created
JakenVeina
JakenVeina2y ago
"variable" and "file" are not terms the have any real relation to each other
mg
mg2y ago
How is this decimal variable you're interested in defined? Where do you create it
JakenVeina
JakenVeina2y ago
they are only associated insomuch as you make them so therefore, yes, you can define and use these things however you like
ERR404
ERR404OP2y ago
what do you mean by that? The namespace and class?
mg
mg2y ago
I mean is it a member variable of a class, a local variable in a method, a constant?
JakenVeina
JakenVeina2y ago
the variable
mg
mg2y ago
Paste the line where you create the variable
ERR404
ERR404OP2y ago
decimal ironO = 0.10m;
mg
mg2y ago
That's almost certainly a local variable in a method
JakenVeina
JakenVeina2y ago
so, by "file" I'm guessing you actually mean "file of source code"
mg
mg2y ago
Though I suppose it could also be a field
ERR404
ERR404OP2y ago
I think so
mg
mg2y ago
Are fields and local variables terms you're familiar with?
JakenVeina
JakenVeina2y ago
in which case, yes, but you're thinking about it wrong
ERR404
ERR404OP2y ago
vaguely not sure about fields
mg
mg2y ago
Can you paste the lines surrounding the line where you create the variable?
ERR404
ERR404OP2y ago
they are the exact same but with the ironO and the value changed
mg
mg2y ago
Is this in a method? Or a class or struct?
ERR404
ERR404OP2y ago
in a class namespace Projects { public class Value : Price { and then all the things
mg
mg2y ago
Ok, so you will be able to access those variables, but they're instance variables of the Value class i.e. you'll need to do something like
var myValue = new Value();
Console.WriteLine(myValue.ironO);
var myValue = new Value();
Console.WriteLine(myValue.ironO);
ERR404
ERR404OP2y ago
would I need to do that for every single one?
mg
mg2y ago
Yes But it looks like what you might want is to just make them all static or constant In which case you can just use Value.ironO wherever you want carefree In that case it should be constant
ERR404
ERR404OP2y ago
so not like this: static public decimal ironO = 0.10m;
mg
mg2y ago
Unless for some reason you want to change it, in which case there are more design-oriented issues to work through That would make them static public const decimal ironO = 0.10m; would make them constant
ERR404
ERR404OP2y ago
ERR404
ERR404OP2y ago
I'm trying to use the things from the left project to work in the right project also all the error messages are from unfilled variables that I have yet to put a price to
mg
mg2y ago
So what's your question at this moment
ERR404
ERR404OP2y ago
How do i get a var from the massive list to work in a separate program
mg
mg2y ago
You have the right idea, but you're not accessing it properly There's something you need in addition to just ironO on the right hand project to make the compiler know what you're talking about
ERR404
ERR404OP2y ago
I tried Price.ironO but that wasnt enough are the classes arranged correctly?
mg
mg2y ago
Because Price isn't the name of the class that variable is defined in
ERR404
ERR404OP2y ago
so Value.ironO? and how do you give the code the draker background?
mg
mg2y ago
yep what oh in discord? $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/
mg
mg2y ago
$codegif
mg
mg2y ago
or for inline, `code` => code
ERR404
ERR404OP2y ago
thanks when I do ''Value.ironO'' it says 'Price' does not contain a definition for 'ironO'
mg
mg2y ago
Sounds like you're not compiling an up-to-date version of the file Make sure it's saved before you try to build/run
ERR404
ERR404OP2y ago
I just saved and backed it up so check for updates?
mg
mg2y ago
Try building it again now
ERR404
ERR404OP2y ago
now it says Value doesn't exist in the current context and it is up to date
mg
mg2y ago
Oh is Worth defined in its own project as well as in the Unity project?
ERR404
ERR404OP2y ago
as in it's own folder or what? like do namespace example : Projects?
mg
mg2y ago
You need to review some C# fundamentals Go over what a project is, what a class is, how to reference a project from another one, how scope and member variables work
ERR404
ERR404OP2y ago
where can I do that I havent found a place to explain that yet or explain it to where I understand
mg
mg2y ago
C# docs - get started, tutorials, reference.
Learn C# programming - for beginning developers, developers new to C#, and experienced C# / .NET developers
ERR404
ERR404OP2y ago
and just read that entire thing
mg
mg2y ago
The object oriented programming section will probably be helpful And intro to classes
ERR404
ERR404OP2y ago
So would you recommend to stop working for a bit and just learning?
mg
mg2y ago
I wouldn't necessarily stop. You learn by doing. It's just that at this point you need some more fundamental knowledge of the language before trying to do what you're doing
ERR404
ERR404OP2y ago
in over my head?
mg
mg2y ago
Not necessarily I also just have stuff to do so I'm trying to give you resources that aren't me 😛 But I can give a quick explanation of how things are laid out In C#, you have a project, and then that project contains namespaces, and those namespaces contain classes, and then those classes contain properties, fields, methods, and a bunch of other stuff If you want to use something you created in project A in project B, you have to add a reference to project A to project B Then you can using ProjectA.Some.Namespace and your code using the stuff you defined in project A will compile In your case things look a little fucked because it looks like you have both a file called Worth (add the .cs extension, by the way) in your Unity project, and an entirely different project called Worth I'm not sure why that's the case, but it probably shouldn't be But if you really want it to be that way, where the Worth class is defined in its own project, then to use it in your Unity project, you'll have to add a reference to the Worth project Then, in your Unity project, you'll add a using directive for the namespace that the Worth class is in, and then you'll be able to use it
ERR404
ERR404OP2y ago
I need to clean up my stuff. All the code from the worth folder is in the Worth file and i didn't delete it just in case
mg
mg2y ago
Ok, in that case you don't need to worry about different projects and references It looks like everything is in the same namespace, so Value.ironO should not cause any issues As for Price, I'm just now realizing that's what the class containing your Main method is called It should not be that way, and Value has no reason to inherit from it (Value : Price)
mg
mg2y ago
Here's a thing on inheritance, although it's a little abstract A classic example is if you have an Animal class and then you have a Dog, Cat, and Giraffe class Those three should all inherit from Animal, because they're animals It's an "is a" relationship In your case, you're saying "Value is a Price," which makes no sense
ERR404
ERR404OP2y ago
adding .cs to the Worth project fucked with the Unity project where it was fine before added 6 errors Can we pick this up tomorrow? It's 10pm for me and I gotta go to bed
mg
mg2y ago
yeah sure thing might not be me tomorrow but you're always welcome to ask questions
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