VS2010中VC程序在一个class中调用另一个class中的函数

zhouleyiman 2012-10-10 02:35:49
大家好,小弟目前在修改VC程序(其实是VS2010中VC#程序),在一个VC的class中调用另一个class中的函数,发觉无法调用。我写的函数如下(是一个读初始文件的函数)
------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; //可以调用 kernel32 API了

namespace CallDll
{
class ReadFile
{
public string m_file_path;
[DllImport("kernel32 ")]
private static extern long WritePrivateProfileString(string Section, string Key, string Val, string FilePath);
[DllImport("kernel32 ")]
private static extern int GetPrivateProfileString(string Section, string Key, string Def, StringBuilder RetVal, int Size, string FilePath);

public ReadFile(string IniFilePath)
{
m_file_path = IniFilePath;
}

public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, m_file_path);
}

public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, " ", temp, 255, m_file_path);
return temp.ToString();
}

//读取函数,也就是要调用的函数
public string readinifile(string mPathfle, string mSection, string mKey)
{
ReadFile ini = new ReadFile(mPathfle);
return ini.IniReadValue(mSection, mKey);
}

internal static string readinifile()
{
throw new NotImplementedException();
}
}
}
-----------------------------------------------
目前是,我要在另一个class( public partial class MainForm : Form中)中调用函数readinifile
求大家帮忙了
以前有使用VB编程,感觉差异太大,
...全文
380 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouleyiman 2012-12-09
  • 打赏
  • 举报
回复
都谢谢了,只是回复得太慢了
iyomumx 2012-10-10
  • 打赏
  • 举报
回复
public string readinifile(string mPathfle, string mSection, string mKey)
可以改为
public static string readinifile(string mPathfle, string mSection, string mKey)
因为没使用到任何实例成员,并且函数体内重新构造了一个类……这种即用即弃的调用效率低而且不能发挥面向对象的优势。
现在你可以通过CallDll.ReadFile.readinifile(... 来调用该方法了
不过更好的方法是通过在MainForm中定义一个 ReadFile 类型的成员变量,并在适当的时机初始化,在需要读写ini文件时直接使用IniReadValue/IniWriteValue来读写
真相重于对错 2012-10-10
  • 打赏
  • 举报
回复
c# 几乎无法用c++的class

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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