如何在不调用方法的情况下从另一个方法访问var

如何解决如何在不调用方法的情况下从另一个方法访问var

您好,这是我关于堆栈溢出的拳头真实文章,如果无法正确执行:D

ExternalController

我目前正在尝试找出如何致电,而不必将其定义为公众 (因为使用var context = await _interaction.GetAuthorizationContextAsync(returnUrl); // redirect to external Identity provider automatically,if requested if (string.IsNullOrWhiteSpace(context.IdP) == false) { var idp = context.IdP; return RedirectToAction("Challenge","ExternalAuthentication",new { provider = idp,returnUrl }); } 时由于某种原因而无法使用),而不必在方法namespace foobar //not originally named foobar { [DesignTimeVisible(false)] public partial class MainPage : ContentPage { int Spinrate = 2000; int Points = 1; int clickmulti = 1; public static object Public { get; private set; } public MainPage() { InitializeComponent(); var gamemusic = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; var laughtrack = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); gamemusic.Load("audioloop.mp3"); laughtrack.Load("wslaugh.wav"); gamemusic.Play(); } async void bigger(object sender,EventArgs args) { Points += Spinrate + (50 * 2 * clickmulti); Pointdisplay.Text = Convert.ToString(Points); cmdisplay.Text = Convert.ToString(clickmulti); if (Spinrate >= 300) { Spinrate -= 50; } if (Spinrate <= 1000) { clickmulti = 2; } if (Spinrate <= 500) { clickmulti = 3; } await img.RelRotateTo(360,(uint)Spinrate); laughtrack.Play(); } } } 中定义var 因为它在程序中造成了很多延迟。

目前我已经尝试过:

  1. var laughtrack = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();设为公共变量,但出现以下错误:

bigger作为输出

  1. 我在类范围内定义了laughtrack,并且收到与尝试1相同的错误。

  2. 我在同一方法中添加了声明并调用Error CS0825 The contextual keyword 'var' may only appear within a local variable declaration or in script code foobar。它似乎会引起很多延迟,即使不是原因,每次调用该方法时,它仍然会加载它。

感谢所有为此付出努力的人!

解决方法

如果要使其在其他方法中可见,则需要在级别(不在特定方法内)声明laughtrack。但是,如果执行此操作,则不能使用var,则必须知道实际的类型。

ISimpleAudioPlayer laughtrack;

public MainPage()
{
  InitializeComponent();
  var gamemusic = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
  laughtrack = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
  gamemusic.Load("audioloop.mp3");
  laughtrack.Load("wslaugh.wav");
  gamemusic.Play();
}
,

您的第一次尝试非常好,而且确实是您需要执行的操作,但是var在函数外部无法使用。只需明确地写出变量的类型即可!

,

当您创建一个方法或简单地将其称为函数时,您在其中定义的所有变量或嵌套函数都将作用于该特定函数或代码块,并且在该作用域之外将不可用,除非被以下方法返回或抛出该功能。

在您的情况下,标准方法是使用属性和方法定义结构。但是,请考虑如何启动这些属性。如果那是通过调用一个函数,那么您仍然需要调用该函数,但是它允许出现Singleton行为。

为进一步阐明,考虑纯函数编程的概念,其中纯函数需要一些输入并返回单个输出。在OOP中,该方法可以更新全局变量(在C#中没有这样的东西,但是C ++和C有这些变量)以及类或结构中的作用域属性/变量。但是,您不能直接访问其成员。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-