0

I have this situation where i'm trying to save "chat logs" while people switch around views in my flex mobile application..

so, my plan is i'm starting out with a main object that I plan to re-use as the main chat log object..I call it textObj

so, when someone new wants to chat my plan is to make a new object with the persons username

so how if i were to get the username from something like data.username how could I translate that into the var name of the object I want to make? So in the end i end up with

var uniqueUsername:Object = new Object();

so I can eventually do something like

uniqueUsername.chatLog="Sample text log";

EDIT: I guess I wasn't clear I dont literally mean uniqueUsername as the username....

say data.username was equal to AwesomeDudeYeah

and I wanted to make

var AwesomeDudeYeah:Object = new Object();

I cant just do

var data.username:Object = new Object();

so how could I make that work?

I want to make the username how I refer to the chat logs inside the textObj

Then how would I go about adding that to the textObj obj?

So i end up with textObj.uniqueUsername.chatLog="Sample text log";

which i could reference by doing textObj[uniqueUsername][chatLog]

(at least I think thats how it would be done)

Any help on these topics would be great!

2 Answers 2

2

bracket notation

var textObj:object = new Object();
textObj[uniqueUsername].chatLog = "Sample text log";


//accessed by
textObj[uniqueUsername].chatLog

if uniqueUsername does not exist in the textObj then you will get errors so make sure you handle them

    textObj[uniqueUsername] //basically if this is undefioned or null

//then when you do 
 textObj[uniqueUsername].chatLog // is trying to find chatLog on a null/undefined object

The error gods will smite you down

[EDIT] After reading your comments with Jax I think you should rework it a bit and make it more readable

var aUsers:Array = new Array( );

var oUser:Object = new Objbect();
    oUser.name = uniqueUsername;
    oUser.chatLog = "Sample text log";

aUsers.push( oUser );

Personally I would make a user class and hold user data in that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining how I can make the username an object var. And the EDIT of your posts seems like the solution I might go with for now, I think making an array and pushing the object in the array seems to be what i was actually thinking. And its the best bet for what I understand about programming. If i understand what you meant by a user class to hold data in I'd definitely try it, but i really have no idea what you mean
1

First, let me mention that using untyped object is not a good idea. You're essentially making 'data soup' which is hard to code with, to debug, will probably create null exceptions on runtime and will slow down your development efficiency. You should be using typed classes to create your objects (create a User class with a log and name property, etc).

However, the answer to your question:

var textObj:Object = {}; // if not instanciated already
textObj.uniqueUsername = {chatLog:'Sample Text Log'};

// Or you can do it all in one line
var textObj:Object = {uniqueUsername:{chatLog:'Sample Text Log'}};

You get the idea, however, this is not recommened.

2 Comments

EDIT: I guess I wasn't clear I dont literally mean uniqueUsername as the username.... say data.username was equal to AwesomeDudeYeah and I wanted to make var AwesomeDudeYeah:Object = new Object(); I cant just do var data.username:Object = new Object(); so how could I make that work? I want to make the username how I refer to the chat logs inside the textObj
well, it depends what you mean by 'equal to AwesomeDudeYear'. is that a string? another object? You'll probably just want to do an associative array with obj[enterStringNameHere] = {};, but again, this is a ludicrous way of doing it. Might as well create a User class that holds the name or have a Dictionary holds the instance of the User through a key (the username).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.