Using New to create a new type

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

Using New to create a new type

Postby saraaku » Aug 10, 2007 1:34


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?

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

Postby vdecampo » Aug 10, 2007 1:37

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.

Posts: 975
Joined: Jun 22, 2005 22:33

Postby Deleter » Aug 10, 2007 1:46

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)
end constructor

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

Postby stylin » Aug 10, 2007 8:23

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".
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois

Postby cha0s » Aug 10, 2007 11:39

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

Postby anonymous1337 » Aug 10, 2007 12:03

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. -_-;;
Posts: 56
Joined: Jul 16, 2007 23:55

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

Postby saraaku » Aug 10, 2007 18:31

Sorry, AGain

Return to “Beginners”

Who is online

Users browsing this forum: Google [Bot] and 6 guests