How to use Arrays In FreeBASIC

General FreeBASIC programming questions.
Post Reply
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

How to use Arrays In FreeBASIC

Post by Gablea »

hi Everyone,

I have been using arrays for a while in my Visual basic applications (my friend showed me how to use them)

this is an example of a array I use in VB.net

Say I wanted to add a cash payment to the Tender array i would call this

Code: Select all

AddToTenderList("CASH", "", (TenderValue / 100), "", "")
This is the code I use to load things to the array

Code: Select all

Public TenderPayment As New List(Of TenderItem)

Public Sub AddToTenderList(ByVal Line1 As String, ByVal Line2 As String, ByVal LineTotal As Decimal, ByVal CardSlipData_Print As String, ByVal CardSlipData_View As String)
        TenderPayment.Add(New TenderItem(TenderPayment.Count, Line1, Line2, LineTotal, CardSlipData_Print, CardSlipData_View))
        LoadItemsTender()
    End Sub

    Private Sub LoadItemsTender()
        Dim spaceNeeded As Integer
        FrmTenderScreen.ListTenders.Items.Clear()

        For Each i As TenderItem In TenderPayment
            item = vbNullString

            item = i.Line1_Tender
            spaceNeeded = Len(item)

            If i.Line2_Tender <> "" Then
                item += vbNewLine & i.Line2_Tender
                spaceNeeded = Len(i.Line2_Tender)
            End If

            spaceNeeded += Len(Format(Val(i.Price_Tender), "£######0.00"))
            item += Space((DisplayWidth - 2 - spaceNeeded)) & Format(Val(i.Price_Tender), "£######0.00")

            With FrmTenderScreen.ListTenders
                .DrawMode = DrawMode.OwnerDrawVariable
                .Items.Add(item)

                If .Items.Count > 0 Then
                    .SelectedIndex = .Items.Count - 1
                End If
            End With
        Next
    End Sub

This is the Class code that powers the array settings

Code: Select all

Public Class TenderItem

    Public ID_Tender As Integer
    Public Line1_Tender As String
    Public Line2_Tender As String
    Public Price_Tender As Decimal
    Public CardSlipData_Print As String
    Public CardSlipData_View As String

    Public Sub New(ByVal ItemID As Integer, ByVal Line1_Description As String, ByVal Line2_Description As String, ByVal ItemPrice As Decimal, ByVal LocalCardSlipData_Print As String, ByVal LocalCardSlipData_View As String)
        ID_Tender = ItemID
        Line1_Tender = Line1_Description
        Line2_Tender = Line2_Description
        Price_Tender = ItemPrice
        CardSlipData_Print = LocalCardSlipData_Print
        CardSlipData_View = LocalCardSlipData_View
    End Sub
End Class




Would someone be kind enough to show me how to use simple arrays in FreeBASIC
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: How to use Arrays In FreeBASIC

Post by vdecampo »

This isn't an array. It looks like you are using a Listbox control. They are 2 very different things.

-Vince
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: How to use Arrays In FreeBASIC

Post by Gablea »

In VB the listbox is used to display data from the array

The system store the sale information / tender information / multilsaver information in arrays to make it easier for me
To store the data to the network if needed.

I understand that there will be some work to use the same idea in freebasic but I'm sure in the long run it will be more
Beneficial Then the current way I'm doing it (text file with CSI data)

If anyone else has A better way to deal with the sale data I'm open to ideas and suggestions
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: How to use Arrays In FreeBASIC

Post by sancho2 »

It looks like a "list". Some languages have a built in list object which acts as a collection of items.
https://msdn.microsoft.com/en-us/librar ... .110).aspx
VB.Net Lists have builtin methods such as ADD, and properties such as COUNT.
In FB you will have to write those methods yourself.

In your code the variable 'item' is not declared/defined. I was under the impression that VB.Net requires all variables be declared. Is it a string?

Code: Select all

For Each i As TenderItem In TenderPayment
            item = vbNullString
Although I can't run it because of not having a list box in FB, I scratched up some code that replaces the List object with an array.
The code might look something like this:

Code: Select all

' This is the class defined in FB
Type TenderItem
	As Integer ID_Tender 
	As String Line1_Tender
	As String Line2_Tender 
	As Double Price_Tender 
 	As String CardSlipData_Print
 	As String CardSlipData_View
 	
 	Declare Sub NewItem (ByVal ItemID As Integer, ByVal Line1_Description As String, _
 	                                 ByVal Line2_Description As String, ByVal ItemPrice As Double,  _ 
 	                                 ByVal LocalCardSlipData_Print As String, ByVal LocalCardSlipData_View As String)
End Type

Sub TenderItem.NewItem(ByVal ItemID As Integer, ByVal Line1_Description As String,  _
                                      ByVal Line2_Description As String, ByVal ItemPrice As Double, _ 
                                      ByVal LocalCardSlipData_Print As String, ByVal LocalCardSlipData_View As String)
	'
	ID_Tender = ItemID
	Line1_Tender = Line1_Description
	Line2_Tender = Line2_Description
	Price_Tender = ItemPrice
	CardSlipData_Print = LocalCardSlipData_Print
	CardSlipData_View = LocalCardSlipData_View
End Sub

Declare Sub LoadItemsTender()
Declare Sub AddToTenderList(ByVal Line1 As String, ByVal Line2 As String, _ 
                                            ByVal LineTotal As Double, ByVal CardSlipData_Print As String,  _
                                            ByVal CardSlipData_View As String)


'Public TenderPayment As New List(Of TenderItem)
Dim Shared tenderPayment(Any) As TenderItem  

Sub AddToTenderList(ByVal Line1 As String, ByVal Line2 As String, _ 
                               ByVal LineTotal As Double, ByVal CardSlipData_Print As String, _ 
                               ByVal CardSlipData_View As String)
	Dim As Integer n = UBound(tenderPayment)
	
	n += 1
	If n < 1 Then n = 1
	
	ReDim Preserve tenderPayment(1 To n)
	tenderPayment(n).NewItem(n, Line1, Line2, LineTotal, CardSlipData_Print, CardSlipData_View)
	LoadItemsTender()
End Sub

Sub LoadItemsTender()
	Dim spaceNeeded As Integer
	Dim As String item
  	
  	' the following line clears the list box 
  	' FB doesn't handle windows controls natively and this functionality 
  	' is out of the scope of the question on arrays   
  	FrmTenderScreen.ListTenders.Items.Clear()		 
	
	Dim As Integer n = UBound(tenderPayment)
	
	For x As Integer = 1 To n
      Dim As TenderItem i
      'item = vbNullString
      item = ""
		i = tenderPayment(x)
      
      item = i.Line1_Tender
      spaceNeeded = Len(item)

      If i.Line2_Tender <> "" Then
          item += Chr(13) + i.Line2_Tender
          spaceNeeded = Len(i.Line2_Tender)
      End If

      ' FB does not have a FORMAT function natively. You have to include string.bi to use it or
      ' write your own 
      spaceNeeded += Len(Format(Val(i.Price_Tender), "£######0.00"))
            
      ' I have no idea what DisplayWidth is, as it is not declared in this sub
      item += Space((DisplayWidth - 2 - spaceNeeded)) & Format(Val(i.Price_Tender), "£######0.00")

      ' this with statement block is populating a list box and again this is out of our scope
      With FrmTenderScreen.ListTenders
 			.DrawMode = DrawMode.OwnerDrawVariable
 			.Items.Add(item)

 			If .Items.Count > 0 Then
				.SelectedIndex = .Items.Count - 1
 			End If
      End With
		
	Next


End Sub
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: How to use Arrays In FreeBASIC

Post by Gablea »

@sancho2
Thank you for that. The listbox is just used to display data from the array. (Does anyone have any example code of a listbox in freebasic (native code) as at the moment I am using locate and print and it is ok but not as good as it could be.

So now with the array could I save it to a text file and re load it back into the array if I want? I also assume I can easily delete items from the array.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: How to use Arrays In FreeBASIC

Post by BasicCoder2 »

FreeBasic doesn't have any native listbox control you have to roll your own or learn to use one of the GUI libraries.
Do you have an image of what this vb listbox looks like when displayed?
.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: How to use Arrays In FreeBASIC

Post by Gablea »

Ask and you shall recevie
Image

This is a example of the list box that I am using in the Visual basic application.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: How to use Arrays In FreeBASIC

Post by BasicCoder2 »

Well that is not how I imagined it. I can't clearly connect the output with the user defined type data.
I imagined it would look something like this:

Code: Select all

screenres 1000,500,32
color rgb(0,0,0),rgb(255,255,255):cls

type LIST_BOX
    as integer x
    as integer y
    as integer w
    as integer h
end type

dim shared as LIST_BOX lstBox
lstBox.x = 100
lstBox.y = 50
lstBox.w = 800
lstBox.h = 400


type TenderItem
    as integer ID_Tender
    as string  Line1_Tender
    as string  Line2_Tender
    as double  Price_Tender
    as string  CardSlipData_Print
    as string  CardSlipData_View
end type

dim shared as TenderItem tList(0 to 100)  'list of tenderItems
dim shared as integer tCount              'number of items on list
dim shared as integer selectedItem        'number of selected item

sub showList()
    screenlock
    cls
    line (lstBox.x,lstBox.y)-(lstBox.x+lstBox.w,lstBox.y+lstBox.h),rgb(0,0,0),b
    if tCount>0 then
        draw string (lstBox.x+4,lstBox.y+4), "ID_Tender   Line1      line2     price     CardSlipData_Print  CardSlipData_View"
        for i as integer = 0 to tCount-1
            draw string (lstBox.x+4*8,lstBox.y+i*16+32),str(tList(i).ID_Tender)
            draw string (lstBox.x+14*8,lstBox.y+i*16+32),tList(i).Line1_Tender
            draw string (lstBox.x+24*8,lstBox.y+i*16+32),tList(i).Line2_Tender
            draw string (lstBox.x+34*8,lstBox.y+i*16+32),str(tList(i).Price_Tender)
            draw string (lstBox.x+54*8,lstBox.y+i*16+32),tList(i).CardSlipData_Print
            draw string (lstBox.x+74*8,lstBox.y+i*16+32),tList(i).CardSlipData_View
        next i
    end if
    screenunlock
end sub

sub addItem(ID as integer,L1 as string, L2 as string,Price as double,CSDP as string,CSDV as string)
    tList(tCount).ID_Tender          = ID
    tList(tCount).Line1_Tender       = L1
    tList(tCount).Line2_Tender       = L2
    tList(tCount).Price_Tender       = Price
    tList(tCount).CardSlipData_Print = CSDP
    tList(tCount).CardSlipData_View  = CSDV
    tCount = tCount + 1
end sub

'some random data
for i as integer = 0 to 4
    addItem(int(rnd(1)*100),chr(int(rnd(1)*26)+65),chr(int(rnd(1)*26)+65),int(rnd(1)*1000),chr(int(rnd(1)*26)+65),chr(int(rnd(1)*26)+65))
next i

showList()
sleep

Essentially a list box is just a rectangle piece of the display screen within which the data is displayed.
As regards inserting, deleting, editing, loading and saving data it is just like any other list including scrolling up/down most of which is in code I first demonstrated a couple of years ago.
http://www.freebasic.net/forum/viewtopi ... lit=Gablea
Although those examples held the data in parallel arrays the display logic is the same.
.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: How to use Arrays In FreeBASIC

Post by Gablea »

@BasicCoder2
Thank you for that I shall look at the code when I get to my computer.

I'm just trying to make my application more efficient with handling data etc (still having some display issues with the time if a user is being slow with the keyboard but that is nothing to do with this)

On a side note what sort of cost would I be looking at for someone to completely re do my program using the lastest settling (like rewrite it) I'm sure my program can be a lot more stabler then what I have it at the moment (crashes every 1 in 30 loads)
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: How to use Arrays In FreeBASIC

Post by BasicCoder2 »

Gablea wrote:On a side note what sort of cost would I be looking at for someone to completely re do my program...
No idea what it would cost. A lot of money I suspect. Writing large scale stable professional code is way out of my league. Asking someone to re do your code is like an artist asking a professional artist how much it would cost for them to fix up their artwork. It would be easier I suspect for the expert to redo it from scratch. They can tutor you but they can't do it for you. Be careful paying an amateur programmer for they might just make it less stable, harder to modify and drop out of the task at any time. It will also take them a lot longer to write the code then it would if done by a professional. On two occasions professional C++ programmers looked at my FB code and within seconds fully comprehended it although they didn't use FB and then pull out all the logical errors within seconds. It is like playing a musical instrument, singing or drawing. You think you are good at it until you meet and see the work of someone who really is good at it.
.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: How to use Arrays In FreeBASIC

Post by sancho2 »

BasicCoder2 wrote: Essentially a list box is just a rectangle piece of the display screen within which the data is displayed.
As regards inserting, deleting, editing, loading and saving data it is just like any other list including scrolling up/down most of which is in code I first demonstrated a couple of years ago.
It might actually be a ListView control, which has a lot more options. It may also be simply a multi column listbox. Both of these controls are available to VB.Net.
The multi column listbox is a real pain in the ass but judging by the string formatting such in Gablea's code this might be what it is.
Then again it might be an ordinary listbox using string formatting instead of columns to create alignments.
The image he showed us has multiple controls on it and the list looks to be just the large bordered rectangle on the left with the items listed.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: How to use Arrays In FreeBASIC

Post by Gablea »

sancho2 wrote:
BasicCoder2 wrote: Essentially a list box is just a rectangle piece of the display screen within which the data is displayed.
As regards inserting, deleting, editing, loading and saving data it is just like any other list including scrolling up/down most of which is in code I first demonstrated a couple of years ago.
It might actually be a ListView control, which has a lot more options. It may also be simply a multi column listbox. Both of these controls are available to VB.Net.
The multi column listbox is a real pain in the ass but judging by the string formatting such in Gablea's code this might be what it is.
Then again it might be an ordinary listbox using string formatting instead of columns to create alignments.
The image he showed us has multiple controls on it and the list looks to be just the large bordered rectangle on the left with the items listed.

Hi sancho2
It is a
Standard list box in cish basic and I'm using The format function to aglin the text.

If I posted on here a job advert do you think much would replay to it?
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: How to use Arrays In FreeBASIC

Post by sancho2 »

I don't think there is any rules against posting a job advert.
I'm not sure how successful its going to be however.
Post Reply