C#中调用带参数的类

风一样的大叔 2012-12-21 05:41:57
 
public partial class Route : Form
{
public List<string> picCount = new List<string>();
public Route(string statusInfo)
{
InitializeComponent();
this.listBox1.Focus();
this.statusInfo = statusInfo;
}
}

public class ThreadWithState
{
public void ThreadProc()
{
//这边报错, 没用0个参数
Route r = new Route("");
//请问这边该怎么改?
r.picCount...
}
}


求解决方法
...全文
814 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
风一样的大叔 2013-01-07
  • 打赏
  • 举报
回复
用委托与事件解决了
hzexe 2012-12-23
  • 打赏
  • 举报
回复
引用 10 楼 wawd74520 的回复:
引用 6 楼 caozhy 的回复:一颗星,不应该。 ++
++
njw1028 2012-12-23
  • 打赏
  • 举报
回复
引用 6 楼 caozhy 的回复:
一颗星,不应该。
亮了。
「已注销」 2012-12-22
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
不知道一颗星怎么上来的
风一样的大叔 2012-12-22
  • 打赏
  • 举报
回复
报:没有为任何堆栈架构加载任何符号,无法显示代码
风一样的大叔 2012-12-22
  • 打赏
  • 举报
回复
引用 1 楼 Mockqi 的回复:
引用带参数的类 真能想 那分明是构造函数 C# code?123456789101112131415161718192021 public Post(int Id,string title) { this.ID = Id; this.Title = title; } p……
你看我的代码,传空值,报错了
qinzhu_ 2012-12-22
  • 打赏
  • 举报
回复
来到我身边 2012-12-22
  • 打赏
  • 举报
回复
引用 6 楼 caozhy 的回复:
一颗星,不应该。
what's the mesning
失落的神庙 2012-12-22
  • 打赏
  • 举报
回复
引用 6 楼 caozhy 的回复:
一颗星,不应该。
++
dl_newpage 2012-12-22
  • 打赏
  • 举报
回复
public Route(string statusInfo) { InitializeComponent(); this.listBox1.Focus(); this.statusInfo = statusInfo; } 自定义构造函数后,当进行实例化时通过该构造函数对其进行初始化,构造函数所带参数也要在相应位置赋值。例: public Route(string statusInfo,int intIndex){} 实例化时Route GetRoute=new Route("open",0) 。参数不能空缺。不知道说得对不对,有误请指正。
风一样的大叔 2012-12-22
  • 打赏
  • 举报
回复
引用 6 楼 caozhy 的回复:
一颗星,不应该。
波西米亚人生 2012-12-21
  • 打赏
  • 举报
回复
解决办法有2种: 方法一,在Program中的入口实例化构造函数就必须传参:
 static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Route());
        }
    }
方法二:
 
public partial class Route : Form
    {
        public List<string> picCount = new List<string>();
        public Route(string statusInfo)
        {
            InitializeComponent();
            this.listBox1.Focus();
            this.statusInfo = statusInfo;
        }
    }

public class ThreadWithState
    {
        public void ThreadProc()
        {
            //这边报错, 没用0个参数          
            Route r = new Route("");
            //请问这边该怎么改?
            r.picCount...
        }
    }
类Route中没有默认的构造函数引起的,只要添加默认的构造函数就可以了。
threenewbee 2012-12-21
  • 打赏
  • 举报
回复
一颗星,不应该。
kxdabao 2012-12-21
  • 打赏
  • 举报
回复
Route 类 在 Program.cs 里看有没有启动 Application.Run(new Route("dd")); 如果是这样Application.Run(new Route());会报错 改成 Application.Run(new Route(""));
Assassin_ 2012-12-21
  • 打赏
  • 举报
回复
Route r = new Route("aaaaaaaaaaa"); 给构造函数传值
  • 打赏
  • 举报
回复


 public class Post
    {
        public Post(int Id,string title)
        {
            this.ID = Id;
            this.Title = title;
        }

        public int ID { get; set; }
        public int CategoryID { get; set; }

        public string Title { get; set; }
        public string Summary { get; set; }
        public string Alias { get; set; }
        public string Content { get; set; }
        public DateTime CreateTime { get; set; }

        public Category Category { get; set; }
        public ICollection<Tag> Tags { get; set; }
        public ICollection<Comment> Coments { get; set; }
    }

Post post=new Post(1,"title")

Change_L 2012-12-21
  • 打赏
  • 举报
回复
public void fangfa() ; public gouzao(); 你那是构造。
  • 打赏
  • 举报
回复
引用
带参数的类
真能想 那分明是构造函数


   public Post(int Id,string title)
        {
            this.ID = Id;
            this.Title = title;
        }
        public int ID { get; set; }
        public int CategoryID { get; set; }

        public string Title { get; set; }
        public string Summary { get; set; }
        public string Alias { get; set; }
        public string Content { get; set; }
        public DateTime CreateTime { get; set; }

        public Category Category { get; set; }
        public ICollection<Tag> Tags { get; set; }
        public ICollection<Comment> Coments { get; set; }
    }


    Post post=new Post(1,"title")

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧