我正在编写一本帮助我学习C#的书,其中一个项目就像是在小学电脑课程中教授的旧游戏之一.此特定示例使用for循环定义房间或区域有多少出口(外门).
这是通过外门移动的一个例子.当我回到门口时,使用"MoveToANewLocation()"方法,"currentLocation"失去了它的价值.for循环随后将值设置为负值,从而导致错误.
private void MoveToANewLocation(Location newLocation)
{
currentLocation = newLocation;
exits.Items.Clear();
for (int i = 0; i < currentLocation.Exits.Length; i++)
{
exits.Items.Add(currentLocation.Exits[i].Name);
}
exits.SelectedIndex = 0;
description.Text = currentLocation.Description;
if (currentLocation is IHasExteriorDoor)
{
goThroughTheDoor.Visible = true;
}
else
{
goThroughTheDoor.Visible = false;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个与上面完全相同的参考示例,它有效.当按钮"goThroughTheDoor"调用"MoveToANewLocation()"方法时,我很难理解为什么currentLocation会丢失它的值.
抱歉,如果不清楚,我仍然是现代编程的新手
我们有一个任务是将一个引用另一个类的类从C#传递给C++.C#类看起来像这样:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public class ImageInfo
{
public uint Width;
public uint Height;
public uint Stride;
}
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal class FrameInfo
{
public int FrameNumber;
public double TimeStamp;
public ImageInfo Image; // This is the problem
}
Run Code Online (Sandbox Code Playgroud)
C++ struct看起来像这样(pack也是1):
struct FrameInfo
{
public:
unsigned int frameNumber;
double timeStamp;
ImageInfo *image;
};
struct ImageInfo
{
public:
unsigned int m_width;
unsigned int m_height;
unsigned int m_stride;
};
Run Code Online (Sandbox Code Playgroud)
我们以这样的方式将C#类传递给C++:
[DllImport("ourLib.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern int OnFrame(int pId, …Run Code Online (Sandbox Code Playgroud) 我目前正在使用此代码:
List<int> list = new List<int>();
list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
color0 = list[1];
color1 = list[2];
color2 = list[3];
color3 = list[4];
Run Code Online (Sandbox Code Playgroud)
有没有一种可能的方法,此列表可以在1个元素中包含2个参数?我的意思是:
List<int,int> list = new List<int,int>();
list.Add(0,3);
list.Add(1,8);
color0=list[1][2]; //output 3
color1=list[1][1]; //output 0
color2=list[2][2]; //output 8
color3=list[2][1]; //output 1
Run Code Online (Sandbox Code Playgroud)
我有可能实现类似的目标吗?
为什么只能在C#中的单个语句中声明和初始化var?
我的意思是为什么我们不能使用:
var x;
x = 100;
Run Code Online (Sandbox Code Playgroud)
由于它是一个隐式类型局部变量"var",并且编译器采用了变量赋值运算符右侧的类型,为什么它只应在单个语句中声明和初始化呢?
我有奇怪的错误,寻找解决方案特别困难,因为它包含特殊字符。
我有一个类库,我将其添加为对 C# 项目的引用。它有一个函数(这是由 VS 根据元数据自动创建的):
public static void StartReadingDAQ(int dATALength, out double[] dAQData, out double samplingRate, out string errorMessage);
Run Code Online (Sandbox Code Playgroud)
所以声明看起来很好(注意第二个参数)。
然后当尝试调用该函数时:
double[] xxx = new double[_DATALength];
StartReadingDAQ(_DATALength, out xxx, out _SamplingRate, out _ErrorMessage);
Run Code Online (Sandbox Code Playgroud)
我收到编译错误:
Error CS1503 Argument 2: cannot convert from 'out double[]' to 'out double[*]'
Run Code Online (Sandbox Code Playgroud)
它究竟意味着什么?这是我第一次看到像double[*].
我想获得的关键SelectedItem的ComboBox,但不弄清楚如何让我做的是代码,
void CboBoxSortingDatagridview(ComboBox sender)
{
foreach (var v in DictionaryCellValueNeeded)
{
if (!DictionaryGeneralUsers.ContainsKey(v.Key) && v.Value.RoleId == Convert.ToInt32(((ComboBox)sender).SelectedItem)) // here getting value {1,Admin} i want key value which is 1 but how?
{
DictionaryGeneralUsers.Add(v.Key, (GeneralUser)v.Value);
}
}
dataGridViewMain.DataSource = DictionaryGeneralUsers.Values;
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式绑定了组合框,
cboRolesList.DataSource = new BindingSource(dictionaryRole, null);
cboRolesList.DisplayMember = "Value";
cboRolesList.ValueMember = "Key";
Run Code Online (Sandbox Code Playgroud) 我们注意到一些我们无法解释的奇怪事 在课堂上我们有这两个功能:
Public Overloads Shared Function ToList(ByVal input As Object, _
Optional ByVal StringSeparator As String = ";", _
Optional ByVal CharacterCasing As String = "", _
Optional ByVal StartRow As Integer = 0, _
Optional ByVal EndRow As Integer = -1) As String
...
End Sub
Public Overloads Shared Function ToList(ByVal Input As Object, _
Optional ByVal SplitStringSeparator As String = ",", _
Optional ByVal JoinStringSeparator As String = ",", _
Optional ByVal PreFixStr As String = "", _ …Run Code Online (Sandbox Code Playgroud) 在C++中我知道当一个打算从基类继承时,通常应该使用虚拟析构函数.但是,使用C#我不知道该怎么做.请考虑以下代码:
public abstract class Character
{
private string characterName;
public int health;
Character()
{
}
~Character(){
}
public virtual void SetCharacterName( string tempName )
{
characterName = tempName;
}
public virtual string GetCharacterName( )
{
return characterName;
}
}
Run Code Online (Sandbox Code Playgroud)
(注意:我听说Unity的Unity3Ds实现与标准略有不同.也许忽略一些小的格式错误,代码似乎功能......)
我的第一直觉是将~Character()析构函数设为虚拟,将其定义为:
virtual ~Character(){
}
Run Code Online (Sandbox Code Playgroud)
但这样做会导致IDE返回错误.
在C#中,对于希望继承的抽象类,使用虚拟析构函数是必要的还是被认为是标准的?还是有其他方法用C#制作虚拟析构函数?
我在nest中创建一个查询
var searchResult = ( (ElasticClient)_Db ).Search<PackageRecord, PackageRecordSearchHit> ( s => s
.Index ( Db_deals_IndexName )
.Type ( Db_Package_TypeName )
.From ( request.Page * _DefaultPageSize )
.Size ( _DefaultPageSize )
.Query ( q => q
.QueryString ( qs =>qs
.OnFields ( f =>f.TenantId )
.Query ( user.Tenant.Id.ToString () ) )
&&
q.Nested ( n => n
.Path ( f => f.List_BorrowerSet[ 0 ] )
.Query ( qm => qm.QueryString ( qs => qs
.OnFields (
f => f.List_BorrowerSet.First ().PrimaryBorrower.ContactDetails.Name_First,
f …Run Code Online (Sandbox Code Playgroud) 一位同事给了我一些我必须在.NET应用程序中使用的C#类.
有一个我从未见过的拼写错误,我在互联网上找不到任何解释......
这是代码:
public void GoTo<TView>() where TView : Form, new()
{
var view = Activator.CreateInstance<TView>();
//si on vient de creer une startup view alors on affiche l'ancienne
//la reference a la nouvelle sera detruite en sortant de la fonction GoTo
if (view is StartupForm)
{
ShowView(_StartupForm);
}
else ShowView(view);
}
Run Code Online (Sandbox Code Playgroud)
new()在方法声明的最后是什么关键字?
c# ×9
syntax ×2
arguments ×1
arraylist ×1
c++ ×1
combobox ×1
for-loop ×1
inheritance ×1
int ×1
keyvaluepair ×1
keyword ×1
marshalling ×1
nest ×1
new-operator ×1
null ×1
pointers ×1
vb.net ×1