C# to BlitzMax conversion

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

pappavis(Posted 2006) [#1]
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; }
	}
}






TIA!


Perturbatio(Posted 2006) [#2]
for 1 and 2:

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


pappavis(Posted 2006) [#3]
BTW, how do yoou get to format your code in the HTML block above??


Diablo(Posted 2006) [#4]
http://blitzmax.com/faq/faq_entry.php?id=2

Edit:

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


Oddball(Posted 2006) [#5]
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?


FlameDuck(Posted 2006) [#6]
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).


Tibit(Posted 2006) [#7]
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



pappavis(Posted 2006) [#8]
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 :)).


Tibit(Posted 2006) [#9]
Ops sorry read your post to quick, Missunderstood your actual question.


vmars316(Posted 2013) [#10]
Is that true ?
that BlitzMax can compile C# code .
I think Monkey can compile to C# code .
But I didn't know that BlitzMax can compile C# code .

Thanks...Vernon


Yasha(Posted 2013) [#11]
Is that true ?
that BlitzMax can compile C# code .


No. This thread was about converting by hand from C# to the equivalent BlitzMax code.

BlitzMax can handle C and C++ by farming the task out to GCC (it can't actually compile them itself). Interoperating with C# though is a really messy business in general, best avoid it and write either an entirely native or entirely CLR program.