首页 \ 问答 \ C#类实例列表(C# List of class instances)

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
更新时间:2023-01-16 06:01

最满意答案

尝试

$("#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>";
}

相关问答

更多
  • 我这样做是通过在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 ...
  • 尝试 $("#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 ...
  • 您可以在单击函数中获取所需的值,并将它们作为GET请求传递。 例如 : $("#go").bind("click", function () { var mail = $('#email').value(); $.fancybox({ 'content': '

    Join Our Email List