Page 1 of 1

const string arrays

Posted: 09 Sep 2007 18:42
by shish
i'm trying to get a constant string array to work and i'm not having much luck, i'm using it for menu items to be shown on an lcd

i have 9 items of 16 chars each declared, if i omit the '[16]' it doesn't compile.

Code: Select all

     const MSG_MainPRO as String[9][16] = ("Refresh Rate","Datalog Rate","Log Mode","Save CSV File","Save BIN File","Averaging","Backlight","Datalog File","Exit")
within my code: ('Program_Value' is a byte value of the menu item i want to display)

Code: Select all

lcd_out(2,1,MSG_MainPRO[Program_Value])
it compiles but i get nothing on the display, where am i going wrong?

Re: const string arrays

Posted: 09 Sep 2007 19:36
by yo2lio
Hi,
shish wrote:

Code: Select all

     const MSG_MainPRO as String[9][16] = ("Refresh Rate","Datalog Rate","Log Mode","Save CSV File","Save BIN File","Averaging","Backlight","Datalog File","Exit")
This combinations not work in this version of MikroBasic & MikroPascal.

Work around :

Code: Select all

program case_msg

const MSG_MainPRO1 as String[16] = "Refresh Rate"
const MSG_MainPRO2 as String[16] = "Datalog Rate"
const MSG_MainPRO3 as String[16] = "Log Mode"
const MSG_MainPRO4 as String[16] = "Save CSV File"
const MSG_MainPRO5 as String[16] = "Save BIN File"
const MSG_MainPRO6 as String[16] = "Averaging"
const MSG_MainPRO7 as String[16] = "Backlight"
const MSG_MainPRO8 as String[16] = "Datalog File"
const MSG_MainPRO9 as String[16] = "Exit"

dim Program_Value as byte
    text_20 as string[20]
    
Sub Procedure Get_Msg(dim location as byte, dim byref msg as string[20])
  select case location
    case 0 msg = MSG_MainPRO1
    case 1 msg = MSG_MainPRO2
    case 2 msg = MSG_MainPRO3
    case 3 msg = MSG_MainPRO4
    case 4 msg = MSG_MainPRO5
    case 5 msg = MSG_MainPRO6
    case 6 msg = MSG_MainPRO7
    case 7 msg = MSG_MainPRO8
    case 8 msg = MSG_MainPRO9
  end select
End Sub
    
main:
  Lcd_Init(PORTD)                  ' initialize  (4-bit interface connection)
  Lcd_Cmd( LCD_CURSOR_OFF)         ' send command to LCD (cursor off)
  Lcd_Cmd(LCD_CLEAR)               ' send command  to LCD (clear LCD)
  Program_Value = 0
  while Program_Value < 10
    Get_Msg(Program_Value,text_20)
    Lcd_Out(2,1,text_20)
    inc(Program_Value)
    delay_ms(2000)
  wend
End.

Posted: 09 Sep 2007 19:40
by shish
thanks yo2lio

i was doing this to 'clean up' some previous code i'd written to do it, i thought it would make it much simpler... i'll just go back to what i had previously!

thanks for taking the time to write out code though!

Posted: 09 Sep 2007 22:01
by Kalain
shish wrote:thanks yo2lio

i was doing this to 'clean up' some previous code i'd written to do it, i thought it would make it much simpler... i'll just go back to what i had previously!

thanks for taking the time to write out code though!
I was doing the same. (cleaning some code and got some problems)
Have a look here it will help.
http://www.mikroe.com/forum/viewtopic.php?t=11480

Posted: 09 Sep 2007 22:21
by MAN
Hi shish;

If you are using P16 family see this example http://www.mikroe.com/forum/viewtopic.php?t=11310 and get the Lib_MDStrArray ftp://www.ttelecom.com.br/pub/mcl_lib/ that can drive this strings constants. But if you are using P18 I suggest that you use RAM array.
at the moment, mB not drives correct multi dimensional const strings with dynamic index but this function lets you control this. Maybe this lib helps you.

Posted: 09 Sep 2007 22:23
by shish
thanks Kalain, it was your post that prompted me to start improving my code