March 05, 2021, 01:34:42 AM

Author Topic: Exiting Case/select statements  (Read 650 times)

Offline TomToad

Re: Exiting Case/select statements
« Reply #30 on: February 05, 2021, 04:23:07 PM »
To pass parameters to the functions, you just do it the normal way.  The only thing is that all functions in your array must accept and return the same parameters.
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Local f:String(n:Int)[10]
  4.  
  5. f[0] = Func0
  6. f[1] = Func1
  7. f[2] = Func2
  8.  
  9. Print f[0](10)
  10. Print f[1](20)
  11. Print f[2](30)
  12.  
  13. Function Func0:String(n:Int)
  14.         Return "You passed "+n+" into Func0"
  15. End Function
  16.  
  17. Function Func1:String(n:Int)
  18.         Return "You passed "+n+" into Func1"
  19. End Function
  20.  
  21. Function Func2:String(n:Int)
  22.         Return "You passed "+n+" into Func2"
  23. End Function
  24.  

For some odd reason, I cannot get this example to work with the NG version of BlitzMax.  Works fine with the original "vanilla" BlitzMax
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Baggey

Re: Exiting Case/select statements
« Reply #31 on: February 05, 2021, 05:30:32 PM »
To pass parameters to the functions, you just do it the normal way.  The only thing is that all functions in your array must accept and return the same parameters.
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Local f:String(n:Int)[10]
  4.  
  5. f[0] = Func0
  6. f[1] = Func1
  7. f[2] = Func2
  8.  
  9. Print f[0](10)
  10. Print f[1](20)
  11. Print f[2](30)
  12.  
  13. Function Func0:String(n:Int)
  14.         Return "You passed "+n+" into Func0"
  15. End Function
  16.  
  17. Function Func1:String(n:Int)
  18.         Return "You passed "+n+" into Func1"
  19. End Function
  20.  
  21. Function Func2:String(n:Int)
  22.         Return "You passed "+n+" into Func2"
  23. End Function
  24.  

For some odd reason, I cannot get this example to work with the NG version of BlitzMax.  Works fine with the original "vanilla" BlitzMax

Hi Tom,

Ive run your code and ive realised that to use The functions in the way im trying. They must be declared as a string!? Thankyou ;D
Im glad ive leart how to automate the writting of Blitz code, as it wont be to hard to alter now.

I do beleive that im indeed running "Vanilla Blitzmax version 1.50". Not getting on with "NG" it just throws to many errors  :-X

Thankyou all again.

Kind Regards Baggey
Currently Running a PC that just Aint fast enough!?
ZX Spectrum 48k, NEXT, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip.

Jesus was only famous because of his DAD.

Offline Baggey

Re: Exiting Case/select statements
« Reply #32 on: February 05, 2021, 05:43:31 PM »
Could also be written as
LongTextString :+ "_" + (RSet(i, 3).Replace(" ", "0"))
If i = 255 Then LongTextString :+ "]"

(just wrote it to make you aware of ":+" to skip "text = text + x")

All tips are greatly appriciated. I remember it's like the 'C' style " = and + " at the sametime. C=C+5 can be C:+5 Thanks!
Currently Running a PC that just Aint fast enough!?
ZX Spectrum 48k, NEXT, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip.

Jesus was only famous because of his DAD.

Offline TomToad

Re: Exiting Case/select statements
« Reply #33 on: February 05, 2021, 05:58:24 PM »
Hi Tom,

Ive run your code and ive realised that to use The functions in the way im trying. They must be declared as a string!? Thankyou ;D
Im glad ive leart how to automate the writting of Blitz code, as it wont be to hard to alter now.

I do beleive that im indeed running "Vanilla Blitzmax version 1.50". Not getting on with "NG" it just throws to many errors  :-X

Thankyou all again.

Kind Regards Baggey
No.  I am only using String and Int as an example on how to pass and return values.  You use whatever type your program needs instead.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Baggey

Re: Exiting Case/select statements
« Reply #34 on: February 05, 2021, 06:17:55 PM »
Hi Tom,

Ive run your code and ive realised that to use The functions in the way im trying. They must be declared as a string!? Thankyou ;D
Im glad ive leart how to automate the writting of Blitz code, as it wont be to hard to alter now.

I do beleive that im indeed running "Vanilla Blitzmax version 1.50". Not getting on with "NG" it just throws to many errors  :-X

Thankyou all again.

Kind Regards Baggey
No.  I am only using String and Int as an example on how to pass and return values.  You use whatever type your program needs instead.

Okay if you've got 10 mins?

The fiist Block of code dosen't work?   
     
Code: [Select]
    SuperStrict

    Global OO:Byte(n:Int)[]=[_000,_001,_002]
     
    Print OO[0](10)
    Print OO[1](20)
    Print OO[2](30)
     
    Function _000:Byte(n:Int)
            Return "You passed "+n+" into Func0"
    End Function
     
    Function _001:Byte(n:Int)
            Return "You passed "+n+" into Func1"
    End Function
     
    Function _002:Byte(n:Int)
            Return "You passed "+n+" into Func2"
    End Function

And the second does?

Code: [Select]
    SuperStrict
     
    Global OO:String(n:Int)[]=[_000,_001,_002]
     
    Print OO[0](10)
    Print OO[1](20)
    Print OO[2](30)
     
    Function _000:String(n:Int)
            Return "You passed "+n+" into Func0"
    End Function
     
    Function _001:String(n:Int)
            Return "You passed "+n+" into Func1"
    End Function
     
    Function _002:String(n:Int)
            Return "You passed "+n+" into Func2"
    End Function

Kind Regards Baggey
Currently Running a PC that just Aint fast enough!?
ZX Spectrum 48k, NEXT, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip.

Jesus was only famous because of his DAD.

Offline TomToad

Re: Exiting Case/select statements
« Reply #35 on: February 05, 2021, 07:14:03 PM »
You have changed the return type to Byte, but are still returning a String. 

It seems that you are not yet familiar with all of the basics in BlitzMax.  That makes it more difficult to explain some of the more advanced stuff.  I would recommend reading a BlitzMax tutorial, the docs included with BlitzMax are not that great. 

There is a fairly decent tutorial here.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Dabz

Re: Exiting Case/select statements
« Reply #36 on: February 05, 2021, 07:15:56 PM »
Quick glance your returning a string not a byte... But you can swing it like this:-

Code: [Select]
SuperStrict

    Global OO:Object(n:Int)[]=[_000,_001,_002]
     
    Print String(OO[0](10))
    Print String(OO[1](20))
    Print String(OO[2](30))
     
    Function _000:Object(n:Int)
            Return "You passed "+n+" into Func0"
    End Function
     
    Function _001:Object(n:Int)
            Return "You passed "+n+" into Func1"

    End Function
     
    Function _002:Object(n:Int)
            Return "You passed "+n+" into Func2"

    End Function
[/s]
Though, you cannot mix and match data types in Blitzmax it seems, I'm sure I've used a language in the past that let you do such a thing, so, my example is pretty much the same as your "string" version which works!

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit

Offline Dabz

Re: Exiting Case/select statements
« Reply #37 on: February 05, 2021, 07:37:12 PM »
I'm telling so many fibs... This is how I managed it (If your trying to move different data types), just have one "type" that contains fields representing each data type you need to return:-

Code: [Select]
SuperStrict

Type TType
Field x:Int
Field str:String
EndType

Global OO:TType(n:Int)[]=[_000,_001,_002]
     
Print TType((OO[0](10))).str
Print TType((OO[1](20))).x
Print TType(OO[2](30)).str
     
Function _000:TType(n:Int)
Local newtype:TType = New TType
newType.str = "You passed "+n+" into Func0"
    Return newType
End Function
     
Function _001:TType(n:Int)
    Local newtype:TType = New TType
newType.x = n
    Return newType
End Function
     
Function _002:TType(n:Int)
     Local newtype:TType = New TType
newType.str = "You passed "+n+" into Func2"
    Return newType
End Function

Been a while! :D lol

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit

Offline Baggey

Re: Exiting Case/select statements
« Reply #38 on: February 05, 2021, 07:54:34 PM »
Thanks again.

Owww. Of course the function was declared as a ':Byte' and inside the function it returned a string now if i'd off passed back a byte. It would of been a different kettle of fish  ^-^

Bugger! Programming used to be so easy  ::)

I darn't ask about objects! So is this the term thats used as reflection. Sort of like reflecting variables back and forth. LOL

I do sort of remember in a 'C' course i did many Moons ago. About passing in variables into functions and then passing them back.

Still Familiarity and Experiance are the Key! If if i say so my self.

And Dabz you lost me at TYPES  :o

Thanks for the link to the PDF it's better than the other one thats doing the rounds. The misses wants to go bed early tonight to her suprise  :P I said yeah! Ive got a new PDF to read!  :o

Thanks Guy's and Kind Regards Baggey
Currently Running a PC that just Aint fast enough!?
ZX Spectrum 48k, NEXT, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip.

Jesus was only famous because of his DAD.

Offline Dabz

Re: Exiting Case/select statements
« Reply #39 on: February 05, 2021, 08:21:25 PM »
Object is just the base class for all types in Blitzmax:-

Code: [Select]
anInt:Int = 1
aString:String = "Hello"

Print IsString(anInt)
Print IsString(aString)

Function IsString(obj:Object)
If String(obj) Return True
Return False
End Function

Handy thing when playing with types and casting stuff, it doesnt work with, how you say, natural (primitive) data types in Blitzmax like int or byte... I thought it did, some languages represent an int as an object of type "int", so my mistake there... Anyway, yes, that means "string" is a type, so even though you know nothing about them, you are indeed using them anyway, because a string is actually an object of type "string":-

Code: [Select]
Print "HELLO".tolower()

Which is why the above works, tolower() is a member of type "string".

I'll stop mentioning string now! :D lol

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit

Offline Baggey

Re: Exiting Case/select statements
« Reply #40 on: February 05, 2021, 08:44:08 PM »
So, I think Finally this is my new layout.

Code: BlitzMax
  1.  SuperStrict
  2.      
  3.     Global OO(n:Byte)[]=[_000,_001,_002]
  4.      
  5.     OO[0](10)
  6.     OO[1](20)
  7.     OO[2](30)
  8.      
  9.     Function _000(n:Byte)
  10.             Print "You passed "+n+" into 000 But not back out!"
  11.     End Function
  12.      
  13.     Function _001(n:Byte)
  14.             Print "You passed "+n+" into 001 But not back out!"
  15.     End Function
  16.      
  17.     Function _002(n:Byte)
  18.             Print "You passed "+n+" into 002 But not back out!"
  19.     End Function
  20.  

So now we can call the opcode and also pass a byte to it. I think thou some of the indexed functions require a displacement and a byte thou. Come to think off it also a 16 bit address as well! :)

Looking at this if it does turn out to be faster than a Case/Select. I think also looks like a neater and eaiser to read layout as well!

Note, Im going to want to call this .bmx file from another .bmx file so maybe Z80.OO[38](23) So this would be like calling up the 00 Opcodes, LD H,23 Not sure how this is going to interact with another Byte? Maybe Z80.OO[33](0)(64) or Z80.OO[33,0,64] This would load the LSB and then the MSB of the Display file 16384. ie the Equivalent to ... 00 Opcodes, LD HL,16384

Kind Regards Baggey

Currently Running a PC that just Aint fast enough!?
ZX Spectrum 48k, NEXT, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip.

Jesus was only famous because of his DAD.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal