Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

- - - - -

typecasting in C#


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Miguel De La Cruz   Members   -  Reputation: 122

Like
0Likes
Like

Posted 18 August 2007 - 01:16 PM

I'm having a problem where I must typecast an array of a certain class to an array of structs. So: Class ClassName { }; void draw() { ClassName Uh; vertexbuffer.setdata((struct)Uh);//here its wrong } How would I turn Uh into a struct? Any help here would be greatly appreciated.

Ad:

#2 Washu   Senior Moderators   -  Reputation: 3113

Like
0Likes
Like

Posted 18 August 2007 - 01:28 PM

You don't. Classes and structures are different concepts in C#.

#3 Miguel De La Cruz   Members   -  Reputation: 122

Like
0Likes
Like

Posted 18 August 2007 - 01:34 PM

Dang. Heh, first week of learning C#. so how would I derive something that is put into the setdata() function?

#4 Alpha_ProgDes   Crossbones+   -  Reputation: 3297

Like
0Likes
Like

Posted 18 August 2007 - 01:45 PM

My question is why? Second question are you sure that you have convert these classes to struct? Third question what is compelling you to do so?

#5 Miguel De La Cruz   Members   -  Reputation: 122

Like
0Likes
Like

Posted 18 August 2007 - 02:23 PM

1) setdata only takes in a struct, it is used for vertices. I must have a class of it so I can derive from it.
2) yea, me sure. structs can't be derived in C#.
3) I must derive so I can control the vertices in different ways using a virtual update.

#6 Telastyn   Members   -  Reputation: 3328

Like
0Likes
Like

Posted 18 August 2007 - 02:33 PM

You don't. You make Uh contain an array of structs and then call

vertexbuffer.setdata(Uh.VertexData);


or something similar.

#7 blackbird04217   Members   -  Reputation: 144

Like
0Likes
Like

Posted 18 August 2007 - 02:40 PM

He is trying to use a vertex buffer... I know this because I am sitting next to him. He isn't listening to my advice and should be creating a different type then what he has... (He has a ParticleVertex which holds TONS of data including velocities...) And thats unneeded, so he needs to make a different vertex deffinition to use in the vertex buffer

#8 Miguel De La Cruz   Members   -  Reputation: 122

Like
0Likes
Like

Posted 18 August 2007 - 02:42 PM

I need that vertex buffer information, it is for the vertex shader moving the point sprites.

#9 Miguel De La Cruz   Members   -  Reputation: 122

Like
0Likes
Like

Posted 18 August 2007 - 02:43 PM

Telastyn, great idea! Why didn't I think of that? I blame this food to my left here. But thanks, I will try that. Eh nope, won't work... I must have the array of structs be derivable. You see the structs are verts like this:

struct array of
100 lava verts
200 exploding verts
100 smoke verts


and I must control the lava verts to flow downwards, but the rest to do their own thing. So basicly, they would all have an empty update except lava verts(derived) which would have a virtual update.

#10 Telastyn   Members   -  Reputation: 3328

Like
0Likes
Like

Posted 18 August 2007 - 03:55 PM

vertices are vertices. Their behavior might be controlled by something else, but they don't change type. And this might be better served in the .NET or other forums.

#11 Miguel De La Cruz   Members   -  Reputation: 122

Like
0Likes
Like

Posted 18 August 2007 - 04:42 PM

(scratches head)

#12 SamLowry   Members   -  Reputation: 1206

Like
0Likes
Like

Posted 20 August 2007 - 02:00 AM

Quote:
Original post by Miguel De La Cruz
(scratches head)

Compare it to numbers. A number is a number, nothing more. You can use a number to indicate a player's health, and when it reaches to 0 the player dies, but you aren't going to put that logic in number, but in player.

Keep the simple concepts simple. If you need to add logic to them, building a new concept (a new class) around it might be a better solution.

#13 Miguel De La Cruz   Members   -  Reputation: 122

Like
0Likes
Like

Posted 21 August 2007 - 02:20 AM

Nice Explanation Sam, however, how would I create an array of them?
I must have an array of structs and pass in the entire array, not one by one.

#14 Josh Petrie   Moderators   -  Reputation: 2286

Like
0Likes
Like

Posted 21 August 2007 - 02:48 AM

Stop thinking so low level. Vertices are vertices, they are "dumb" structures that just hold data and have no logic. The logic is in a higher-level object that holds some vertex data -- that object can be derived from, have behavior overridden via dynamic dispatch, et cetera. So you might have a

class BasicRenderObject
{
private Vertex[] verts;

public Vertex[] Vertices
{
get
{
return verts;
}
}

public BasicRenderObject(int vertexCount)
{
verts = new Vertex[vertexCount];
}

public virtual void Update()
{
// No-op in base class.
}
}

then you might have TerrainRenderObject and SuperRobotRenderObject and LavaMonsterRenderObject -- whatever. Update the vertices as you need to in the overridden Update() method, and use the Vertices property to access the raw verts for SetData() et al.

(I feel compelled to point out that I do not not like this design, but it addresses your issue.)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS