An error message that I cannot handle

New to FreeBASIC? Post your questions here.
Post Reply
ivory348
Posts: 49
Joined: Dec 14, 2019 12:07
Location: Groningen, Netherlands

An error message that I cannot handle

Post by ivory348 »

The error line is indicated in the code snippet
The code was taken from the FreeBasic manual, as published on the Internet
Could someone please bail me out?

Code: Select all

Type departmentType
    id as integer
    managerId as integer
    floor as integer
end Type

Type emplyeeType
    fname as string*10
    lname as string*10
    empId as integer
    dept as integer
end Type

function initDept(deptId as integer) as departmentType
    
    dim tmpDpt as departmentType
    
    select case deptId
    case 24
        with tmpDpt
            .id = deptId
            .managerId = 1012
            .floor = 13
        end with
    end select
    return tmpDpt
end function

dim employee as emplyeeType

with employee
    .fname = "Suzan"
    .lname = "Jones"
    .empId = 1001
    .dept = initDept(24)''error 181 here invalid assignment/conversion
end with
     
end function   
sleep
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: An error message that I cannot handle

Post by Tourist Trap »

ivory348 wrote:The error line is indicated in the code snippet
The code was taken from the FreeBasic manual, as published on the Internet
Could someone please bail me out?
Hello if you do this

Code: Select all

'with ...
#print typeOf(.dept)
#print typeOf(initDept(24))
you read as a compiler output (not in the program running but in your IDE compiler messages window rather):
INTEGER
DEPARTMENTTYPE
And INTEGER doesn't know by default how to be converted in something like a DEPARTMENTTYPE. That's what is meant by the error message about a problem of conversion. Of course it can be fixed, but what is the purpose of this, I don't get it right now.

A remark anyway:

Code: Select all

'initDept(24) is the value at index=24 of an array of items with type=DEPARTMENTTYPE
employee.dept = initDept(24)
There is no such an array here in any case.

I think the thing that is meant here is somthing like that:

Code: Select all

employee.dept = type<departmentType>(24, 0, 0).id
Some temporary DEPARTMENTTYPE is created and constructed with id=24. Then this variable is evaluated relatively to its "id" field, and employee.dept takes this value. I don't really know however the primary goal of the author.
Last edited by Tourist Trap on Jan 04, 2020 15:49, edited 1 time in total.
ivory348
Posts: 49
Joined: Dec 14, 2019 12:07
Location: Groningen, Netherlands

Re: An error message that I cannot handle

Post by ivory348 »

" but what is the purpose of this, I don't get it right now." . . . . well, I am studying freeBasic using the manual on the Internet as my study material. In an attempt to understand the (somewhat extensive) code examples, I try them out. And if they bug out I am lost. This is the origin of my post.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: An error message that I cannot handle

Post by Tourist Trap »

ivory348 wrote:" but what is the purpose of this, I don't get it right now." . . . . well, I am studying freeBasic using the manual on the Internet as my study material. In an attempt to understand the (somewhat extensive) code examples, I try them out. And if they bug out I am lost. This is the origin of my post.
No problem, I just wanted to say that I don't understand what was the author of code goal for doing that. I added an explanation above about the fact it is an array that is written with the 24 as an index.
ivory348
Posts: 49
Joined: Dec 14, 2019 12:07
Location: Groningen, Netherlands

Re: An error message that I cannot handle

Post by ivory348 »

Re following response to my query:

If I do this
Hello if you do this

Code: Select all

     Code: Select all
     'with ...
     #print typeOf(.dept)
     #print typeOf(initDept(24))
     
And apply it like so:

Code: Select all

            ' .dept = initDept(24)''error 181 here invalid assignment/conversion  (WAS)
             .dept = typeOf(initDept(24)) (IS NOW)
     
I get more error messages.

Please advise. Thank you.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: An error message that I cannot handle

Post by fxm »

You introduced 2 errors compared to the original code:
(see the 2 lines put in comment in the code below)

Code: Select all

Type departmentType
    id as integer
    managerId as integer
    floor as integer
end Type

Type emplyeeType
    fname as string*10
    lname as string*10
    empId as integer
'    dept as integer  '' bad datatype
    dept as departmentType
end Type

function initDept(deptId as integer) as departmentType
   
    dim tmpDpt as departmentType
   
    select case deptId
    case 24
        with tmpDpt
            .id = deptId
            .managerId = 1012
            .floor = 13
        end with
    end select
    return tmpDpt
end function

dim employee as emplyeeType

with employee
    .fname = "Suzan"
    .lname = "Jones"
    .empId = 1001
    .dept = initDept(24)
end with
     
'end function   '' ???
sleep
ivory348
Posts: 49
Joined: Dec 14, 2019 12:07
Location: Groningen, Netherlands

Re: An error message that I cannot handle

Post by ivory348 »

"You introduced 2 errors compared to the original code:"

Sorry,
re #1, bad mistake, I missed that checking out the code.
re #2, I hope I hope that mistake will sort itself out as I finish the example code.

Many thanks for your response.
Patrick.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: An error message that I cannot handle

Post by MrSwiss »

Another implementation (a bit more complete, IMO) using different way of declaring type(s)
with alternative data-types, as well as a formated result output (which is missing in original):

Code: Select all

' (c) 2020-01-04, MrSwiss
' ----- types -----
Type departmentType
    As ULong    deptId, managerId, floor
    Declare Sub init(ByVal As ULong, ByVal As ULong, ByVal As ULong)
End Type

Sub departmentType.init( _
    ByVal dept_id   As ULong, _
    ByVal mgr_id    As ULong, _
    ByVal nfloor    As ULong  _
    )
    With This
        select case As Const dept_id
            Case 24
                .deptId = dept_id
                .managerId = mgr_id
                .floor = nfloor
            Case Else
                Print "department unknown!"
                Exit Sub
        end Select
    End With
End Sub

Type emplyeeType
    As String * 10  fname, lname
    As ULong        empId
    As departmentType   dept_t          ' embedded type
End Type
' ----- end of types -----

' ===== main =====
Const As UByte      t_pos = 18          ' tab position (preset)
Dim As emplyeeType  employee            ' one instance (just for example)

with employee                           ' initialize type
    .fname = "Suzan"
    .lname = "Jones"
    .empId = 1001
    .dept_t.init(24, 1012, 13)          ' only department 24 is implemented !!!
end With

'display result (nicely formated)
With employee                           ' get: primary type's info
    Print "employee contains:" : Print
    Print "given name:"; Tab(t_pos); RTrim(.fname)  ' remove trailing spaces
    Print "family name:"; Tab(t_pos); RTrim(.lname)
    Print "employee ID:"; Tab(t_pos); .empId
    With .dept_t                        ' get: embedded type's info
        Print "department ID:"; Tab(t_pos); .deptID
        Print "manager ID:"; Tab(t_pos); .managerId
        Print "floor:"; Tab(t_pos); .floor
    End With
End With

Sleep
' ===== end main =====  ' ----- EOF -----
Sometimes, using a different implementation, might help to better understand a concept ...
Post Reply