Tag:

从另一个类文件访问C#表单文本框

我想从另一个类文件(例如chartscopier.cs)访问Form1元素,但我无法更改chartscopier.cs中的textbox1文本。 我怎样才能做到这一点? 这是我的代码: Form1.cs的 namespace TEST { public partial class Form1 : Form { public Form1() { InitializeComponent(); var CopyCharts = new System.Threading.Timer(chartscopier.CopyGraph, null, 0, 60000); } } } chartscopier.cs namespace TEST { class chartscopier { //var Timer = new Timer(CopyGraph, null, 0, 60000); public static void CopyGraph(object data) { Stopwatch strTimer = new Stopwatch(); […]

什么时候结构太大了?

可能重复: C#中的结构Vs类 很抱歉,如果这是一个开放式问题,但我只是想知道我的结构是否太大了。 我想要使​​用结构的原因是因为我已经知道它们比类更快,我认为我真的需要速度。 我发现如果你的结构太大,它实际上会减慢你的程序,所以我想知道这个的指导方针以及我的结构是否需要转换为类。 public struct Tiles { public Rectangle rect; //i know this wont run with them being initalized like this but if i change to a class this will //stay like this and im in the middle of deciding what to do Bitmap currentPic = new Bitmap(50, 50); ImageAttributes imgAttr = new […]

无法访问公共类属性

我正在尝试创建一个应该在其代码中包含这些变量的新类: class Map { // Variable declaration public int Width { get; set; } // Width of map in tiles public int Height { get; set; } // Height of map in tiles public int TileWidth { get; set; } public int TileHeight { get; set; } } 但出于某种原因,在Game1.cs中创建一个新的Map类后,我似乎无法解决诸如Width和Height之类的问题。 public class Game1 : Microsoft.Xna.Framework.Game { […]

在类中使用扩展方法,它们扩展

考虑这个课程: public class Thing { public string Color { get; set; } public bool IsBlue() { return this.Color == “Blue”; // redundant “this” } } 我可以省略关键字this因为Color是Thing的属性,我在Thing编码。 如果我现在创建一个扩展方法: public static class ThingExtensions { public static bool TestForBlue(this Thing t) { return t.Color == “Blue”; } } 我现在可以将我的IsBlue方法更改为: public class Thing { public string Color { get; […]

如何将值从主窗体返回到不同的forms或类(C#)

由于一些不明原因,这个愚蠢的事情无法实施。 我在主窗体中有一个int count ,我想返回另一个类或表单。 namespace my_speller { public partial class login : Form { public login() { InitializeComponent(); } int count; private void btnlogin_MouseUp(object sender, MouseEventArgs e) { dbaccess obj = new dbaccess(); for (int i = 0; i < 10; i++) { if (txtusername.Text == obj.Usersusername()[i]) { count = i; break; } } } […]

访问接口方法而不引用类

假设我在一个名为“Interface”的项目中有这样的接口: public interface TestInterface { string Operation(); } 和实现它的类。 该类位于另一个项目“Class”中: public class TestClass : TestInterface { public TestClass() { } public string Operation() { return “This is an Operation”; } } 我的客户做了类似这样的事情(这又是一个不同的项目“客户”): class Program { static void Main(string[] args) { TestInterface i = new TestClass(); i.Operation(); } } 我的问题与这一行有关: TestInterface i = new TestClass(); 通过添加这一行,我实际上被迫在我的“客户端”项目中添加对“接口”和“类”项目的引用。 […]

在c#中将一组成员标记为私有/公共

在c ++类声明中,您可以将一组成员标记为私有或公共,例如 private: int x; double y; 似乎在c#中没有办法做到这一点。 我错了吗?

C# – 将字符串转换为类对象

我正在使用其他人的代码并尝试进行一些修改。 所以我需要做的是采取以下措施: RemoteFileDP remoteFile = new DPFactory().CreateRemoteFileDP(configData); 并更改它,以便remoteFile可以等于字符串变量中的内容。 为了进一步解释,让我再给出一些代码: ConfigDP configData = new ConfigDP(); 所以上面的语句是在remoteFile语句之前执行的,而ConfigDP在它上面有两个类(抽象Config,然后是它的base:abstract ConfigBase)。 DP也是它上面两个抽象类的子代(抽象RemoteFile和抽象RemoteFileBase)。 根据我的理解,remoteFile是从数据库查询中提取的数据的结果,存储在列表或Hashtable中(抱歉只是一个实习生所以我正在研究这个)。 我需要remoteFile接受字符串值的原因是因为有很多方法利用了remoteFile中的信息,我想避免创建一个接受字符串值而不是RemoteFileDP remoteFile的重载方法的WHOLE BUNCH。 所以如果我可以取一个字符串值,如: string locationDirectory; 从另一个方法传入,然后有类似于以下内容: RemoteFileDP remoteFile = locationDirectory; 那么使用remoteFile的所有其他方法都不必重载或更改。 很抱歉所有细节,但这是我第一次发布,所以我希望我提供了足够的信息。 我确实看了C#将动态字符串转换为现有的Class和C#:使用运行时确定的类型实例化对象并编写以下代码: RemoteFilesDP remoteFile = (RemoteFileDP)Activator.CreateInstance(typeof(RemoteFileDP), locationDirectory); 但是我一直得到一个“MissingMethodException”错误,找不到RemoteFileDP的构造函数,但我确实有如下所示的构造函数: public RemoteFileDP() { } //end of RemoteFilePlattsDP constructor 提前感谢您的帮助!

new()约束对类定义有什么作用?

我看到了这个代码示例,并想知道new()约束的目的是什么: public class Client : IClient where T : IClientFactory, new() { public Client(int UserID){ } }

C#binarysearch由T的成员列出

我有一个带有DateTime成员TimeStamp的基类Event 。 许多其他事件类将源于此。 我希望能够快速搜索事件列表,所以我想使用二进制搜索。 (列表数据按时间戳排序,但同时发生的事件可能存在重复的时间戳) 所以我开始写这样的东西: public class EventList : List where T : Event { private IComparer comparer = (x, y) => Comparer.Default.Compare(x.TimeStamp, y.TimeStamp); public IEnumerable EventsBetween(DateTime inFromTime, DateTime inToTime) { // Find the index for the beginning. int index = this.BinarySearch(inFromTime, comparer); // BLAH REST OF IMPLEMENTATION } } 问题是BinarySearch只接受T(所以 – 一个Event类型)作为参数,而我想基于T的成员 – […]