Converting Object Names to Strings in AS2 - string

So i've made an object called "potato", then started a loop to check if something in "_level0" has the name of "_level0.potato". What i think is that since the the things in _level0 are objects instead of strings, the object name can't be recognized as a string, so im guessing i need to find a way to convert the object name to a string or vice versa.
var potato:MovieClip = this.createEmptyMovieClip("potato", this.getNextHighestDepth());
for(objects in _level0){
trace(_level0[objects])
if(_root[objects] == "_level0.potato"){
trace("OMG, i found a potato on level0")
}
}

Your suggestion that the objects are stored as a string is incorrect. If you try using typeof before your
trace(typeof _level0[objects])
you will see that its type is movieclip
and your "_level0.potato" is string
They will not be equal. But you can convert object reference to string using String(...) construct.
And about names. You're confusing names and references. MovieClip object like some others in ac2 have property called _name. In this property name of object is stored like string. But only name, not the full path to its destination.
For your potato mc _name will be equal "potato"
So you could do your search like this
var potato:MovieClip = this.createEmptyMovieClip("potato",this.getNextHighestDepth());
for(objects in _level0){
trace(_level0[objects])
if(_root[objects]._name == "potato"){
trace("OMG, i found a potato on level0")
}
}

Related

How to get type of the object name stored in a string variable?

I have an object name in a string variable. I need to find the type of the object. i could use LibraryExport function but i do not know which library has the object.
for example
String TheObj = "dw_grid_report"
How do i know either "dw_grid_report" is a window or a datawindow object?
FindClassDefinition (classname) is your friend.

Alternatives to boxing string field or extending ALL strings in Kotlin?

Given that one can't subclass builtin types in Kotlin, I'm looking for a way to add special method functionality to specific types of string fields in a record, without applying those extension functions to all strings (even within that record type). Say, for example, I have some mutable string fields inside a record, for which I want to define special purpose behaviors.
class Customer {
// ...
var zipcode: String
var email: String
// ...
}
For zipcode, assume I'd like to be able to call
thisCustomer.zipcode.plusFour to get the +4 of the zip code (99999-9999) via regex.
For email, assume I'd like to be able to call
thisCustomer.email.domain to have a regex go get the ...#company.com
portion of the specified email.
My objectives are to:
Avoid the runtime overhead of boxing zipcode into a class Zipcode with just a
single var inside, given that the size of the Customer recordset could potentailly be millions.
Avoid the syntax of having to assign
thisCustomer.zipcode.value = "99999-9999" or thisCustomer.zipcode = Zipcode("99999-9999"), if I did end up having to box the string.
Avoid adding general String.plusFour and
String.domain extension properties, even just within the Customer
class... because zipcodes and emails could technically call each others'
extensions
I've been thinking this over and considered the following options, but struck out with each:
subclassing String: Not possible since strings (and all built-ins)
are closed
applying an interface Zip to the var declaration (var zipcode:
String, Zip): No such syntax I could find
adding inner functions within the getter itself: Syntax doesn't seem to exist for this
using objects or functions-within-functions: Couldn't think of a way
for this to work, although I might not be imaginative enough
Am I missing an obvious solution here? Even if not obvious, with all the syntactic magic of Kotlin, is there a way to make it happen? Maybe there's a way to accomplish some/all of the objectives above without using any of these approaches?
You can use type aliases to make the intention clear:
typealias ZipCode = String
val ZipCode.plusFour get() = ...
typealias Email = String
val Email.domain get() = ...
class Customer {
// ...
var zipcode: ZipCode
var email: Email
// ...
}
Unfortunately, this only clarifies intentions and doesn't stop the compiler from allowing zipcode.domain. But for now I don't think there is any way which satisfies both objectives 1 and 3.
Kotlin developers have decided not to support assignment-incompatible type aliases, which would have fit all your requirements, in favor of waiting for value classes to be available on JVM, as discussed in https://github.com/Kotlin/KEEP/issues/4.
You may create a decorator class using delegate:
class YourString(val value: String) : Comparable<String> by value, CharSequence by value {
val plusFour: String
get() = "" //your logic here
val domain: String
get() = "" //your logic here
override fun toString() = value
}
Usage:
fun String.toYourString() = YourString(this)
class Customer(var zipCode: YourString, var email: YourString)
val customer = Customer("+4 99999-9999".toYourString(), "xxx#company.com".toYourString())
println(customer.zipCode.plusFour)
println(customer.email.domain)

Casting to types that are know only at Runtime by their string names. C#

I've got a problem here. (C#)
There's a collection in another assembly (I cannot change it) that takes a string as parameter and returns an object.
Like:
object Value = ThatCollection.GetValue("ParameterName");
The problem is, for each parameter string, it returns a DIFFERENT type as object.
What I want is to cast those objects to their respective types, knowing the types only at runtime by their string names.
I need to do some operations with those returned values.
And for that I need to cast them properly in order to access their members and so.
Limitations:
I cannot use "dynamic" since my code needs to be done in an older framework: 3.5 (because of interop issues).
I need to do operations with MANY returned values of different types (no common interfaces nor base classes, except "object", of course)
All I have is a table (containing string values) correlating the parameter names with their returned types.
Yes, I could transform that table into a biiig "switch" statement, not very nice, don't want that.
Any hints??
You want to look into reflection, something like the following should work to cast an object to type T. Set up a simple cast method:
public static T CastToType<T>(object o)
{
return (T)o;
}
Invoke this using reflection:
Type t = Type.GetType(stringName)
MethodInfo castTypeMethod = this.GetType().GetMethod("CastToType").MakeGenericMethod(t);
object castedObject = castTypeMethod .Invoke(null, new object[] { obj });

Visual basic string issues

Every time i try to attribute any type of string to this i get Object reference not set to an instance of an object. I have tried every combination of possible way to handle the string, convert it to a string again and all the fuzz. It's very frustrating and i guess it's some kind of base principle of the structure/class usage and the string array or whatnot (which is also very dumb)
Private Class movie
Public name As String
Public actors As String
Public year As Integer
Public country As String
Public votes As Integer
End Class
Private movies() As movie
If File.Exists(OpenFileDialog1.FileName) Then
lblPath.Text = OpenFileDialog1.FileName
Dim iFile As New StreamReader(lblPath.Text)
While Not iFile.EndOfStream
current = iFile.ReadLine
movies(i).name = "sasasasa"
lbMovies.Items.Add(movies(i).name)
i = i + 1
End While
End If
these are the code parts where i use it
You are creating an empty array of movie objects, as was pointed out previously. Consequently movies(i) is Nothing. When you try to access a member (movies(i).name) the appropriate exception is generated. Note that your code does not even reach the assignment operator = but fails prior to that. In other words, this has nothing to do with strings altogether; you will get the same error if you write movies(i).votes = 42 instead. To fix your code you will first have to create a movie object, populate it and append it to your array.

convert string data into Type for List<>

I'd like to take a string and create a typed List based on the
"Type" documented in the string. For example, suppose str is "System.string". I'd
like the method to create a List for me. Of course, the string could contain text
that "references" any object in the assembly. I've unsuccessfully tried the following:
Type classType = Type.GetType(str);
List<classType> wgList = new List<classType>();
I get a message stating that "classType is a field but is used like a type"..
How do I fix this up to get what I need ?
The following code provided a nice solution:
Type ty = Type.GetType(ItemType);
wgList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(ty));
You can not do that or i don't understand what is your purpose?
Use List< object > instead.
You may use Extension method is Cast< T>() in System.Linq namespace to convert your List< object > to List< T >.
List<Entry> entries = wgList.Cast<Entry>().ToList()

Resources