嗨,我想问,因为我不确定它是否是使用异常的私人:
public int Method(int a, int b) {
if(a<b) throw new ArgumentException("the first argument cannot be less than the second");
//do stuff...
}
Run Code Online (Sandbox Code Playgroud)
if语句之后可以抛出Exception吗?或者我应该总是使用try-catch与异常一起使用?
这个案子从早上起就困扰着我.如果抛出异常,在类构造函数中调用垃圾收集器是一种好习惯吗?我有这样的事情:
public MyClass(/* some arguments */)
{
try
{
//do stuff...
} catch(Exception e) {
//do stuff, save logfile
GC.SuppressFinalize(this);
}
}
Run Code Online (Sandbox Code Playgroud)
我这样做的原因是,如果它抛出异常(通常是NullreferenceException)我想在文本文件中记录它,我不再需要/想要这个对象了.但这是好习惯吗?如果没有怎么做呢?
我有基于ArrayList的JComboBox:
private ArrayList<String> klienci = new ArrayList<String>();
private JComboBox klienciLista;
Run Code Online (Sandbox Code Playgroud)
我在构造函数中添加它:
klienciLista = new JComboBox(klienci.toArray());
klienciLista.setPrototypeDisplayValue("#############################");
panel.add(klienciLista); //JPanel panel
Run Code Online (Sandbox Code Playgroud)
在开始时列表为空.客户端通过线程中的套接字新的ArrayList获取:
public void run() {
try {
host = InetAddress.getLocalHost().getHostName();
socket = new Socket(host, SERVER_PORT);
input = new ObjectInputStream(socket.getInputStream());
output = new ObjectOutputStream(socket.getOutputStream());
output.writeObject(nazwa);
} catch (IOException e) {
System.out.println(e);
JOptionPane.showMessageDialog(null,
"Polaczenie sieciowe dla klienta nie moze byc utworzone");
setVisible(false);
dispose(); // zwolnienie zasobów graficznych
// okno graficzne nie zostanie utworzone
return;
}
try {
while (true) {
container = new …
Run Code Online (Sandbox Code Playgroud) 向您展示我想要实现的目标的最佳方式是展示图片:
我尝试将嵌套表放置在行的每一侧。我寻找解决方案但没有发现任何有趣的东西。当我玩“位置:绝对;”时 我造成的伤害多于好的结果。可以像图中那样做吗?
编辑:这不是我的项目,我对设计没有任何影响。它基于表格,我必须处理它:)
嗨,我正在开发CRM 2011插件,我有反映类型的问题.我已经生成了实体类,我知道属性存在于类型中,但是当我尝试获取其值时,我得到了关于未找到方法的异常.最愚蠢的部分是它在我的机器上完美运行但不适用于客户端.
这是我的代码(我需要从实体获取所有OptionSets并对它们执行操作):
public override void MyExecute()
{
var fse = TargetEntity.ToEntity<Equipment>();
Type equiptmentType = fse.GetType();
TracingService.Trace("FSE object type: {0}", equiptmentType.FullName);
IEnumerable<PropertyInfo> optionSetsProperties = equiptmentType.GetProperties()
.Where(x => x.PropertyType == typeof(OptionSetValue)).ToList(); //I'm getting this property from the object type so it must exist.
foreach (var optionSetsProperty in optionSetsProperties)
{
TracingService.Trace("Resolving ResourceGroup on property: {0}", optionSetsProperty.Name);
ResolveResourceGroupBySkill(optionSetsProperty, fse);
TracingService.Trace("Resoloving ResourceGroup finished");
}
}
private void ResolveResourceGroupBySkill(PropertyInfo optionSetsProperty, Equipment fse)
{
try
{
TracingService.Trace("Trying to get value of: {0}", optionSetsProperty.Name);
OptionSetValue skillLevel = …
Run Code Online (Sandbox Code Playgroud) 我正在使用Windows 8开发人员预览版.我希望开发一个读取XML文件的应用程序.可能吗?当我试图使用XDocument.Load(string uri)
它时,抛出一个访问被拒绝的异常并且没有这样的类FileStream
.我知道metro风格的应用程序基于Silverlight,它无法访问文件系统,但它实际上是一个桌面应用程序,所以我不能读取文件?有没有解决方法?我对银光很新,所以也许我不知道怎么回事?
嗨,我在剃刀视图引擎中的html帮助扩展方法有问题.如果我要打印任何节点,我想渲染SideMenu:
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
</head>
@using MyNamespace.Helpers
<body>
<div id = "page" style = "width: 90%; margin: 0 auto" class="pageSection">
<div id="logo" class="pageSection">aaaa</div>
<div id = "LeftMenuContainer">
@{ if (ViewBag.Menu.LeftMenu.Count > 0)
{
Html.RenderSideMenu((ICollection<MyNamespace.MenuNode>)(ViewBag.Menu.LeftMenu));
}
}
</div>
<div id="content" class="pageSection">@RenderBody()</div>
<div id="footer" class="pageSection"></div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的方法:
public static MvcHtmlString RenderSideMenu(this HtmlHelper helper, ICollection<MenuNode> nodes)
{
string result = "<h3>Menu</h3>";
result += "<ul class=\"SideMenu\">";
foreach (var node in nodes)
result …
Run Code Online (Sandbox Code Playgroud) 最近我用F#开始冒险.我正在尝试创建我将在C#项目中使用的F#库.
现在我面临的问题是我有两种类型的定义(如我所愿)可以自己使用(我正在尝试为c#用法创建流畅的API).
我想如何在c#中使用它(简化示例).
Shopping shopping = new Shopping();
Stuff[] stuff = shopping.GoTo("Wallmart").Buy(new [] { "Candies", "Ice cream", "Milk" }).GoTo("Drug store").Buy(new [] { "Anvil" }).GetStuff();
Run Code Online (Sandbox Code Playgroud)
现在我有两种类型(在separted文件中):
type ShopResult(someContext: ShoppingContext) =
//some logic
member this.GoTo shopName = new ToDoResult(someContext)
type ToDoResult(someContext: ShoppingContext) =
//some logic
member this.Buy what = new ShopResult(someContext)
Run Code Online (Sandbox Code Playgroud)
现在文件顺序导致编译错误,我想知道我的案例是否有任何解决方案?还是让我放弃流畅的api想法?
我想定义一个类继承层次结构,因此参数化方法的参数类型介于我的第一个基类和当前基类之间:
class A {
var x;
}
class B(parent:A = null) extends A {
var y;
protected def method[/* T where, T is subclass of A but super class of type.this */](param:T):Unit = {}
}
class C(parent:B = null) extends B {
var z
protected override def method[/* T where, T is subclass of A but super class of type.this */](param:T):Unit = {}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?是否有任何理由我不应该尝试实现这一点(架构原因或任何其他原因)?
从基类到派生类型转换我遇到了问题(我正在施法,因为我确定该对象是那种确切的类型).这是我的代码(简化):
class MyCollection
{
public:
Element* Get(int i) {
return elements[i];
}
void Add(Element* element) {
//finding i
elements[i] = element;
}
private:
Element* elements[100];
}
class Element {
public:
int ID;
}
class SpecialElement : public Element
{
public:
SpecialElement(char* name) {
this-> Name = name;
}
char* GetName() { return Name; }
private:
char* Name;
}
Run Code Online (Sandbox Code Playgroud)
现在,当我加入SpecialElement的MyCollection的对象,当我把断点添加的时刻,投我的Add方法的参数在立即窗口和调用GetName方法还给我名字的对象,但是当我做这样的事情:
void main() {
MyCollection coll = new MyCollection();
coll.Add(new SpecialElement("MyName"));
SpecialElement* foundElement = (SpecialElement*)coll->Get(0);
foundElement->GetName(); //Error
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么会这样?是不是ObjectElement类型的创建对象?