20170602

今日主要学习数组笔记如下:

数组的语法有两种:

1:数据类型[] 数组名={值,值,值};//在知道具体值的时候使用这种方法

2:  数据类型[] 数组名=new 数据类型[]  //多采用第二种因为以后学习面相对象的时候会涉及到地址的问题

为什么需要数组?
因为要存储多个值时,使用变量较为繁琐,为了简化,使用数组

什么是数组?
数组是相同数据类型变量的集合

数组与变量比的优势:
1、使用更简洁
2、使用效率更高

如何定义数组:

只要数据类型相同类形同就可以定义一个数组例如:

数据类型[] 数组名A={人名,人名,人名};

 数据类型[] 数组名B={动物,动物,动物};

数据类型[] 数组名C={水果,水果,水果};

 

数组的使用:
1、数组长度就是数组里的元素个数
2、数组元素,就是数组里存储的值
3、当声明数组后,系统给数组里的每一个元素进行了自动的编号
   第一个元素的编号是0,第二个是1,以此类推,这个编号叫做  索引

数组长度=length-1 //数组里面的所有值因为数组元素从0开始


Random rd= new Random();//是随机数
  int num1=rd.nextInt(100);  //int num1=rd.nextlnt(这里的数可变的,从零开始包括它本身);

 

 char[] aryChar={'1','b','A','3','R','c','W'};
  //把数组中的大写字母,转换为小写字母
  //问题:如何判断每一个元素是大写字母==========》if(aryChar[i]>='A' && aryChar[i]<='Z')//判断数组中的元素的字母是否是有A-Z之间的大写字母
  //aryChar[i]=(char)(aryChar[i]+32);大写字母+32就转换为了小写字母,因为32是int类型所以要强转把int类型转换成char类型
//要是小写转换成大写的话先判断从小a到小z之间是否有替他的小写字母然后-32就是将数组中所有小写转换成大写

 

protected void RemoteDataUpdate(object sender, NMC.Scheduling.NID.RemoteDataMonitorImpl.RmtDataUpdateEventArgs args) //public override void RemoteDataUpdate(CommRemotingInterface.Wrappers.RmtDataUpdateEventArgs args) { //lock (this) // recursive lock with 'this.State' <><> lock (this.mActionList) { IComData.RemoteIntValueInfo info = args.Info as IComData.RemoteIntValueInfo; if (info == null) { Coordinator.GetIns().GetLogger().FatalFormat("Module::RemoteDataUpdate({0}): Not 'INT' Data.", args.Name); return; } if (info.validity != IComData.RemoteValidity.VALID) { Coordinator.GetIns().GetLogger().ErrorFormat("Module::RemoteDataUpdate({0}): Value validity is '{1}'.", args.Name, info.validity); return; } if (args.Name == this.mLogicStateName) { //// <> a Module logic state update switch (info.descriptor.ToUpper()) { case "NOTREADY": this.State = ModuleState.NotReady; Notifier.GetIns().PostEvent(CoordinatorEvents.ModuleNotReady, new CoordEventArgs(this.mName), string.Format("Module[{0}]::RemoteDataUpdate(): ModuleNotReady.", this.mName)); break; case "OFFLINE": this.State = ModuleState.Offline; Notifier.GetIns().PostEvent(CoordinatorEvents.ModuleOffline, new CoordEventArgs(this.mName), string.Format("Module[{0}]::RemoteDataUpdate(): ModuleOffline.", this.mName)); break; case "ONLINE": this.State = ModuleState.Online; Notifier.GetIns().PostEvent(CoordinatorEvents.ModuleOnline, new CoordEventArgs(this.mName), string.Format("Module[{0}]::RemoteDataUpdate(): ModuleOnline.", this.mName)); break; default: // impossible, do nth <> break; } } else if (args.Name == this.mFullName) { // <> a ControlObject running state update switch (info.descriptor.ToUpper()) { case "IDLE": if (this.mRunState == IComControlObject.RemoteControlState.RUNNING) { if (this.mCurAction != null) { this.mCurAction.Guess(ActionState.Done); // <> recursive lock in 'Guess()::ClearCurAction()' } } this.mRunState = IComControlObject.RemoteControlState.IDLE; break; case "ABORTED": if (this.mCurAction != null) { this.mCurAction.Guess(ActionState.Aborted); // <> recursive lock in 'Guess()::ClearCurAction()' } this.mRunState = IComControlObject.RemoteControlState.ABORTED; break; case "RUNNING": this.mRunState = IComControlObject.RemoteControlState.RUNNING; break; case "ABORTING": this.mRunState = IComControlObject.RemoteControlState.ABORTING; break; case "ALMPAUSE": this.mRunState = IComControlObject.RemoteControlState.ALMPAUSE; break; case "ALMRESUME": this.mRunState = IComControlObject.RemoteControlState.ALMRESUME; break; default: // impossible, do nth <> break; } } else if (args.Name.ToLower().LastIndexOf("slot") >= 0) // <> Slot data update { int pos = args.Name.ToLower().LastIndexOf("slot"); //if (pos < 0) //{ // Console.WriteLine("---ERR---- No key world 'Slot' found in '{0}'! -------\n", args.Name); // return; //} int slot = 0; try { slot = Int32.Parse(args.Name.Substring(pos + 4)); } catch (System.Exception) { Console.WriteLine("---ERR---- Failed to parse slot number from '{0}'! -------\n", args.Name); return; } lock (this.mMaterials) { if (!this.mMaterials.ContainsKey(slot)) { // log Console.WriteLine("---ERR---- {0} No slot {1}! -------\n", args.Name, slot); return; } switch (info.descriptor.ToUpper()) { case "ABSENT": this.mMaterials[slot] = MaterialExistence.Absent; break; case "PRESENT": this.mMaterials[slot] = MaterialExistence.Present; break; case "DOUBLE": this.mMaterials[slot] = MaterialExistence.Double; break; case "CROSS": this.mMaterials[slot] = MaterialExistence.Cross; break; case "SHUTTERED": this.mMaterials[slot] = MaterialExistence.Shuttered; break; default: this.mMaterials[slot] = MaterialExistence.Unknown; break; } } // <> yangy 20170602: move out from 'lock (this.mMaterials)' //this.SlotStateChange(info.descriptor.ToUpper(), slot); // <> subclass may be interest in this } // end if data type check else if (args.Name == this.IsAutoRorName) // xianghf_20240326, AutoRor CIP. Remove the following code from the SubclassUpdate function in ProcessChamber { if (info.descriptor.ToUpper() == "PENDING") { Notifier.GetIns().PostEvent(CoordinatorEvents.Pending4CleanOnGo, new CoordEventArgs(this.Name), string.Format("Module[{0}]::RemoteDataUpdate({1}, {2})", this.Name, args.Name, info.descriptor)); } } // end if data type check // this.SubclassUpdate(args.Name, info.descriptor); // <> for special requirement, subclass can override this this.SubclassUpdate(args.Name, (string.IsNullOrEmpty(info.descriptor) ? info.value.ToString() : info.descriptor)); } // end lock } // end method 详细分析代码
最新发布
08-28
这段代码是C#语言编写的一个事件处理函数`RemoteDataUpdate`,用于处理远程数据更新的逻辑,主要功能是根据接收到的远程数据更新模块状态、控制对象状态、物料槽状态等,并触发相应的事件通知。代码逻辑清晰,结构合理,适用于工业自动化控制系统中模块状态的远程监控。 --- ### 一、函数定义 ```csharp protected void RemoteDataUpdate(object sender, NMC.Scheduling.NID.RemoteDataMonitorImpl.RmtDataUpdateEventArgs args) ``` - **函数作用**:处理远程数据更新事件。 - **参数说明**: - `sender`:事件触发者(未使用)。 - `args`:事件参数,包含远程数据信息。 --- ### 二、线程同步与类型检查 ```csharp lock (this.mActionList) { IComData.RemoteIntValueInfo info = args.Info as IComData.RemoteIntValueInfo; if (info == null) { Coordinator.GetIns().GetLogger().FatalFormat("Module::RemoteDataUpdate({0}): Not 'INT' Data.", args.Name); return; } if (info.validity != IComData.RemoteValidity.VALID) { Coordinator.GetIns().GetLogger().ErrorFormat("Module::RemoteDataUpdate({0}): Value validity is '{1}'.", args.Name, info.validity); return; } ``` - **线程安全**:使用 `lock` 锁住 `mActionList`,防止多线程冲突。 - **类型检查**:确保传入的数据是 `RemoteIntValueInfo` 类型。 - **有效性检查**:判断数据是否有效,无效则记录错误并返回。 --- ### 三、模块逻辑状态更新(`mLogicStateName`) ```csharp if (args.Name == this.mLogicStateName) { switch (info.descriptor.ToUpper()) { case "NOTREADY": this.State = ModuleState.NotReady; Notifier.GetIns().PostEvent(CoordinatorEvents.ModuleNotReady, new CoordEventArgs(this.mName), string.Format("Module[{0}]::RemoteDataUpdate(): ModuleNotReady.", this.mName)); break; case "OFFLINE": this.State = ModuleState.Offline; Notifier.GetIns().PostEvent(CoordinatorEvents.ModuleOffline, new CoordEventArgs(this.mName), string.Format("Module[{0}]::RemoteDataUpdate(): ModuleOffline.", this.mName)); break; case "ONLINE": this.State = ModuleState.Online; Notifier.GetIns().PostEvent(CoordinatorEvents.ModuleOnline, new CoordEventArgs(this.mName), string.Format("Module[{0}]::RemoteDataUpdate(): ModuleOnline.", this.mName)); break; default: break; } } ``` - **功能**:根据远程数据更新模块状态(如NotReady、Offline、Online)。 - **事件通知**:状态变化时通过 `Notifier` 发送事件通知,供其他模块监听。 --- ### 四、控制对象运行状态更新(`mFullName`) ```csharp else if (args.Name == this.mFullName) { switch (info.descriptor.ToUpper()) { case "IDLE": if (this.mRunState == IComControlObject.RemoteControlState.RUNNING) { if (this.mCurAction != null) { this.mCurAction.Guess(ActionState.Done); } } this.mRunState = IComControlObject.RemoteControlState.IDLE; break; case "ABORTED": if (this.mCurAction != null) { this.mCurAction.Guess(ActionState.Aborted); } this.mRunState = IComControlObject.RemoteControlState.ABORTED; break; case "RUNNING": this.mRunState = IComControlObject.RemoteControlState.RUNNING; break; case "ABORTING": this.mRunState = IComControlObject.RemoteControlState.ABORTING; break; case "ALMPAUSE": this.mRunState = IComControlObject.RemoteControlState.ALMPAUSE; break; case "ALMRESUME": this.mRunState = IComControlObject.RemoteControlState.ALMRESUME; break; default: break; } } ``` - **功能**:更新控制对象的运行状态(如Running、Aborted、Idle等)。 - **动作处理**:在状态为IDLE或ABORTED时,调用当前动作的 `Guess()` 方法,结束当前动作。 - **线程安全**:由于调用了 `Guess()`,该方法内部可能再次加锁,需注意防止死锁。 --- ### 五、槽位状态更新(`Slot`) ```csharp else if (args.Name.ToLower().LastIndexOf("slot") >= 0) { int pos = args.Name.ToLower().LastIndexOf("slot"); int slot = 0; try { slot = Int32.Parse(args.Name.Substring(pos + 4)); } catch (System.Exception) { Console.WriteLine("---ERR---- Failed to parse slot number from '{0}'! -------\n", args.Name); return; } lock (this.mMaterials) { if (!this.mMaterials.ContainsKey(slot)) { Console.WriteLine("---ERR---- {0} No slot {1}! -------\n", args.Name, slot); return; } switch (info.descriptor.ToUpper()) { case "ABSENT": this.mMaterials[slot] = MaterialExistence.Absent; break; case "PRESENT": this.mMaterials[slot] = MaterialExistence.Present; break; case "DOUBLE": this.mMaterials[slot] = MaterialExistence.Double; break; case "CROSS": this.mMaterials[slot] = MaterialExistence.Cross; break; case "SHUTTERED": this.mMaterials[slot] = MaterialExistence.Shuttered; break; default: this.mMaterials[slot] = MaterialExistence.Unknown; break; } } // this.SlotStateChange(info.descriptor.ToUpper(), slot); } ``` - **功能**:根据槽位名称(如Slot1、Slot2)更新物料存在状态(如Present、Absent、Double等)。 - **异常处理**:如果槽位编号解析失败,记录错误并返回。 - **线程安全**:对 `mMaterials` 字典加锁,保证线程安全访问。 - **状态管理**:使用枚举 `MaterialExistence` 表示物料状态。 --- ### 六、特殊状态处理(`IsAutoRorName`) ```csharp else if (args.Name == this.IsAutoRorName) { if (info.descriptor.ToUpper() == "PENDING") { Notifier.GetIns().PostEvent(CoordinatorEvents.Pending4CleanOnGo, new CoordEventArgs(this.Name), string.Format("Module[{0}]::RemoteDataUpdate({1}, {2})", this.Name, args.Name, info.descriptor)); } } ``` - **功能**:处理自动旋转(AutoRor)状态为PENDING时的特殊事件。 - **事件通知**:触发 `Pending4CleanOnGo` 事件,供其他模块响应。 --- ### 七、子类扩展处理(`SubclassUpdate`) ```csharp this.SubclassUpdate(args.Name, (string.IsNullOrEmpty(info.descriptor) ? info.value.ToString() : info.descriptor)); ``` - **功能**:提供给子类实现的扩展方法,用于处理特定业务逻辑。 - **参数处理**:优先使用 `descriptor`,若为空则使用 `value` 字符串表示。 --- ### 八、总结 | 模块 | 功能 | |------|------| | 模块状态更新 | 处理模块的NotReady、Offline、Online状态 | | 控制对象状态 | 处理Running、Aborted、Idle等运行状态 | | 槽位状态 | 更新物料存在状态(Present、Absent等) | | 特殊状态处理 | 如AutoRor的PENDING状态 | | 子类扩展 | 提供 `SubclassUpdate` 方法供子类扩展 | --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值