当前位置:编程学习 > C#/ASP.NET >>

C#怎么调用 类中的函数啊?

namespace DotNet.Utilities
{    
public class ImageClass   
 { 
 public bool Compress(string oldfile, string newfile, int width, int height)      
  {          }
...... 
--------------------编程问答-------------------- ImageClass img = new ImageClass();
img.xxxxxx --------------------编程问答-------------------- 调用Compress函数出错?
这个是怎么回事? --------------------编程问答-------------------- 因为你没有实例化,Compress也不是静态方法;
1.实例化:
ImageClass img = new ImageClass();
img.Compress(...);
2.静态方法:
public static bool Compress(string oldfile, string newfile, int width, int height)      
  {          }
1,2任选其一 --------------------编程问答-------------------- 问题解决了,谢谢! --------------------编程问答-------------------- 非静态需要实例化的!静态方法才可用直接调用! --------------------编程问答--------------------
new ImageClass().Compress(oldfile,newfile,width,height);
--------------------编程问答-------------------- 除 --------------------编程问答-------------------- public bool Compress(string oldfile, string newfile, int width, int height)      
  {          }

改成  

public static bool Compress(......)

你看说明,想要通过类名直接调用的,只有静态方法才可以。
不然你就要通过   new ImageClass().compress(....)来。
这是调用成员方法。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,