Get values from object through class


public class BlockType { public static BlockType Dirt_Block = new Block("Blah! values here"); } public struct Block { public static string Value; public Block(string value) { value = value; } }

Is there any way I can get the value from DirtBlock? BlockType.Dirt_Block.Value Dosent work, Im not sure if this is possible, if not any ways to get the same result? (Theres more values, but I shortened it for size) Btw, Im accessing BlockType.Dirt.value from anouther class. I only want to get one value out of it though


Other way to achieve it.

I think this approah provides an easier way to load/save tilemaps, and let defining block properties from text if needed.

Strict OOP with virtual methods and intensive inheritance is not a good idea for a game that may need optimizations to run fine.

Of course it can be improved or done in other way. ;)

public struct BlockProperty { public Texture2D Texture; public string Name; } public class BlockProperties { static readonly List<BlockProperty> Blocks; public static readonly BlockProperty Dirt; public static readonly BlockProperty Grass; public static readonly BlockProperty Wall; static BlockProperties( ) { ContentManager Content = Game1.Instance.Content; Blocks = new List<BlockProperty>( ) { (Dirt = new BlockProperty( ) { Name = "Dirt", Texture = Content.Load<Texture2D>( "textures/dirt" ) }), (Grass = new BlockProperty( ) { Name = "Grass", Texture = Content.Load<Texture2D>( "textures/grass" ) }), (Wall = new BlockProperty( ) { Name = "Wall", Texture = Content.Load<Texture2D>( "textures/wall" ) }), }; } public static BlockProperty ByName( string name ) { return Blocks.FirstOrDefault( b => b.Name == name ); } } public class Block { public BlockProperty BlockType; pulic Vector2 Position; public Block( BlockProperty block ) { this.BlockType = block; } public Block( string block ) { this.BlockType = BlockProperties.ByName(block); } } public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; public static Game1 Instance { get; private set; } Block Block; public Game1( ) { Instance = this; graphics = new GraphicsDeviceManager( this ); } protected override void LoadContent( ) { Block = new Block("Dirt") { Position = new Vector2(100,100) }; } }


That's because Dirt_Block is a BlockType, not a Block. In fact, I wouldn't expect this code to compile, as there is no way of converting a Block to a BlockType.

I'm not quite sure what you're after, but it sounds like you have a collection of block types that would suit an enumeration:

public enum BlockType { Dirt, Wood, Metal }

and you have a collection of Blocks that each have a block type:

public class Block { public BlockType Type { get; set; } }

Here, we are using a public auto property. You should avoid using public fields (no getters or setters).

Now, to create a new Block, you can use object initialisation:

var block = new Block { Type = BlockType.Metal };

Alternatively, you could create a Block constructor which takes a BlockType parameter.


<em>Assuming</em> you mean Dirt_Block to be of type Block - your Block Value field is static - that means it is associated with the Block type, not any particular instance. Make it a public field (or property) and your access will work:

public class BlockType { public static Block Dirt_Block = new Block("Blah! values here"); } public struct Block { public string Value; public Block(string value) { value = value; } }

Also if there is no particular reason you are going with a struct here, I would suggest making Block a class and expose Value as a public property.


Your doubt isn't very clear... but I think what you want is something like this... using OOP paradigm on the block classes.

public class BlockType { public virtual string Value; public static BlockType Dirt = new Block("Blah! values here"); } public class Block : BlockType { private string value; public Block(string value) { this.value = value; } public string Value { get { return value; } } }



  • Hidden classes and equivalence between {} object vs. custom constructor (v8)
  • ListView list is reversed when selection made
  • Get content from jquery CLEditor
  • Python script in a browser
  • Restrict classes that can implement interface in Java
  • Why use CDN(Content Delivery Network‎)? [duplicate]
  • How does a Service communicate with its Activity? How can a Service call a method in the Activity, w
  • At what point is code reordering in C++ optimization stopped?
  • applying background to first, and every third row in a dynamically generated table
  • Do stored procedures have the ability to delete a file from the OS?
  • white-space: pre-wrap not working in IE
  • toString() for each element of an array in Javascript [duplicate]
  • Is it expensive to create the Thread object or to actually start the thread?
  • Must EOT be at the bebinning in the line?
  • Python 3 Sorting a List of Tuples?
  • Different builds of turn-based Game Center game can’t see each other’s matches
  • Generate list with conditional items
  • How can I selectively modify the src attributes of script tags in an HTML document using Perl?
  • Webpack-dev-server and isomorphic react-node application
  • Reading from Windows registry in Perl [duplicate]
  • opengl window freezing during move/resize
  • File loader changed image file name but not the file name in HTML file
  • Redirect STDERR in OPEN pipe comand. Perl Linux
  • MongoError: Incorrect arguments
  • Read a local file using javascript
  • Read text file and split every line in MSBuild
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Java applet as stand-alone Windows application?
  • Incrementing object id automatically JS constructor (static method and variable)
  • Large data - storage and query
  • Do create extension work in single-user mode in postgres?
  • How can I estimate amount of memory left with calling System.gc()?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?