我目前正试图将我的一些代码与我的主窗体分开,并创建类文件来处理一些操作.我已经读到将类绑定到UI并不是一个好习惯,所以我想知道如何仍然返回有关课堂上发生的事件进展的信息.当代码出现在主窗体中时,我会将函数的状态更新写入文本框,以便用户可以知道发生了什么.
如何通过主窗体从类更新用户的例程状态?
我试图使用C++ DLL在C#中编辑我的StringBuilder对象.我的C++代码如下所示:
extern "C" __declspec(dllexport) void __stdcall PrintHead(char* myString)
{
myString = "testIsOkey";
}
Run Code Online (Sandbox Code Playgroud)
我的C#代码是:
[DllImport("StringEdit.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = false)]
public static extern void PrintHead([MarshalAs(UnmanagedType.LPStr)] StringBuilder stringBuilder);
private void button1_Click(object sender, EventArgs e)
{
StringBuilder stringBuilder = new StringBuilder("123456");
PrintHead(stringBuilder);
}
Run Code Online (Sandbox Code Playgroud)
在调用PrintHead之后,我希望看到stringBuilder对象的值从"123456"更改为"testIsOkey",但它不会更改.我无法弄清楚我在哪里弄错了.
谢谢你的帮助.
我对反思有点新意,请原谅我,如果这是一个更基本的问题我正在用c#编写一个程序,并且我试图写一个通用的Empty或null checker方法到目前为止代码读取为
public static class EmptyNull
{
public static bool EmptyNullChecker(Object o)
{
try
{
var ob = (object[]) o;
if (ob == null || !ob.Any())
return true;
}
catch (Exception e)// i could use genercs to figure out if this a array but //figured i just catch the exception
{Console.WriteLine(e);}
try
{
if (o.GetType().GetGenericTypeDefinition().Equals("System.Collections.Generic.List`1[T]"))
//the following line is where the code goes haywire
var ob = (List<o.GetType().GetGenericArguments()[0].ReflectedType>)o;
if (ob == null || !ob.Any())
return true;
}
catch (Exception e) …Run Code Online (Sandbox Code Playgroud) 我用来Directory.Exists()检查文件夹是否存在,但遇到该方法失败。我的命令是:
if (Directory.Exists(myFolder) == false)
{
MessageBox.Show(myFolder, "Invalid Log Folder path", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
if (Directory.Exists(myFolder) == true)
{
MessageBox.Show(myFolder, "Valid Folder path");
}
Run Code Online (Sandbox Code Playgroud)
当 myFolder 为 时,C:\Documents and Settings\UserID\My Documents它返回TRUE。
当 myFolder 是时C:\Documents and Settings\xxx\My Documents,它返回FALSE(假设xxx不存在)。
但是,当 myFolder 为 时,C:\\Documents and Settings\UserID\My Documents它也会返回TRUE.
旁边的双反斜杠C:必定使测试无效。然而,Directory.Exists()却没有认识到这一点。
使用更多\它仍然遇到同样的问题。
我只是无法理解这种失败是如何发生的。我正在使用 VS2008 和 .NET 3.5。
我正在尝试使用vector[].substr(),但我不知道这是否可能。有谁知道另一种方法可以做到这一点?我的目标是取出一个向量中的单词并将其与第一个元音分开。任何帮助表示赞赏。我的代码如下所示:
#include <iostream>
#include "derrick_math.h"
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
#include <algorithm>
using namespace std;
int main()
{
string phrase;
string ay = "ay";
vector<string> vec;
cout << "Please enter the word or phrase to translate: ";
getline(cin, phrase);
istringstream iss(phrase);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter(vec));
for (int i = 0; i < vec.size(); i++)
{
if (vec[i].substr(0, 1) == "a || e || i || o || u || A || E || I || O …Run Code Online (Sandbox Code Playgroud) 我认为我要做的很明显,但如果你不理解,请问.
if (listBox1.SelectedIndex == 1 && 2)
{
label1.Text = "Sometext";
}
Run Code Online (Sandbox Code Playgroud) c# ×5
c++ ×2
directory ×1
events ×1
exists ×1
listbox ×1
marshalling ×1
operators ×1
reflection ×1