Using New to create a new type

New to FreeBASIC? Post your questions here.
Post Reply
saraaku
Posts: 56
Joined: Jul 16, 2007 23:55

Using New to create a new type

Post by saraaku »

Hello,

I am trying to create a new type. I want to use new to allocate 4 spaces to hold 4 new records but get an error when I try to do this. Can someone correct my errors?

Type Employee
Dim as string fname, lname
End Type

Dim p as Employee Ptr = New Employee[5]
p[0] = "Hillsdale Beattle"
print p->fname

The error I get is 'Invalid assignment/conversion"

How do I allocate enough space using the array format of new to store my two variables?

Also is there a way to use new to form a new constructor or to point to a constructor? Can you give me an example?


Thanks
Last edited by saraaku on Aug 10, 2007 19:07, edited 1 time in total.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

I would do this...

Type Employee
fname AS String
lname AS String
End Type

Dim p(5) as Employee
p(0).fname = "Akua Asamoah"
print p(0).fname

I didn't actually compile this but I think its correct.

PS: I don't think this is the correct forum to post questions.
:-)

-Vince
Deleter
Posts: 975
Joined: Jun 22, 2005 22:33

Post by Deleter »

No its not the right forum.

The following code works, but I am not sure where you were going with what you were doing, so you are going to have to be more specific if this is not what you wanted.

Code: Select all

Type Employee
    Dim as string fname, lname
    declare constructor(fname as string, lname as string)
End Type

constructor Employee(fname as string, lname as string)
    this.fname=fname
    this.lname=lname
end constructor

Dim p(5) as Employee Ptr
p(0)=new Employee("Akua","Asamoah")
print p(0)->fname 
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

saraaku, you are using the array form of NEW correctly. Like others have said, the problem is you're trying to assign a string value to an Employee object.

It is an invalid assignment/conversion because Employee has neither a constructor nor assignment operator overloaded to take a string as a parameter. It needs to have either one to allow p[0] = "string".
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Strike two.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

cha0s wrote:Strike two.
To clarify, saaraku, this was the second time you've posted in a forum where it didn't belong. Moreso, tips & tricks for questions. -_-;;
saraaku
Posts: 56
Joined: Jul 16, 2007 23:55

Sorry ChaOS, I did not know that I was in the wrong forum

Post by saraaku »

Sorry, AGain
Post Reply