❔ 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
It depends on what you mean by "variable"
a decimal
also file
or int or string
Local variables - the ones you define as
var x = 0;
- are scoped to the method in which they're created"variable" and "file" are not terms the have any real relation to each other
How is this decimal variable you're interested in defined?
Where do you create it
they are only associated insomuch as you make them so
therefore, yes, you can define and use these things however you like
what do you mean by that? The namespace and class?
I mean is it a member variable of a class, a local variable in a method, a constant?
the variable
Paste the line where you create the variable
decimal ironO = 0.10m;
That's almost certainly a local variable in a method
so, by "file" I'm guessing you actually mean "file of source code"
Though I suppose it could also be a field
I think so
Are fields and local variables terms you're familiar with?
in which case, yes, but you're thinking about it wrong
vaguely
not sure about fields
Can you paste the lines surrounding the line where you create the variable?
they are the exact same but with the ironO and the value changed
Is this in a method?
Or a class or struct?
in a class
namespace Projects
{
public class Value : Price
{
and then all the things
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
This article might be helpful as well https://www.pluralsight.com/guides/understanding-scope-and-visibility-in-c
Understanding Scope and Visibility in C# | Pluralsight
Pluralsight Guides
would I need to do that for every single one?
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 constantso not like this: static public decimal ironO = 0.10m;
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 constantI'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
So what's your question at this moment
How do i get a var from the massive list to work in a separate program
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 aboutI tried Price.ironO but that wasnt enough
are the classes arranged correctly?
Because
Price
isn't the name of the class that variable is defined inso Value.ironO?
and how do you give the code the draker background?
yep
what
oh in discord?
$code
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/$codegif
or for inline, `code` =>
code
thanks
when I do ''Value.ironO'' it says 'Price' does not contain a definition for 'ironO'
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
I just saved and backed it up
so check for updates?
Try building it again now
now it says Value doesn't exist in the current context
and it is up to date
Oh is
Worth
defined in its own project as well as in the Unity
project?as in it's own folder or what?
like do namespace example : Projects?
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
where can I do that
I havent found a place to explain that yet
or explain it to where I understand
C# docs - get started, tutorials, reference.
Learn C# programming - for beginning developers, developers new to C#, and experienced C# / .NET developers
and just read that entire thing
The object oriented programming section will probably be helpful
And intro to classes
So would you recommend to stop working for a bit and just learning?
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
in over my head?
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 itI 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
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
)Inheritance in C#
Learn to use inheritance in C# libraries and applications.
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 senseadding .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
yeah sure thing
might not be me tomorrow but you're always welcome to ask questions
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.