adding/taking info from types

BlitzMax Forums/BlitzMax Programming/adding/taking info from types

ckob(Posted 2007) [#1]
I'm kicking myself for this but I have been away from coding for awhile and just recently decided to try and write a very basic application for myself. I need have an app that adds a users name and email to a list box but I want to only show the name and when I click on the users name I can pull up info on the name and email address. I figured a type would work where I would just use:
Type User
Field Name
Field Email
End Type

Problem is how do I link both fields together so name/email is linked together and then once I do that how do I pull the info from each selected field.


H&K(Posted 2007) [#2]
Name and email are linked together. They are linked together as part of the type user.

AUser.name and AUser.Email

Method NameAndEmail:String()
return Self.Name+Self.Email
Endmethod

If you mean how do you make a union, I dont think Bmax has those.


FlameDuck(Posted 2007) [#3]
Just use a TMap (built-in) or another associative array (which you'll have to write yourself).


Grey Alien(Posted 2007) [#4]
Or have a third field which contains both. Make sure you have a method called ChangeName() which sets name and the combo field, and another method called ChangeEmail() which sets the email and the combo field.


H&K(Posted 2007) [#5]
@Grey,

Would you recomend that over just a method that returns both as if they were attached?


Brucey(Posted 2007) [#6]
As the Duck says, a TMap is probably the best way to go here...

For example:
' you have a list of Users...
Local usersList:User = new TList

' populated with name/emails
' ...
usersList.AddLast(User.Create("Albert", "mymail@..."))
' ...

Local names:TMap = new TMap

' add some name/user objects
For Local u:User = Eachin usersList
  names.Insert(u.name, u)
Next

' retrieve a User for a given name :

Local thisUser:User = User(names.ValueForKey("Albert"))
' will be Null if name isn't in the map


.. you could lookup the names map by the Text value of the currently selected combox/list item, for example.

HTH

:o)


ckob(Posted 2007) [#7]
nice thanks I will try this, again sorry for the noobness its been awhile.

After adding that code Brucey it errors on

Global usersList:User = New TList
(unable to convert from 'unknown' to user)

now im sure its because im not doing something with userlist? or am I from what I understand userlist:user is telling it userlist = Users right?


tonyg(Posted 2007) [#8]
Think it should be userslist:tlist=new tlist


ckob(Posted 2007) [#9]
see I thought that and changed it to
userslist:tlist=new tlist

the problem with that is now it spits an error out on User.Create saying that Create doesnt exist but isnt Create a Built in function


klepto2(Posted 2007) [#10]
no, createisn't a build in function the only build in methods are new compare and delete. You have to implement the user type by yourself.


ckob(Posted 2007) [#11]
wow I always thought create was a built in function like Addlast


klepto2(Posted 2007) [#12]
well, Addlast isn't a built in Function neither. These Functions (Addlast = method)are defined by Mark Sibly or whoever wrote the TList Module and so they are only belonging
to the TList type.
Maybe you should reread the Types section in the Bmax Help to clarify some of these things ;) .


ckob(Posted 2007) [#13]
well I think i did it but I have a problem some where because its either A) not storing the info or I am not pulling the info from it correctly whats wrong...


Type User
	Field Name
	Field Email
		
	Function Create(name$,email$)
	    Local names:TMap = New TMap
		For Local u:User = EachIn usersList
 			names.Insert(u.name, u)
		Next
	End Function
	
	Function Find:User(name:String)
	
	        For Local u:User = EachIn usersList
				
     		     If u.Name = name Then
			   Return u
		     EndIf
					
		Next
	Return
    End Function
		
End Type

Global usersList:TList = New TList

Case AddKeyButt	' user pressed button
	     AddGadgetItem(libListBox0,GadgetText(tfdTextField0))
			usersList.AddLast(User.Create(GadgetText(tfdTextField0), Email))
			

This is what im using to Display the results

			thisUser = User.Find(GadgetItemText(liblistbox0,SelectedGadgetItem(liblistbox0)))
			SetGadgetText(ClickStats,thisUser)




bradford6(Posted 2007) [#14]
Maybe this could help. Uses 2 Tmaps and a list defined in the User Type Scope (nicely encapsulated)

Basically creates 2 TMAPS.
1 to get name from a supplied email,
another to get an Email address from a supplied name.

Type User
	Global usersList:TList = New TList
	Global Name2Email:Tmap = CreateMap()
	Global Email2Name:Tmap = CreateMap()
	
	Field Name:String
	Field Email:String
		
	Function Create(name$,email$)
	    	Local newperson:User = New User
	
		newperson.Name = name
		newperson.email = email
		
		ListAddLast user.userslist , newperson
		MapInsert (user.Name2Email , newperson.Name , newperson ) ' < value is the USER
		MapInsert (user.Email2Name , newperson.Email , newperson )
		
		Print "added "+newperson.Name+" "+newperson.email
		
	End Function
	
	Function Getname:String(email:String)
		this_User:Object = MapValueForKey(User.Email2Name , email) 
		If this_User
			Return User(this_user).name
		Else
			Return "Could Not Find Name, Check your spelling and Caps"
		EndIf
	End Function
	
	Function GetEmail:String(name:String)
		this_User:Object = MapValueForKey(User.Name2Email , name) 
		If this_User
			Return User(this_User).Email
		Else
			Return "Could Not Find EMAIL, Check your spelling and Caps"
		EndIf

   	End Function
		
End Type

Function CreateUser(n$ , e$)
	User.Create(n$ , e$)
End Function
Function GetUserEmail:String(nm$)
	Return User.getEmail(nm$)
End Function

Function GetUserName:String(em$)
	Return User.getName(em$)
End Function


Print "~n~n~nAdding Users"
CreateUser(\"John\",\"John@...")
CreateUser(\"Jim\",\"Jim@...")
CreateUser(\"Sue\",\"Sue@...")



Repeat
Print ; Print 
			mailQuery$ = Input$("Enter a name to get email address=>")
			
			Print mailQuery$+" retrieved: "+GetUserEmail(mailQuery)
Print ; Print 
			nameQuery$ = Input$("Enter an email to get NAME=>")
			
			Print nameQuery$+" retrieved: "+GetUserName(nameQuery)

Until AppTerminate()




bradford6(Posted 2007) [#15]
...?