有没有办法通过c#中的反射在数组属性中设置单个值?
我的属性定义如下:
double[] Thresholds { get; set; }
Run Code Online (Sandbox Code Playgroud)
对于"普通"属性,我使用此代码通过反射设置它:
PropertyInfo pi = myObject.GetType().GetProperty(nameOfPropertyToSet);
pi.SetValue(myObject, Convert.ChangeType(valueToSet, pi.PropertyType), null);
Run Code Online (Sandbox Code Playgroud)
我如何更改此代码以将数组属性中的值设置在任意位置?谢谢!
顺便说一句:我试图使用索引参数,但这似乎只适用于索引属性,而不是数组属性...
我注意到数组有SetValue方法,当你可以使用索引器时,它看起来有点不合适.SetValue有一些特殊用途吗?MSDN文章似乎没有说出SetValue的用途,只是如何使用它.就速度而言,使用哪种方法会更有效?
我使用代码创建了RadioGroup
var radios = new Ext.form.RadioGroup({
columns : 2,
items: [
{boxLabel: 'E-Mail', name: 'communication', inputValue: 1},
{boxLabel: 'Nagios', name: 'communication', inputValue: 2}
]
});
Run Code Online (Sandbox Code Playgroud)
我想检查某个事件上的一个单选按钮.怎么做?我试过用:
radios.setValue(true, false);
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
在尝试使用KVC访问ivars之后,我注意到私人和受保护的ivars没有保护.我把它放在ivar(私人或受保护的关键字)前面并不重要 - 当使用KVC方法"setValue"时,ivar总是公共的ivar.这是我的代码,其中所有七个ivars和属性都可以在类实例之外更改:
//************ interface file ***************//
@interface MyClass : NSObject {
@public
NSNumber *public_num;
@protected
NSNumber *protected_num;
@private
NSNumber *private_num;
NSNumber *private_property;
}
@property (retain) NSNumber *public_property;
@property (retain) NSNumber *private_property;
@end
//********* implementation file *********//
@interface MyClass(){
@private
NSNumber *very_private_num;
}
@property (retain) NSNumber *very_private_property;
@end
@implementation MyClass
@synthesize public_property, private_property, very_private_property;
@end
//****** main **********//
MyClass *myClass = [[MyClass alloc] init];
[myClass setValue:[NSNumber numberWithInt:1] forKey:@"public_num"];
[myClass setValue:[NSNumber numberWithInt:2] forKey:@"protected_num"];
[myClass setValue:[NSNumber numberWithInt:3] forKey:@"private_num"];
[myClass setValue:[NSNumber numberWithInt:4] forKey:@"public_property"]; …Run Code Online (Sandbox Code Playgroud) 背景.我想从一周的格式更改时间表(每行显示7天,yyww中没有日期只有一周(例如1225).在另一张表中,一列列出星期,另一列列出日期.
方法.我将这两张纸分成两个数组,打包第三个数组,我将其设置为第三个数据表.
问题.此行提供错误消息:"无法转换为."
sheet_IndataTabell.getRange(1,1,IndataTable.length+1,7).setValues(IndataTable);
Run Code Online (Sandbox Code Playgroud)
资源.您可以在此处查看文档以及下面的完整功能:
function UpdateTable() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet_Indata = ss.getSheetByName("Indata");
var sheet_IndataTabell = ss.getSheetByName("Indata_Tabell");
var sheet_Calendar = ss.getSheetByName("Kalender");
//Get the table into arrays
var Indata = sheet_Indata.getDataRange();
var CalendarTable = sheet_Calendar.getDataRange();
//Gets the values in the Indata to an Array
var NumberRows = Indata.getLastRow();
var NumberCols = Indata.getLastColumn();
//Browser.msgBox(NumberRows + " " + NumberCols);
var IndataArray = new Array(NumberRows,NumberCols);
var IndataArray = Indata.getValues();
//Create an Array to store the result
var IndataTable …Run Code Online (Sandbox Code Playgroud) 我在视图中有一个简单的表单,它有一个文本区域:
<textarea name="description" value="<?php echo set_value('description'); ?>"></textarea>
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我使用form_validation库验证了此文本区域
$this->form_validation->set_rules('description', 'Description', 'trim|required');
Run Code Online (Sandbox Code Playgroud)
验证工作正常,即如果文本区域为空,它会给我错误,但如果textarea-description被正确填充但是其他字段中存在一些错误,则它不会重新填充textarea
我究竟做错了什么 ?
我想包括对我的组合框的检查,以限制对某些值的"访问".我可以从列表中删除那些无法访问的项目,是的,但是我希望用户看到其他选项,即使他还不能选择它们.
问题:在changelistener中选择另一个值会导致IndexOutOfBoundsException,我不知道为什么.
这是一个SSCCE.它创建一个具有整数值的ComboBox,默认情况下会选择第一个.然后我试着保持它很容易:每次更改值都被视为"错误",我将选择更改回第一个元素.但是,IndexOutOfBounds:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
public class Tester extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ComboBox<Integer> box = new ComboBox<Integer>();
ObservableList<Integer> vals= FXCollections.observableArrayList(0,1,2,3);
box.setItems(vals);
box.getSelectionModel().select(0);
/*box.valueProperty().addListener((observable, oldValue, newValue) -> {
box.getSelectionModel().select(0);
});*/
/*box.getSelectionModel().selectedItemProperty().addListener((observable,oldValue,newValue)->{
System.out.println(oldValue+","+newValue);
box.getSelectionModel().select(0);
});*/
box.getSelectionModel().selectedIndexProperty().addListener((observable,oldValue,newValue)->{
System.out.println(oldValue+","+newValue);
box.getSelectionModel().select(0);
});
Scene scene = new Scene(new Group(box),500,500);
stage.setScene(scene);
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
我用valueProperty,selectedItemProperty和selectedIndexProperty测试了它,以及所有这些:
box.getSelectionModel().select(0);
box.getSelectionModel().selectFirst();
box.getSelectionModel().selectPrevious();
box.setValue(0);
if (oldValue.intValue() …Run Code Online (Sandbox Code Playgroud) 如何检查JSlider或JSpinner的值是通过图形界面而不是通过方法设置的setValue( int n)?
我正在开发一个类别模块,我正在使用CI验证规则.当验证规则失败时,我希望表单具有用户输入的相同值.我已经使用set_value函数在验证规则失败后加载表单中的值.但每次验证失败时,该值都将设置为空白.
这是我的代码.我在构造函数中设置了验证规则.
$this->form_validation->set_rules($this->validation_rules);
if ($this->input->post()) {
$db_array=array(
'CategoryName' => $this->input->post('CategoryName'),
'ParentID' => $this->input->post('ParentID'),
'Status' => $this->input->post('Status')
);
if($this->form_validation->run() != false){
if($id)
{
$result = $this->category_m->update($db_array,$id);
if(isset($result['error'])&& count($result['error']>0))
{
foreach($result['error'] as $error)
{
$this->session->set_flashdata('error',$error);
}
}
if($result['success'])
{
$action['class'] = 'success';
$action['msg'] = 'Data added successfully.';
$this->session->set_flashdata('success',$action);
}
else
{
$action['class'] = 'error';
$action['msg'] = 'Invalid values to the form!';
$this->session->set_flashdata('error',$action);
}
}
else
{
$result = $this->category_m->insert($db_array);
if(isset($result['error']) && count($result['error']>0))
{
foreach($result['error'] as $error)
{
$this->session->set_flashdata('error',$error);
}
}
if($result['success'])
{ …Run Code Online (Sandbox Code Playgroud) setvalue ×10
arrays ×2
c# ×2
codeigniter ×2
php ×2
validation ×2
combobox ×1
debugging ×1
extjs ×1
indexer ×1
integer ×1
ivars ×1
java ×1
javafx ×1
jslider ×1
jspinner ×1
objective-c ×1
private ×1
propertyinfo ×1
radio-group ×1
reflection ×1
swing ×1
textarea ×1
xdebug ×1