我以通常的方式为一个类创建了Excel对象:
using Excel=Microsoft.Office.Interop.Excel;
但是,我将在这个项目中开发很多类,其中大部分都需要使用Excel对象,而且似乎我必须将它放在每个文件的标题中.有没有办法让我以某种方式using在项目级别输入此语句,这样我就不必在每个文件中添加它,并且如果由于某种原因我决定开始使用不同的Excel库,那么更容易更改?
我创建类库,创建Web应用程序项目,然后添加引用类库的Web应用程序项目按此处的说明,而是"利用"它不能添加.(customer.cs的命名空间也是"CDemoLib")(当我添加"使用CDemoLib"时,我得到'无法解析符号CDemoLib'
customer.cs的代码是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CDemoLib
{
public class Customer
{
private int _age;
private string _name;
public Customer()
{
Age = 0;
Name = "Not available";
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在网上找到了以下代码:
using (SqlConnection con = new SqlConnection(connectionString))
{
//
// Open the SqlConnection.
//
con.Open();
//
// The following code uses an SqlCommand based on the SqlConnection.
//
using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("{0} {1} {2}",
reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
}
}
}
Run Code Online (Sandbox Code Playgroud)
谁能解释一下为什么使用 (SqlCommand ..) 不以分号结尾。我的第二个问题通常是在使用之后我们必须有 { } 来指示使用变量的范围为什么在这种情况下它丢失了?以及如何、何时处理命令对象?
在这种方法的情况下:
public void Delete(int id)
{
using (var connection = GetOpenConnection())
{
connection.Execute($"DELETE FROM MyTable WHERE Id = {id}");
}
}
Run Code Online (Sandbox Code Playgroud)
要不就:
GetOpenConnection().Execute($"DELETE FROM MyTable WHERE Id = {id}");
Run Code Online (Sandbox Code Playgroud)
我想知道第二个是否是减轻维护和简化的最佳选择.
我有一个类,Tracker我在其中声明了一个别名
从Tracker.h:
class Tracker {
...
using ArgsMap = std::unordered_map<std::string, std::string>;
std::shared_ptr<ArgsMap> getArgsMapForTask(std::string task);
...
}
Run Code Online (Sandbox Code Playgroud)
在 .cpp 文件中,我定义了该函数:
#include Tracker.h
...
// ArgsMap here gives error: Use of undeclared identifier 'ArgsMap'
std::shared_ptr<ArgsMap> Tracker::getArgsMapForTask(std::string taskName)
{
ArgsMap a; // this gives no error, compiler recognizes ArgsMap
}
Run Code Online (Sandbox Code Playgroud)
如何ArgsMap在函数签名中使用?
我已经用“using”关键字声明了函数指针的别名,但我不知道如何使用该别名。
在类UpdateState的函数中Person,我想替换m_state与当前状态和要转换到下一个状态相对应的函数的返回值。但是,以下错误发生在第 38 行Person.cpp,我不知道如何纠正该行。我认为我错误地使用了别名数组。
error C2064: term does not evaluate to a function taking 1 arguments
Run Code Online (Sandbox Code Playgroud)
人.h
error C2064: term does not evaluate to a function taking 1 arguments
Run Code Online (Sandbox Code Playgroud)
人物.cpp
#pragma once
enum STATE
{
A,
B,
C,
D,
E,
STATE_NUM,
};
class Person
{
public:
Person();
~Person();
void UpdateState();
STATE IsInStateA(char nextState);
STATE IsInStateB(char nextState);
STATE IsInStateC(char nextState);
STATE IsInStateD(char nextState);
STATE IsInStateE(char nextState);
private:
STATE m_state;
};
Run Code Online (Sandbox Code Playgroud) 用这种方式编写C++中的各种程序而不使用范围解析运算符:
#include <iostream>
#include <string>
int main()
{
std::string name = "My Name";
std::cout << name << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我也看到使用"使用"关键字:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "My Name";
cout << name << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
出于效率原因哪一个更好?
通过阅读这篇文章,我有点困惑:
他们演示了调用2个一次性对象,结束了更多的内存使用,而没有usings 的方法,执行相同的指令,使用较低的内存.
你能解释一下为什么在这个例子中使用会增加内存消耗吗?
我想了解在某些情况下是否应该避免使用.
我认为dispose释放记忆是一个好主意,但看起来我错了.
我的代码没有编译给出错误:
找不到类型或命名空间名称"Int32"(您是否缺少using指令或程序集引用?)
为什么会发生这种情况/我该如何解决?
这是有问题的代码:
///***********************************************************
///Author Name: Harkamal Singh
///Creation Date: 17th Nov, 2008
///File Name: CountryPrp.cs Component Used:
///Called From: Business Logic Layer
///Description: Class File For Booking Functionality
///Tables Accessed:
///Program specs:
///UTP doc:
///Tested By:
///***********************************************************************
///Modification History:
///Change No. Changed By Date Version Raised By/SRS No Description
///***********************************************************************using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Summary description …Run Code Online (Sandbox Code Playgroud) 何时以及为何在Asp.net(C#)中使用USING语句添加大括号?
例如
using (Log L = new Log())
{
...
}
Run Code Online (Sandbox Code Playgroud)
为什么以及什么时候放花括号?好处?使用和使用我们用来在源代码中包含命名空间的区别是什么
using system.net
Run Code Online (Sandbox Code Playgroud)
还有一件事,最后一件事,因为它说USING语句自动实现TRY CATCH然后为什么总是编码器把try catch放在里面使用?例如
using (SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom))
{
try
{
//sqlCom.ExecuteNonQuery();
//sqlDA.Fill(ds,"Login");
DataTable dt = new DataTable("DT_CR");
sqlDA.Fill(dt);
ds.Tables[0].Merge(dt);
return ds;
}
catch (SqlException se)
{
Response.Write(se.Message);
return null;
}
Run Code Online (Sandbox Code Playgroud) 什么是
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
【“使用系统”】是什么意思?为什么没有这些行就无法启动代码?
我刚刚学习 C++,我不太明白将using std::stringVS放在#include <string>主文件顶部之间的区别。
我似乎能够定义字符串而无需#include <string>这里:
#include <iostream>
using std::cout; using std::cin;
using std::endl;
using std::string;
int main()
{
string s = "hi";
cout << s;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这似乎运行没有问题,那么为什么我会出现这个问题呢#include <string>?
我无法让我的代码工作.我需要访问一个只有一行文本的网站,并在cmd提示符下显示它.该网站受密码保护,所以我使用webClient存储cookie,并试图找出如何给它的用户名和密码."使用"声明不起作用,有人知道我可以尝试什么吗?
using System;
using System.Net;
namespace Temperature
{
public class CookieAwareWebClient : WebClient
{
public CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = m_container;
}
return request;
}
using (var client = new CookieAwareWebClient());
var values = new NameValueCollection
{
{"username", "admin"},
{"password","secret"},
};
client.UpLoadValues("http://10.10.1.52:8001/get?OID4.3.2.1=", values);
string tempString = client.DownloadString("http://10.10.1.52:8001/get?OID4.3.2.1=");
Stream response = myClient.OpenRead("http://10.10.1.52:8001/get?OID4.3.2.1=");
Console.WriteLine(tempString);
}
}
Run Code Online (Sandbox Code Playgroud)