12 Replies - 740 Views - Last Post: 15 August 2012 - 05:21 AM

#1 rex64  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 168
  • Joined: 31-January 12

Constant Array

Posted 08 August 2012 - 07:08 PM

Is there a way to do something like this? I wanted it created at compile time to boost speed:
const typeEnum[] myArray1 = {typeEnum.ship};


Is This A Good Question/Topic? 0
  • +

Replies To: Constant Array

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Constant Array

Posted 09 August 2012 - 12:22 AM

Yes... you've just done it. But I don't really understand what you're trying to achieve... you've created an array with one object, meaning you could have just had an object of the enumeration type. Why do you want a constant array of enumerated types? How will this increase compile time speed?
Was This Post Helpful? 0
  • +
  • -

#3 rex64  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 168
  • Joined: 31-January 12

Re: Constant Array

Posted 09 August 2012 - 07:06 AM

I will add more to the list. I am getting an error:
Error 9 'myArray1' is of type '_3DgameTest.typeEnum[]'. A const field of a reference type other than string can only be initialized with null.
Was This Post Helpful? 0
  • +
  • -

#4 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Constant Array

Posted 09 August 2012 - 07:52 AM

Ah, of course. The question still stands though - why an array of 1 item? Why not just a const of that item? And why at all, what is the effect and benefit you're expecting?
Was This Post Helpful? 1
  • +
  • -

#5 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

Re: Constant Array

Posted 09 August 2012 - 09:43 AM

The only way to have a constant "Array" or any "Reference" type (which is any class/array object) you must use the following:

static readonly ArrayItem[] = new ArrayItem[]{Item1, Item2};


Was This Post Helpful? 0
  • +
  • -

#6 rex64  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 168
  • Joined: 31-January 12

Re: Constant Array

Posted 10 August 2012 - 01:42 PM

Thanks. This is called several times for every object in the game. This is for gravity checking.

Also, you mentioned Reference. Which was my next question. How do I declare this in my class variables? I want one class to have references to other classes:
public ref clsObject theShip = null;


The error I am getting:

Quote

Error 18 Invalid token 'ref' in class, struct, or interface member declaration

Was This Post Helpful? 0
  • +
  • -

#7 rex64  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 168
  • Joined: 31-January 12

Re: Constant Array

Posted 11 August 2012 - 10:18 AM

How can you make a pointer or reference to a float?
Was This Post Helpful? 0
  • +
  • -

#8 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

Re: Constant Array

Posted 11 August 2012 - 12:26 PM

ref is used in methods (that's the only use I know of)

So if you want something to be passed by reference regardless of whether it is a reference type or not do the following:

void modifyValueByReference(ref float valueToBeModified)
{
   valueToBeModified++;
}



To use the method:
float myFloat = 0f;

modifyValueByReference(ref myFloat);



But when a "Class" is involved it's almost always referenced, sometimes classes can be designed that they don't really follow the rules of reference, like "string" for example each time it's modified a new allocation of memory is created thus updating its reference point which means passing it around by reference is pointless where as when you're passing classes around they are being passed by reference and when you modify 1 value it modifies that block of data referenced by the pointer, unless you make your class setup new memory each time.



As for pointers you may want to read into ptr structures/classes and the com interop services maybe the marshal class aswell
Was This Post Helpful? 1
  • +
  • -

#9 rex64  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 168
  • Joined: 31-January 12

Re: Constant Array

Posted 11 August 2012 - 12:32 PM

Thanks.
Was This Post Helpful? 0
  • +
  • -

#10 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Constant Array

Posted 13 August 2012 - 08:32 AM

View Postracidon, on 11 August 2012 - 03:26 PM, said:

But when a "Class" is involved it's almost always referenced, sometimes classes can be designed that they don't really follow the rules of reference, like "string" for example each time it's modified a new allocation of memory is created thus updating its reference point which means passing it around by reference is pointless where as when you're passing classes around they are being passed by reference and when you modify 1 value it modifies that block of data referenced by the pointer, unless you make your class setup new memory each time.


A class is always a reference type. A struct on the other hand is a value type. Most of the base types in C#, like int, float and char, are actually structs, not classes. structs follow most of the same rules as classes but there are some differences. structs can't be inherited for example. string types are classes and their values are placed on the heap and not on the stack. The actual characters of the string are handled differently than most other classes. Check the article below for more reading on the topic of reference vs value types.

http://msdn.microsof...ibrary/ms173109
Was This Post Helpful? 0
  • +
  • -

#11 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

Re: Constant Array

Posted 13 August 2012 - 09:30 PM

View PostSixOfEleven, on 13 August 2012 - 08:32 AM, said:

View Postracidon, on 11 August 2012 - 03:26 PM, said:

But when a "Class" is involved it's almost always referenced, sometimes classes can be designed that they don't really follow the rules of reference, like "string" for example each time it's modified a new allocation of memory is created thus updating its reference point which means passing it around by reference is pointless where as when you're passing classes around they are being passed by reference and when you modify 1 value it modifies that block of data referenced by the pointer, unless you make your class setup new memory each time.


A class is always a reference type. A struct on the other hand is a value type. Most of the base types in C#, like int, float and char, are actually structs, not classes. structs follow most of the same rules as classes but there are some differences. structs can't be inherited for example. string types are classes and their values are placed on the heap and not on the stack. The actual characters of the string are handled differently than most other classes. Check the article below for more reading on the topic of reference vs value types.

http://msdn.microsof...ibrary/ms173109

I'm aware of this, but you can make a class act like string, so although it is reference if you passed the value of a class it would not update the original.
This happens with string because of how the operators are defined, in string they setup a completely new array assigning it a new memory address meaning the original one which is holding address X and now the new one which points to address Y.
This can be achieved with classes, so I was basically trying to say that you can make a class not follow the general rule of reference.
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Constant Array

Posted 14 August 2012 - 02:21 PM

A string is still a reference type, because it is a class. It is stored on the heap and not on the stack as value types are. Like you said, it is the way that the operators are coded. Sure, you can code classes that override the way operators behave but in the end they are still reference types because they exist on the heap and not the stack. If you don't want a class to act as a reference type I'd take a hard look at making it a struct instead.
Was This Post Helpful? 0
  • +
  • -

#13 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

Re: Constant Array

Posted 15 August 2012 - 05:21 AM

still wasn't disagreeing with anything you said.. was just making some things clear
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1