Page 1 of 2

Public Dim

PostPosted: Sun Oct 31, 2010 10:06 am
by Henning
Hi,

Why "Public Dim myVar As Short"?
KB, as almost every other basic, are happy with "Public myVar As Short".

Why does this throw an error " = expected..."
Public Const RS232 As Byte = 1
KB and VB and... accept (require) it.

This doesn't work, Type is not recognized.
Code: Select all
Type T_Msg
  Type As Byte
  ID As Integer
  tSYSTEM As Byte
  unit As Byte
  port As Byte
  data(7) As Byte
  ReplyTo As Integer
  crc As Integer
End Type


To be continued.
/Henning

Re: Public Dim

PostPosted: Tue Nov 02, 2010 12:39 pm
by berndnoetscher
Why "Public Dim myVar As Short"?

type name expected in line 9 in file Global.QObject
...Short

Code: Select all
Public Dim myVar As Integer


would be right. There is one Integer type only yet (64 bit).

Dim is always needed, because it reflects for beginners the kind of declaration (won't change it for several reasons). I know, of course, that VB6 and KBasic allows to declare a variable just with "Public var As Integer", but this is just another odd of BASIC which has several disadvantages for me/the project (one rule lesser). Hope you will come over it.

The error is
Code: Select all
= expected in line 9 in file Global.QObject
...As Byte = 1


which means that "As Byte" is to omit, because the const automatically get the right type

Code: Select all
Public Const RS232 = 1


Check out the manual if you unsure about the syntax.

Re: Public Dim

PostPosted: Tue Nov 02, 2010 12:42 pm
by berndnoetscher
Why "Public Dim myVar As Short"?

type name expected in line 9 in file Global.QObject
...Short

Code: Select all
Public Dim myVar As Integer


would be right. There is one Integer type only yet (64 bit).

Dim is always needed, because it reflects for beginners the kind of declaration (won't change it for several reasons). I know, of course, that VB6 and KBasic allows to declare a variable just with "Public var As Integer", but this is just another odd of BASIC which has several disadvantages for me/the project (one rule lesser). Hope you will come over it.

The error is
Code: Select all
= expected in line 9 in file Global.QObject
...As Byte = 1


which means that "As Byte" is to omit, because the const automatically get the right type

Code: Select all
Public Const RS232 = 1


Check out the manual if you unsure about the syntax.

Type is not supported as in KBasic. The right way would be to create a new code file, which contains several variables, by now. I would like to assist you to get it in Q7Basic terms.
Anyway, I plan to do support for "Type" as known in KBasic for V1.

Re: Public Dim

PostPosted: Tue Nov 02, 2010 11:46 pm
by Henning
No problem, just felt a need to know ;)

So, a Const is kind of type-less until it is used in code. There it takes the correct type depending on where it is used?

Ex:

Dim var1 As Byte
Dim var2 As Integer
Dim var3 As Boolean

Const myconst = 1

will be ok for all of

var1 = myconst
var2 = myconst
var3 = myconst

As I understand it a Boolean if zero = False, non-zero = True. So I can continue to write If var2 Then.

/Henning

Re: Public Dim

PostPosted: Wed Nov 03, 2010 7:58 am
by berndnoetscher
Yes, you are right.
There is auto-conversion done when you assign expression (like a const). If the conversion fails, you get an exception (e.g. if the types are not compatible).

Code: Select all
Dim var1 As Byte


must be

Code: Select all
Dim var1 As Integer


Because there is no type Byte (8 Bit). All is integer with 64 bit, if you want to read and write files, there will be a 8,16,32,64 bit support of Integers. In these days, switching to 64 bit it is not so important to have smaller integers (you know integers are objects in memory as well).

<<As I understand it a Boolean if zero = False, non-zero = True. So I can continue to write If var2 Then.
Yes

Re: Public Dim

PostPosted: Wed Nov 03, 2010 10:37 am
by Henning
Because there is no type Byte (8 Bit). All is integer with 64 bit


That is not so good for me, 98% of the work in my app is dealing with byte-arrays. Reading/creating UDP packets.

Hmmmm, has to rethink this... maybe I can use strings for a workaround...

/Henning

Re: Public Dim

PostPosted: Wed Nov 03, 2010 2:47 pm
by berndnoetscher
Henning wrote:
Because there is no type Byte (8 Bit). All is integer with 64 bit


That is not so good for me, 98% of the work in my app is dealing with byte-arrays. Reading/creating UDP packets.

Hmmmm, has to rethink this... maybe I can use strings for a workaround...

/Henning


No, problem. For this case QByteArray/Type is the right one to use...

Re: Public Dim

PostPosted: Wed Nov 03, 2010 4:55 pm
by Henning
I guess I will have to wait/do something else, until User Defined Types are implemented.

The main reason for using UDT's is they can host different types of vars under a common name. In the next app to port from VB to Q7 UDT's hold byte/integer/string vars.
That app also has a challenge to create something like VB's User Controls. 8-)

Type T_Msg
Type As Byte
ID As Integer
tSYSTEM As Byte
unit As Byte
port As Byte
data(7) As Byte
ReplyTo As Integer
crc As Integer
End Type

/Henning

Re: Public Dim

PostPosted: Thu Nov 04, 2010 8:16 am
by berndnoetscher
Henning wrote:I guess I will have to wait/do something else, until User Defined Types are implemented.

The main reason for using UDT's is they can host different types of vars under a common name. In the next app to port from VB to Q7 UDT's hold byte/integer/string vars.
That app also has a challenge to create something like VB's User Controls. 8-)

Type T_Msg
Type As Byte
ID As Integer
tSYSTEM As Byte
unit As Byte
port As Byte
data(7) As Byte
ReplyTo As Integer
crc As Integer
End Type

/Henning


The solution could be to use the Array datatype, which can be nested with other arrays and all supported types OR to use classes and objects. Sounds difficult, but it is just another way to do it. And it already works. Arrays can be easily read and write to disk with ReadArray(...) and WriteArray(...). Check out the example "Array".

classes and objects solution could look like:

code file T_Msg.QObject

with variables

Public Dim Type As Integer
Public Dim ID As Integer
Public Dim tSYSTEM As Integer
Public Dim unit As Integer
Public Dim port As Integer
Public Dim data As Array
Public Dim ReplyTo As Integer
Public Dim crc As Integer

functions for reading/writing those variables....

Re: Public Dim

PostPosted: Thu Nov 04, 2010 8:25 pm
by Henning
Hi

I have partly solved it the "easy" way. By replacing the Typename.Member with Typename_member.

The original
Code: Select all
Public Dim TxMsg[17] As Byte
Type T_Msg
  Type As Byte
  ID As Short
  tSYSTEM As Byte
  unit As Byte
  port As Byte
  data(7) As Byte
  ReplyTo As Short
  crc As Short
End Type
'
Type U_SetupMsg
  Type As Byte
  UType As Byte
  UCh As Byte
  USys As Byte
  Uunit As Byte
  UPwr As Byte
  USpc As Byte
  USpc2 As Byte
  USpc3 As Byte
  ReplyTo As Short
  crc As Short
End Type

Type Shadow
  data(8) As Byte
End Type

Public TMsg As T_Msg
Public UTMsg As U_SetupMsg
Public TCmd As T_Msg
Public AckMsg As T_Msg
Public BS As T_Msg
Public InShadow[254] As Shadow
Public UtShadow[254] As Shadow

'Later in a sub next problem arises, TxMsg() is a Byte Array to send as UDP
  TxMsg[0] = TCmd.Type  ' 1
  TxMsg[1] = TCmd.ID \ 256 'Asc("C")
  TxMsg[2] = TCmd.ID And &HFF ' 0
  TxMsg[3] = TCmd.tSYSTEM
  TxMsg[4] = TCmd.unit
  TxMsg[5] = TCmd.port
  TxMsg[6] = TCmd.data[7]
  TxMsg[7] = TCmd.data[6]
  TxMsg[8] = TCmd.data[5]
  TxMsg[9] = TCmd.data[4]
  TxMsg[10] = TCmd.data[3]
  TxMsg[11] = TCmd.data[2]
  TxMsg[12] = TCmd.data[1]
  TxMsg[13] = TCmd.data[0]
  TxMsg[14] = TCmd.ReplyTo \ 256
  TxMsg[15] = TCmd.ReplyTo And &HFF
  CRCTx = 0
  For i = 0 To 15      'Calculate CRC
    CRCTx = CRCTx + TxMsg[i]
  Next
  TCmd.crc = CRCTx
  TxMsg[16] = TCmd.crc \ 256
  TxMsg[17] = TCmd.crc And &HFF
  'TxMsg(7) = crcTx And &HFF


'New' version, TxMsg() could be a String (if Mid$() is working?)
Code: Select all
Public Dim TMsg_Type As Integer
Public Dim TMsg_ID As Integer
Public Dim TMsg_tSYSTEM As Integer
Public Dim TMsg_unit As Integer
Public Dim TMsg_port As Integer
Public Dim TMsg_data[7] As Integer
Public Dim TMsg_ReplyTo As Integer
Public Dim TMsg_crc As Integer
'
Public Dim TCmd_Type As Integer
Public Dim TCmd_ID As Integer
Public Dim TCmd_tSYSTEM As Integer
Public Dim TCmd_unit As Integer
Public Dim TCmd_port As Integer
Public Dim TCmd_data[7] As Integer
Public Dim TCmd_ReplyTo As Integer
Public Dim TCmd_crc As Integer

Public Dim AckMsg_Type As Integer
Public Dim AckMsg_ID As Integer
Public Dim AckMsg_tSYSTEM As Integer
Public Dim AckMsg_unit As Integer
Public Dim AckMsg_port As Integer
Public Dim AckMsg_data[7] As Integer
Public Dim AckMsg_ReplyTo As Integer
Public Dim AckMsg_crc As Integer

Public Dim BS_Type As Integer
Public Dim BS_ID As Integer
Public Dim BS_tSYSTEM As Integer
Public Dim BS_unit As Integer
Public Dim BS_port As Integer
Public Dim BS_data[7] As Integer
Public Dim BS_ReplyTo As Integer
Public Dim BS_crc As Integer


Public Dim UTMsg_Type As Integer
Public Dim UTMsg_UType As Integer
Public Dim UTMsg_UCh As Integer
Public Dim UTMsg_USys As Integer
Public Dim UTMsg_Uunit As Integer
Public Dim UTMsg_UPwr As Integer
Public Dim UTMsg_USpc As Integer
Public Dim UTMsg_USpc2 As Integer
Public Dim UTMsg_USpc3 As Integer
Public Dim UTMsg_ReplyTo As Integer
Public Dim UTMsg_crc As Integer
'
'Type Shadow
Public Dim Shadow_data(8) As Integer
'End Type
'
'*Public TMsg As T_Msg
'*Public UTMsg As U_SetupMsg
'*Public TCmd As T_Msg
'*Public AckMsg As T_Msg
'*Public BS As T_Msg
Public Dim InShadow[254] As Shadow_data
Public Dim UtShadow[254] As Shadow_data

'In same sub as above
  TxMsg[0] = TCmd_Type  ' 1
  TxMsg[1] = TCmd_ID \ 256 'Asc("C")
  TxMsg[2] = TCmd_ID And &HFF ' 0
  TxMsg[3] = TCmd_tSYSTEM
  TxMsg[4] = TCmd_unit
  TxMsg[5] = TCmd_port
  TxMsg[6] = TCmd_data[7]
  TxMsg[7] = TCmd_data[6]
  TxMsg[8] = TCmd_data[5]
  TxMsg[9] = TCmd_data[4]
  TxMsg[10] = TCmd_data[3]
  TxMsg[11] = TCmd_data[2]
  TxMsg[12] = TCmd_data[1]
  TxMsg[13] = TCmd_data[0]
  TxMsg[14] = TCmd_ReplyTo \ 256
  TxMsg[15] = TCmd_ReplyTo And &HFF
  CRCTx = 0
  For i = 0 To 15      'Calculate CRC
    CRCTx = CRCTx + TxMsg[i]
  Next
  TCmd_crc = CRCTx
  TxMsg[16] = TCmd_crc \ 256
  TxMsg[17] = TCmd_crc And &HFF

'I will try Public Dim TxMsg As String * 18
Mid$(TxMsg,1,1) = Chr$(TCmd_Type And &HFF) ' Here comes the (self-documenting) Hex values again. Or what replaces Mid$.


/Henning