C#类实例列表(C# List of class instances)
我不是编程新手,但我对C#很新。 我正在尝试使用类的实例填充列表。
我希望下面的代码在控制台中显示数字0到9,但它重复9次十次。 显然,我做错了什么。
我怀疑在列表中添加“a”只是添加对“a”的引用,而不是class1的新实例。 我不知道我应该添加什么。 将新的class1实例添加到列表中的正确语法是什么?
提前感谢您的回答!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<class1> iList = new List<class1>(); class1 a = new class1(); for (int i = 0; i < 10; i++) { iList.Add(a); iList[i].var1 = i; } for (int i = 0; i < iList.Count; i++) { System.Console.Write("var1 of " + i + ": " + iList[i].var1 + "\r\n"); } Console.ReadLine(); } } class class1 { public int var1; } }
I am not new to programming but I am very new to C#. I am trying to populate a list with instances of a class.
I would expect the following piece of code to display numbers 0 to 9 in the console but instead it repeats 9 ten times. Clearly I am doing something wrong.
I am suspecting that adding "a" to the list simply adds a reference to "a", and not a new instance of class1. I am not sure what I should be adding though. What would be the correct syntax to add a new instance of class1 to the list?
Thanks in advance for any answer!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<class1> iList = new List<class1>(); class1 a = new class1(); for (int i = 0; i < 10; i++) { iList.Add(a); iList[i].var1 = i; } for (int i = 0; i < iList.Count; i++) { System.Console.Write("var1 of " + i + ": " + iList[i].var1 + "\r\n"); } Console.ReadLine(); } } class class1 { public int var1; } }
原文:https://stackoverflow.com/questions/34346423
最满意答案
尝试
$("#form").bind("submit", function () { $.fancybox.showLoading(); // it was $.fancybox.showActivity(); for v1.3.4 $.ajax({ type: "POST", cache: false, url: "test.php", // make sure your path is correct data: $(this).serializeArray(), // your were using $(form).serialize(), success: function (data) { $.fancybox(data); } }); return false; }); // bind
现在,
$.fancybox(data);
将返回(在fancybox内)你从text.php文件发送的任何内容,这样你就可以从该文件中返回<div id="message">
,如:if($query){ echo "<div id='message'>Data for $name inserted successfully!</div>"; } else { echo "<div id='message'>An error occurred!</div>"; }
Try
$("#form").bind("submit", function () { $.fancybox.showLoading(); // it was $.fancybox.showActivity(); for v1.3.4 $.ajax({ type: "POST", cache: false, url: "test.php", // make sure your path is correct data: $(this).serializeArray(), // your were using $(form).serialize(), success: function (data) { $.fancybox(data); } }); return false; }); // bind
Now,
$.fancybox(data);
will return (inside fancybox) whatever you sent from the text.php file so you could return the<div id="message">
from within that file like :if($query){ echo "<div id='message'>Data for $name inserted successfully!</div>"; } else { echo "<div id='message'>An error occurred!</div>"; }
相关问答
更多-
YouTube和Fancybox 2(YouTube and Fancybox 2)[2022-03-02]
我这样做是通过在URL的末尾添加?rel=0来隐藏所有相关视频( 这里有关于rel param的更多信息) 您可以为每个视频网址(手动)添加?rel=0 ,或者让fancybox beforeLoad函数在打开之前添加它:(示例还将自动播放设置为1) beforeLoad: function () { var url = $(this.element).attr("href"); url += '?autoplay=1&rel=0'; this.href = url; } (请注意 ... -
解决方案是: $this->widget('application.extensions.fancybox.EFancyBox', array( 'target' => 'a.fitGraphic', 'id' => 'fitGraphic', 'config' => array( 'type' => 'inline', 'min-width' => '300px', 'min-h ...
-
使用Fancybox 2发布数据(Posting data with Fancybox 2)[2024-02-15]
尝试 $("#form").bind("submit", function () { $.fancybox.showLoading(); // it was $.fancybox.showActivity(); for v1.3.4 $.ajax({ type: "POST", cache: false, url: "test.php", // make sure your path is correct data: $(thi ... -
fancybox变量(fancybox variables)[2021-05-12]
您可以在单击函数中获取所需的值,并将它们作为GET请求传递。 例如 : $("#go").bind("click", function () { var mail = $('#email').value(); $.fancybox({ 'content': 'Join Our Email List
好的,我已经设法解决了这个问题。 不完全是我想要的,但我会工作得很好。 我的解决方案使用onComplete回调来强制将overlay的display属性设置为block 。 它被打包在setTimeout因为它立即调用它将无法正常工作。 在一定时间内减少超时也会导致此解决方案失败。 我相信有更好的方法可以解决这个问题,但我还没有找到...但是;) $.fancybox( $('#notifications_fancybox').html(), { 'au ...通过使用webdriver解决 import selenium.webdriver as webdriver driver = webdriver.Chrome(r"C:\Users\...\chromedriver.exe") driver.get("http://...") content = driver.find_element_by_xpath('') content.click() urls = re.findall('https://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[! ...鉴于您的示例代码,我相信您应该选择datacity作为ID而不是CLASS。 在当前形式中,jQuery选择失败,并且执行的默认行为而不是jQuery代码。 要选择此项: Go 用这个: $('#datacity').on("click", function (e) { 而不是这个: $('.datacity').on("click", funct ...基本的Fancybox实现(Basic Fancybox Implementation)[2022-03-03]
在这些情况下,您应该始终关注开发者控制台的任何线索。 就你而言,消息说: TypeError:$(...)。on不是函数 这意味着该功能“on”不存在。 jQuery文档 - http://api.jquery.com/on/ - 就是这样说的 版本添加:1.7 所以,你需要高于1.7的jQuery版本才能使它工作。 In those situations you should always look at developer console for any clue. In your case, the ...Fancybox内容不刷新(Fancybox Content do not refresh)[2022-04-04]
所以我能够解决这个问题,我在我的fancybox调用中使用了这个代码: afterLoad: function(){ this.content = $(this.element).attr('rel'); } 我也改变了我的PHP代码: echo '' .truncate($value['managementSummary']). '
代替 alert($(this).data('album-id')); 使用 alert($(this.element).data('album-id')); 小提琴 fancybox将图像,ajax等内容类型设置为元素 fancybox提示和技巧部分有很好的样本和所有摆弄 instead of alert($(this).data('album-id')); use alert($(this.element).data('album-id')); fiddle fancybox set conte ...相关文章
更多- C#开发微信门户及应用(4)--关注用户列表及详细信息管理
- hibernate 对list修改
- FreeMarker集合(List、Map)
- C#开发微信门户及应用(3)--文本消息和图文消息的应答
- 高性能solr c#客户端EasyNet.Solr
- C#开发微信门户及应用(5)--用户分组信息管理
- 用C#捕捉键盘和鼠标
- 基于C#的UDP协议的同步实现
- 微信公众平台入门开发教程.Net(C#)框架
- Map、List集合类的清空使用clear还是重新new呢
最新问答
更多- 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
- 如何通过引用返回对象?(How is returning an object by reference possible?)
- 矩阵如何存储在内存中?(How are matrices stored in memory?)
- 每个请求的Java新会话?(Java New Session For Each Request?)
- css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
- 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
- xcode语法颜色编码解释?(xcode syntax color coding explained?)
- 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
- 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
- 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
- 西安哪有PLC可控制编程的培训
- 在Entity Framework中选择基类(Select base class in Entity Framework)
- 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
- 电脑二级VF有什么用
- Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
- 金华英语角.
- 手机软件如何制作
- 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
- 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
- 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
- Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
- 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
- python的访问器方法有哪些
- DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
- 在Ruby中对组合进行排序(Sorting a combination in Ruby)
- 网站开发的流程?
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。