What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

Easy C# doubt

escequi

New Member
Joined
Nov 25, 2011
Messages
143
Reaction score
0
So as you'll see i'm pretty new to C# (and any programming language in that matter) and i'm having a problem with:
Code:
public static Composite Identify() {
            int StashSlots = Loki.Game.Inventory.StashInventory.AvailableInventorySquares.get;
            if (LokiPoe.Me.IsInTown && StashSlots <= 15)
            {

            }
As it gives me this error:
Code:
an object reference is required for the nonstatic field method or property
 
Ok, i tried this:
Code:
Loki.Game.Inventory.StashInventory _m = new Loki.Game.Inventory.StashInventory;
            int StashSlots = _m.AvailableInventorySquares;
But then i get this error:
Code:
The type 'Loki.Game.Inventory.StashInventory' has no constructors defined
 
Try creating a new object for the Loki class. Then use that object's StashInventory property to get the stash inventory.

you have to understand the difference between the instantiation of a class versus the use of properties.

Using Constructors (C# Programming Guide)
Sorry if i'm just too dumb, but i've read this aritcle and some others and all of'em explains what Constructors are, i guess i get it, but idk what it does have to do with instantiation of this class, since i don't have acces to its code i can't tell its Constructors, also idk how to
Try creating a new object for the Loki class.
since Loki is just a namespace and StashInventory is a class not a property (if i'm not just too dumb to understand it) and its property is AvailableInventorySquares... I've tried like
Code:
object m;
m = new  Loki.Game.Inventory.StashInventory
but i'm pretty sure i did something wrong here (and as a non-native english speaker i find it hard to understand some thermology on the guides)
 
First things first:

An object is just an instantiation of a class. Think of a class such as Car, in the car you can have multiple methods, properties (velocity, number of seats, horsepower), etc. A constructor is basically what you put near the beginning of a class in order to initialize it and setup the class object properly. It is called when a new object of that class is created.

So lets say you want to declare a new object from the class Car, let's call it a Camry. You declare it as such: Car Camry = new Car();

Now in that same Car class, let's say there's a constructor. And this constructor is used to setup a set number of properties (number of seats, car length, etc), and attach methods to be used (such as a method called TradeInValue(), which calculates trade in value based on given parameters). Also, in the real world, sometimes certain methods have to be initialized in order, otherwise it does not work properly (take Windows Forms for example). So the constructor's job is to just do a bunch of things when an object of that class is declared/used.

I mistook StashInventory to be a property, my mistake. If you wanted to create a new object from the StashInventory class, try this: StashInventory myInventory = new Loki.Game.Inventory.StashInventory();

Also, you can declare common namespaces you'll be using at the top of the source code, that way you can eliminate Loki.Game.Inventory, since it's already declared above, and just go with : StashInventory myInventory = new StashInventory();

Also, you're far from being dumb. When I first started learning C#, I was far behind what you are doing now. Just keep going to StackOverflow and Microsoft's tutorials, they'll help you out big time. I have only been doing C# for a short while now, so I might not be explaining things properly, and hopefully people on here can correct me when I am wrong :P.
 
First things first:

An object is just an instantiation of a class. Think of a class such as Car, in the car you can have multiple methods, properties (velocity, number of seats, horsepower), etc. A constructor is basically what you put near the beginning of a class in order to initialize it and setup the class object properly. It is called when a new object of that class is created.

So lets say you want to declare a new object from the class Car, let's call it a Camry. You declare it as such: Car Camry = new Car();

Now in that same Car class, let's say there's a constructor. And this constructor is used to setup a set number of properties (number of seats, car length, etc), and attach methods to be used (such as a method called TradeInValue(), which calculates trade in value based on given parameters). Also, in the real world, sometimes certain methods have to be initialized in order, otherwise it does not work properly (take Windows Forms for example). So the constructor's job is to just do a bunch of things when an object of that class is declared/used.

I mistook StashInventory to be a property, my mistake. If you wanted to create a new object from the StashInventory class, try this: StashInventory myInventory = new Loki.Game.Inventory.StashInventory();

Also, you can declare common namespaces you'll be using at the top of the source code, that way you can eliminate Loki.Game.Inventory, since it's already declared above, and just go with : StashInventory myInventory = new StashInventory();

Also, you're far from being dumb. When I first started learning C#, I was far behind what you are doing now. Just keep going to StackOverflow and Microsoft's tutorials, they'll help you out big time. I have only been doing C# for a short while now, so I might not be explaining things properly, and hopefully people on here can correct me when I am wrong :P.
Yeah got it, thanks for taking your time to explain it to me XD anyways, i'm still getting that error
Code:
The type 'Loki.Game.Inventory.StashInventory' has no constructors defined
with
Code:
StashInventory myInventory = new Loki.Game.Inventory.StashInventory();
 
Back
Top