You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

用户可编辑数据库存在重复ID:如何生成唯一ID?(WinForms场景)

Fixing Duplicate IDs in Your material_data Collection for Windows Forms TreeView

Got it, let's work through this ID duplication problem you're facing with your List<material_data> "database" and TreeView control. Below are practical, tailored approaches to generate unique IDs that fit your Windows Forms scenario:

1. Auto-Increment from the Highest Existing ID

This is the simplest approach if you want integer IDs and don't mind gaps (e.g., if you delete an item, the next ID won't reuse the old number—though that's usually fine for uniqueness).

When adding a new material_data entry:

// Assume your collection is named materialDatabase
int nextId = materialDatabase.Any() ? materialDatabase.Max(m => m.ID) + 1 : 1;

var newMaterial = new material_data
{
    ID = nextId,
    Description = "Your New Material",
    Data = new double[] { 0.5, 1.2, 3.0 }
};

materialDatabase.Add(newMaterial);

Pros: No extra dependencies, easy to implement, works with your existing integer ID setup.
Cons: Doesn't reuse IDs from deleted items (but uniqueness is still guaranteed).

2. Use GUIDs for Global Uniqueness

If you don't need integer IDs and want bulletproof uniqueness (even across different instances of your app), switch the ID property to a Guid type:

First update your class:

public class material_data 
{ 
    public Guid ID { get; set; } 
    public string Description { get; set; } 
    public double[] Data { get; set; } 
}

Then generate a new ID when creating entries:

var newMaterial = new material_data
{
    ID = Guid.NewGuid(), // Generates a unique GUID every time
    Description = "Your New Material",
    Data = new double[] { 0.5, 1.2, 3.0 }
};

Pros: 100% unique across systems, no need to check existing data.
Cons: GUIDs are non-intuitive strings (not user-friendly if you display IDs in the TreeView), requires adjusting any code that relied on integer IDs.

3. Thread-Safe Counter (For Multi-Threaded Apps)

If your app might add material_data entries from multiple threads, use a thread-safe counter to avoid race conditions that could cause duplicate IDs:

First initialize a static counter based on your existing data:

private static int _nextMaterialId;

// Call this once when your app starts or when loading the database
_nextMaterialId = materialDatabase.Any() ? materialDatabase.Max(m => m.ID) + 1 : 1;

Then use Interlocked to increment safely:

int newId = Interlocked.Increment(ref _nextMaterialId);

var newMaterial = new material_data
{
    ID = newId,
    Description = "Your New Material",
    Data = new double[] { 0.5, 1.2, 3.0 }
};

materialDatabase.Add(newMaterial);

Pros: Prevents duplicate IDs even in multi-threaded scenarios.
Cons: Overkill if your app is single-threaded, but no harm in using it anyway.

4. Find the First Available ID (For Continuous Numbering)

If you need IDs to be continuous (no gaps even after deleting items), loop through numbers until you find one that's not used:

int newId = 1;
while (materialDatabase.Any(m => m.ID == newId))
{
    newId++;
}

var newMaterial = new material_data
{
    ID = newId,
    Description = "Your New Material",
    Data = new double[] { 0.5, 1.2, 3.0 }
};

Pros: Maintains continuous numbering.
Cons: Slow for large datasets (since it checks every ID each time). Only recommend this for small collections.

Quick Note for Your TreeView

When repopulating your TreeView after adding new items, make sure to use the unique ID as a reliable identifier (e.g., store it in the TreeNode.Tag property) so you can easily map nodes to their corresponding material_data entries later.

内容的提问来源于stack exchange,提问作者Gabriel M. Silva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:33:14