var ClassReference:Class = getDefinitionByName(chanelClassName) as Class问题

今天在用as编程时,遇到个问题

var ClassReference:Class = getDefinitionByName(chanelClassName) as Class;

这段代码无论如何也无法编译。

google了一下,就在网上找到了答案,顿时明白了这个原理;



英文贴子上有如下内容:


I’ve been playing with Tweensy these days. Tried to understand how it works and how to create effects using particles generator. At some point I run into an error like the following one:

ReferenceError: Error #1065: Variable Box is not defined.
at global/flash.utils::getDefinitionByName()

What triggered it is this part of code:

var myClass:Class = getDefinitionByName("Box") as Class;

The error is saying that the it cannot retrieve the class code because the class is not compiled with the application. Even though you put something like

import mx.containers.Box;

the class is not compiled with the application because the linker and the compiler do not add classes that are not referenced in the code.

What is the solution… Pretty simple and straight forward:

private var _dummyVarToAddBoxToAppCompilation:Box;

Even though you’ll never use the _dummyVarToAddBoxToAppCompilation variable it is necessary to use that line to instruct the compiler to include mx.containers.Box in the compilation.

You’ll find the same answer on multiple blogs and forums but still needed to make an article on it. More is always better and easier to find.


原理很答案,你的代码中没有一处直接用到这个getDefinitionByName(chanelClassName) as Class类

所以编译器不会把这个类编译到二进制code中,就造成了所谓的运行期错误。

如果要解决也很答案,只要为一个var 指定这个类就可以了。


以下是一个使用 ScriptEnv 编写的 HFSS 脚本,用于输出每个对象的欧姆损耗。你可以将此脚本应用于任何 HFSS 工程中: ```vb Sub OutputOhmicLosses() Dim oProject As Project Dim oDesign As Design Dim oModule As Module Dim oEditor As Editor Dim oDefinition As Object Dim oSolution As Object Dim oModuleSolution As Object Dim oFields As Object Dim oReport As Object Dim oOutput As Object Dim oDataset As Object Dim oResult As Object Dim oValue As Object Dim sOutputFile As String Dim sObjectName As String Dim dOhmicLoss As Double ' 获取当前工程对象 Set oProject = ScriptEnv.Project ' 获取当前设计对象 Set oDesign = oProject.GetActiveDesign ' 获取模块对象 Set oModule = oDesign.GetModule("Solutions") ' 获取编辑器对象 Set oEditor = oDesign.SetActiveEditor("3D Modeler") ' 获取定义对象 Set oDefinition = oModule.GetDefinitionByName("LastAdaptive") ' 获取解算对象 Set oSolution = oModule.GetSolution ' 执行解算 oModuleSolution = oSolution.GetModule("Fields") oModuleSolution.Solve oDefinition ' 获取场对象 Set oFields = oSolution.GetFields ' 输出欧姆损耗 sOutputFile = "C:\Output\OhmicLosses.csv" Set oReport = oModule.CreateReport "Ohmic Losses", "Fields" oReport.AddTrace "Ohmic Loss", "Ohmic Loss", False, sObjectName, Array("Domain:=","Object"), Array("SolutionName:=", oSolution.GetName()) oReport.ExportToFile sOutputFile, "CSV" ' 打开输出文件并输出结果 Set oOutput = oModule.CreateOutput("Ohmic Losses Output", sOutputFile) Set oDataset = oOutput.GetDataSets.Item(1) For Each oResult In oDataset.GetResults sObjectName = oResult.GetAsString("Object") dOhmicLoss = oResult.GetAsDouble("Ohmic Loss") Debug.Print sObjectName, dOhmicLoss Next ' 关闭输出文件 oOutput.Close ' 提示输出完成 MsgBox "欧姆损耗输出完成。" End Sub ``` 该脚本首先获取当前工程对象和设计对象,然后通过解算求解场分布。接着,脚本创建一个报告对象,并将每个对象的欧姆损耗输出到 CSV 文件中。最后,脚本打开输出文件,输出每个对象的欧姆损耗,并提示输出完成。 请注意,脚本中的输出文件路径需要根据实际情况进行修改。此外,脚本中的输出结果是通过调试窗口(Debug Window)进行输出的,你可以根据需要修改输出方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值