小编Oli*_*bes的帖子

while循环只显示1项

我正在使用while循环根据第一个组合框选择的值填充第二个组合.然而,正在发生的是,循环只在第二个组合框中显示1个项目,而不是大约20个.如果我在while循环上设置断点,我可以看到所有项目都在计算但只是没有出现在组合框中.

如果有人能指出我的基本新手错误,我将不胜感激.非常感谢

Private Sub cmbCustomer_SelectedIndexChanged(ByVal sender As System.Object, _
                                             ByVal e As System.EventArgs) _
    Handles cmbCustomer.SelectedIndexChanged

    sql = "SELECT * from Departments WHERE Customer = '" & cmbCustomer.Text & "'"

    Dim cmd As New OleDb.OleDbCommand

    cmd.CommandText = sql
    cmd.Connection = oledbCnn
    dr = cmd.ExecuteReader

    While dr.Read()
        If (dr.HasRows) Then
            cmbDept.Text = CStr((dr("Name"))) <--- 2nd combobox
        End If
    End While

    cmd.Dispose()
    dr.Close()
End Sub
Run Code Online (Sandbox Code Playgroud)

vb.net visual-studio-2010 visual-studio

0
推荐指数
1
解决办法
205
查看次数

如何画一个正方形

我在绘制以下方块时遇到麻烦:

* # # # # # 
* * # # # # 
* * * # # # 
* * * * # # 
* * * * * # 
* * * * * *
Run Code Online (Sandbox Code Playgroud)

这是我写的方法,但它不能正常工作:

public void Draw(int width){
    char asterisk = '*';
    char hash = '#';
    int counter = 0;
    for (int h = 0; h < 6; h++) { //height?
        for (int w = 0; w < width; w++) {
            if (h == counter)
                Console.Write …
Run Code Online (Sandbox Code Playgroud)

c# console-application

0
推荐指数
1
解决办法
5772
查看次数

使用StreamWriter/Reader内部循环的"using"语句

这是我的情况.

  1. 从文本文件中读取一行
  2. "处理"线
  3. 将"已处理"行写入新文本文件
  4. 循环到#1并重复直到EOF

我是这样做的:

using (StreamReader srReader = new StreamReader(strInputFile))
{
    // loop until EOF
    while ((strCurrentLine = srReader.ReadLine()) != null)
    {
        // "process" strCurrentLine...

        // write the "processed" strCurrentLine to a new text file
        using (StreamWriter sw = new StreamWriter(strFullOutputPathFileName, false))
        {
            // write strCurrentLine to the new file
            sw.WriteLine("stuff...");
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

我的经理告诉我,使用using像我在循环中的语句将极大地阻碍性能.原因是因为StreamWriter实例将在循环时创建多次.所以,如果我循环1000次,我会有1000个StreamWriter物体实例,这可能严重阻碍性能.

这是真的?另外,我的方法是否是实现此目的的最佳方法?

c# using streamwriter streamreader

0
推荐指数
1
解决办法
3528
查看次数

如何将字符串添加到数组VB.NET?

我有一个像这样的数组

Dim array() As String = {}
Run Code Online (Sandbox Code Playgroud)

和以下代码

For i = 0 To membertable.Rows.Count - 1
    If InStr(membertable.Rows(i)("name"), txtSearch.Text, CompareMethod.Text) - 1 _
       <> -1 And txtSearch.Text.Length >= 3 Then

        found = True

        'add the item that matches the criteria to the array here.

    End If
Next i
Run Code Online (Sandbox Code Playgroud)

因此,代码循环遍历访问表的行,并且每次在"name"列下找到与我想要将该项添加到数组的条件匹配的值.数据库项始终是一个字符串.

vb.net arrays

0
推荐指数
1
解决办法
1万
查看次数

为什么使用Take()似乎可以加速我的EF查询?

我有一个复杂的LINQ到EF查询超时.在调试时我添加了一个Take(200)并没有超时.奇怪的是它只返回了127行!

换句话说,即使它没有改变结果Take,也要添加加速查询.有什么可能导致这种情况?

为了澄清,生成的SQL完全相同,只是将Take(200)EF添加TOP 200SELECT.

c# linq entity-framework sql-server-2008

0
推荐指数
1
解决办法
72
查看次数

棘手的LINQ查询:返回包含最大长度子字符串的子字符串的字符串

我有2个数据表,每个数据表有300,000行(从带有OLEDB的2个excel工作表导入的数据表).

第一个数据表是'dtTosearch',第二个数据表是'sourceDt'.

这是2个表的示例:

数据表示例

我需要为'untagged'列(sourceDt)中的每一行找到'token'列(dtTosearch)中每一行的匹配项.匹配条件是:

  1. 'site'value ='site'值
  2. 'cat'值='类别'值
  3. 未标记的值必须包含标记值
  4. 如果上述所有条件都存在超过一个匹配,则查询必须返回 具有最大长度的令牌匹配. (这是我没有弄清楚如何使用linq执行的棘手部分)
  5. 这个任务必须在最短的处理时间内运行 - 因为它的要求,因为它更专业,因为我与朋友 - 同事-JAVA热心的开发人员打赌,.NET将运行得更快(如同白天( - :)

香港专业教育学院添加了代码的相关部分,它工作正常,但不是我想要的方式,我想改善处理时间,看看linqQuery()函数中的foreach循环 - 如果你帮我替换那个循环我会很感激通过将我的查询扩展到条件号4,循环操作条件4,因为linq查询的结果按'token'长度按降序排序,因此它将退出并返回具有最大行长度的结果.

Private Sub startScanning()
    Dim siteNum As Double
    Dim categoryNum As Double
    Dim stringToSearchin As String

    For i = 0 To sourceDt.Rows.Count - 1
        siteNum = sourceDt.Rows(i).Item(0)
        categoryNum = sourceDt.Rows(i).Item(1)
        stringToSearchin = sourceDt.Rows(i).Item(3)
       Debug.WriteLine( linqQuery(siteNum, categoryNum, stringToSearchin) & " " &
           stringToSearchin)
    Next
End Sub

Private Function linqQuery(ByVal sitenum As Double, ByVal cat As Double,
                           ByVal …
Run Code Online (Sandbox Code Playgroud)

linq vb.net

0
推荐指数
1
解决办法
641
查看次数

将元素添加到XML C#

我试图将以下元素添加到C#上的.XML中:

<Launch.Addon>
        <Name>IvAp</Name>
        <Disabled>False</Disabled>
        <Path>C:\Program Files (x86)</Path>
        <Commandline></Commandline>
</Launch.Addon>
Run Code Online (Sandbox Code Playgroud)

使用以下代码:

XDocument xd1 = new XDocument();
xd1 = XDocument.Load(pathToAData + "\\dll.xml");

XElement root = new XElement("Launch Addon");
root.Add(new XElement("Name", "IvAp"));
root.Add(new XElement("Disable", "False"));
root.Add(new XElement("Path", "C:\\Program Files (x86)\\IVAO\\IvAp v2\\ivap_fsx_bootstrap.dll"));
root.Add(new XElement("Commandline"));
xd1.Element("Launch Addon").Add(root);
xd1.Save(pathToAData + "\\dll.xml");
Run Code Online (Sandbox Code Playgroud)

但它在try {} catch {}块中引发错误,如果你能帮助我,我将非常感激

这是错误:

.System.Xml.XmlException: El carácter ' ', con valor hexadecimal 0x20, no puede incluirse en un nombre.
Run Code Online (Sandbox Code Playgroud)

c# xml

0
推荐指数
1
解决办法
65
查看次数

删除if语句并使其更具可读性

嗨想知道是否有更好的方法来删除其他if语句并使用策略模式.有任何想法吗?

public async Task<TResponse> HandleResponseAsync<TRequest, TResponse>(TRequest item)
    where TResponse : class, new()
{
    TResponse result = default(TResponse);
    Type currentResponseType = typeof(TResponse);   

    if (currentResponseType == typeof(MyResponseA))
    {
        result = //dosomething
    }
    else if (currentResponseType == typeof(MyResponseB))
    { 
        result = //dosomething
    }
    else if (currentResponseType == typeof(MyResponseC))
    {
        result = //dosomething
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

c# design-patterns

0
推荐指数
2
解决办法
241
查看次数

用递归替换while循环

快速简单的问题:

这是:

private static void SetupConnection()
{
    try
    {
        TcpClient client = new TcpClient(myServer, myPort);
        //Do whatever...
    }
    catch (SocketException)
    {
        //Server is closed. Retry in 10 minutes.
        Thread.Sleep(600000);
        SetupConnection();
    }
Run Code Online (Sandbox Code Playgroud)

一个可行的替代方案:

private static void SetupConnection()
{
    while (true)
    {
        try
        {
            TcpClient client = new TcpClient(myServer, myPort);
            //Do whatever...
            break;
        }
        catch (SocketException)
        {
            //Server is closed. Retry in 10 minutes.
            Thread.Sleep(600000);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然第二个看起来"更干净",但如果第一个也是可以接受的话,我仍然很好奇 - 如果不是,那么为什么不呢?

c# sockets recursion tcpclient while-loop

0
推荐指数
1
解决办法
95
查看次数

如何在所有玩家之间循环,然后重新开始?

我有一个名为NewTurn()的函数,用于设置下一个播放器。目前,我正在使用if-else更改播放器:

if (p == players[0]) 
{ 
    p = players[1]; 
}
else 
{ 
    p = players[0]; 
}
Run Code Online (Sandbox Code Playgroud)

如果我需要3个玩家怎么办?有没有更简单的方法可以用%编写此代码。

c# unity-game-engine

0
推荐指数
1
解决办法
29
查看次数

在通用接口上使用“工作单元”时,为什么会看到错误?

我有一个名为:的通用类存储库GenericRepository,还有一个名为:的通用接口,IGenericRepository并且Product是数据库中的表之一。

当我通过这种方式将“工作单元”与此通用存储库一起使用时:

public class UnitOfWork: IDisposable
{
    GroceryStore_DBEntities db = new GroceryStore_DBEntities();
    private IGenericRepository<Product> _genericRepository;
    public IGenericRepository<Product> GenericRepository
    {
        get
        {
            if (_genericRepository == null)
            {
                _genericRepository = new  GenericRepository<Product>(db);
            }
            return _genericRepository;
        }
    }}
Run Code Online (Sandbox Code Playgroud)

我遇到2个错误,您可以在下面看到:

  1. 错误CS0311类型'GroceryStore.DataLayer.Context.Product'不能用作通用类型或方法'GenericRepository <TEntity>'中的类型参数'TEntity'。没有从'GroceryStore.DataLayer.Context.Product'到'GroceryStore.DataLayer.Repositories.IGenericRepository <GroceryStore.DataLayer.Context.Product>'的隐式引用转换。

  2. 无法将类型'GroceryStore.DataLayer.Services.GenericRepository <GroceryStore.DataLayer.Context.Product>'隐式转换为'GroceryStore.DataLayer.Repositories.IGenericRepository <GroceryStore.DataLayer.Context.Product>'。存在显式转换(您是否缺少演员表?)

你能告诉我我哪里出问题了吗?为什么?以及我该如何解决?


我有以下声明:

public interface IGenericRepository<TEntity>
    where TEntity: class
{ }

public class GenericRepository<TEntity>
    where TEntity: class, IGenericRepository<TEntity>
{ }
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
214
查看次数

将谓词作为参数传递给C#

我最近从一家公司进行了评估,该公司有一个案例,他们希望将谓词设置为方法的输入参数。对此我几乎没有经验,我一直在自己研究。代码如下:

using System;

public interface IBird
{
    Egg Lay();
}

public class Chicken : IBird
{
    public Chicken()
    {
    }

    public void EggLay()
    {
    }

    public Egg Lay()
    {
        return new Egg();
    }
}

public class Egg
{
    public Egg(Func<IBird> createBird)
    {
        throw new NotImplementedException("Waiting to be implemented.");
    }

    public IBird Hatch()
    {
        throw new NotImplementedException("Waiting to be implemented.");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
//      var chicken1 = new Chicken();
//      var egg …
Run Code Online (Sandbox Code Playgroud)

c# predicate func

-1
推荐指数
1
解决办法
683
查看次数

如何在unity3d中使用c#获取模型描述列表?

如何为3D角色模型定义模型描述列表(如颈部,脊柱,下唇等)?我试着编写一个脚本:

using UnityEngine;
using System.Collections;
public class Arrays : MonoBehaviour
{
    public GameObject[] players;

    void Start()
    {
        players = GameObject.FindGameObjectsWithTag("avatar_5");  
        for (int i = 0; i < players.Length; i++)
        {
            Debug.Log(players[i].name);
            Debug.Log("Player Number " + i + " is named " + players[i].name);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但结果我收到此错误: UnityException:标签:avatar_5未定义

c# unity-game-engine

-4
推荐指数
1
解决办法
469
查看次数