C# to BlitzMax conversion

BlitzMax Forums/BlitzMax Beginners Area/C# to BlitzMax conversion

As dayjob the intracacies of C#/ASP.NET is the way to pay my bills but am a n00b to BlitzMax.

After having looked at the excellent http://www.blitzbasic.com/Community/posts.php?topic=42519 [a href="http://www.blitzbasic.com/Community/posts.php?topic=42519]Beginners tutorial[/a] by wave i got a good idea that Blitzmax is capable at OOP possibilities. Though some things BlitzMax vs C# doesnt make sense to me yet.

Though, its unclear to me if;
1. BlitzMax have support for properties inside its Type?
2. Can have Constructors in Type?
3. Function overloading, such as clsEnemy.Create(posX) and clsEnemy.Create(posX) not possible?

A example of what i can do in C#, but no idea how in BlitzMax is the following example:
1. Create a class clsMain
2. create class clsEnemy.
3. Instantiate a enemy in clsMain.
4. Set a property such as <enemyobject>.PosX+=1;
5. then in a clsMain loop, waiting for keypress ESC, I do
<enemyobject>.Update();
5.1 Inside of <enemyobject>.Update() i read the property of PosX then do a Drawxxxxx position update.

I cant get to figure out how to do (4) then use (5.1) as it seems the property PosX is incasseble inside clsEnemy.Update().

See here below:
* a C# code example
* a BlitzMax implementation of the code, but Blitzmax dont work, or i dont know what i do wrong?

This is the code which i would make for c#, it is more or less the same as per the Wave' tutorial, but this is in C#, provided here for refrence clarfication.

I'd really appreciate it if someone can look at my code, and point me out the difference between C# and BlitzMax?


// -----------------------------------------------------------------
//'---------- Start C# code fragment.
using System;

public class clsMain
{
	//this is the constructor
	public clsMain()
	{
	}
	
	//this is the main loop;
	public void Main()
	{
		clsEnemy badguy1 = new clsEnemy();
		
		for(int intLoop; intLoop < 10; intLoop++)
		{
			badguy1.posX+=1;
			badguy1.Update();
		}
	}
}


// this is our enemy class
public class clsEnemy
{
	private int _posX;		//declare a global variable to store public asccesable posX value.
	
	public clsEnemy()
	{
		_posX = 0;
	}
	
	public void Update()
	{
		// please note, here i use the public property, something which BlitzMax seems not to be able to do??
		Console.WriteLine("I was updated to " + this.posX);
	}
	
	// a simple property.
	public int posX
	{
		get { return _posX; }
		set { _posX = value; }
	}
}




// -----------------------------------------------------------------
//'Now here follows how i try to implement the above code in BlitzMax.
'This application was written with BLIde <a href="http://www.blide.org" target="_blank">http://www.blide.org</a>
'Application:
'Author:
'Description:
'
Strict

Local appInstance1:clsMain = New clsMain;
appInstance1.Main();

Type clsMain
	Method Main()
		Local badguy1:clsEnemy = New clsEnemy;
		Graphics 800, 600, 0
		
		'// this is the main loop, in the c# example i used a for-loop.
		While not KeyHit(KEY_ESCAPE)
			Cls;
			badguy1.posX:+1;
			badguy1.Update();
			Flip;
		Wend
	End Method
End Type

Type clsEnemy
	Field posX:Int;		'// this would be the public accessbale property as in C#??
	
	Method Update()
		DrawText ("I was updated to " + Self.posX, Self.posX, 20);
	End Method
End Type
// -----------------------------------------------------------------


TIA!

for 1 and 2:
Type TMyType
	Field MyProperty:Int

	Function Create:TMyType(val:Int)
		Local tempMyType:TMyType = new TMyType
			tempMyType.MyProperty = val
		return tempMyType
	End Function
End Type

for 3, no unfortunately, overloading is not supported (not the end of the world mind you).

BTW, how do yoou get to format your code in the HTML block above??

http://blitzmax.com/faq/faq_entry.php?id=2

Edit:

Or...
http://www.blitzbasic.com/faq/faq_entry.php?id=2

lol@Diablo. Why didn't you just use the faq forum code? It's on the page you just linked too.
[faq id]
What are the forum codes?

1. BlitzMax have support for properties inside its Type?
2. Can have Constructors in Type?
3. Function overloading, such as clsEnemy.Create(posX) and clsEnemy.Create(posX) not possible?
No, no and no. You can't have (real) access modifiers either.

* a BlitzMax implementation of the code, but Blitzmax dont work, or i dont know what i do wrong?
Huh? Apart from the C style comments, it compiles and runs just fine (ie. as expected).

Thanks :)


A example of what i can do in C#, but no idea how in BlitzMax is the following example:
1. Create a class clsMain
2. create class clsEnemy.
3. Instantiate a enemy in clsMain.
4. Set a property such as <enemyobject>.PosX+=1;
5. then in a clsMain loop, waiting for keypress ESC, I do
<enemyobject>.Update();
5.1 Inside of <enemyobject>.Update() i read the property of PosX then do a Drawxxxxx position update.



Sounds like a simple task if you know how ;)


clsMain.MainLoop() ' <--- You must call this manually, I think main is always run automatically in C

Type clsMain

	Function MainLoop()
	
		Graphics 300,300,0 'DebugWindowMode
		
		Print "Game Started"
		
		' Instantiate Enemy
		Local Enemy:clsEnemy 
		Enemy = New clsEnemy 
		
		While Not KeyDown(Key_Escape)
			Enemy.Update()
			Flip;Cls ' Flips buffers (you draw to the backbuffer), Cls clears the Frontbuffer.
		Wend
		
		Print "Game Over"
		End					
			
	EndFunction
	
EndType

Type clsEnemy
	Field X,Y = 150 'Default Values, If not set then = 0
	
	Method Update()
		X:+1
		DrawOval X,Y,10,10
	EndMethod
EndType


Side note:
1. Variables can not contain < or > (I do not think that was what you meant, but just in case that is possible in C :)
2. You can use [a http://fullnameoflink...]Click Here[/ a] to make links on this forum.

TLists
Most time I would use the built-in Tlists for the above job, here is an example that extends the above:

clsMain.MainLoop() ' <--- You must call this manually, I think main is always run automatically in C

Type clsMain

	Global Enemies:TList = CreateList()
	 'This List is created when the program compiles because it is a global
	
	Function MainLoop()
	
		Graphics 300,300,0 'DebugWindowMode
		
		Print "Game Started"
		
		' Instantiate some Enemies
		For Local n = 0 To 200
			New clsEnemy 
		Next
														
		While Not KeyDown(Key_Escape)
			clsEnemy.UpdateAll()
			Flip;Cls ' Flips buffers (you draw to the backbuffer), Cls clears the Frontbuffer.
		Wend
		
		Print "Game Over"
		End					
			
	EndFunction
	
EndType

Type clsEnemy
	Field X = Rand(-300,0)
	Field Y = Rand(10,290)
	
	Method New()
		' Add this enemy to a list in clsMain
		clsMain.Enemies.Addlast( Self )
	EndMethod
	
	Function UpdateAll()
		For Local Enemy:clsEnemy = EachIn clsMain.Enemies
			Enemy.Update()
		Next
	EndFunction
	
	Method Update()
		X:+2
		If X > GraphicsWidth() Then X = 0
		DrawOval X,Y,10,10
	EndMethod
EndType


Huh? Apart from the C style comments


BTW yes the code does compile :)

I use the C#-code comments and general OOP-style coz i wanna be able to easily convert the code to c# at a later stage, for a project that i have in mind :)).

Ops sorry read your post to quick, Missunderstood your actual question.