添加属性到字符串

huangapple go评论65阅读模式
英文:

Add Properties to string

问题

这是您提供的代码的翻译部分:

它关闭了我的原始问题。对C#相当新,我想知道是否有一种方法可以实现以下操作:

string joint = "Press Joint" || "ProPress";

我是否可以使用switch语句来实现这一点?即将字符串设置为空,并以编程方式提取一个值,然后根据该值切换情况?我已经创建了一个更新程序实用程序,用于检查Revit中的模型,以强制执行最小管道长度标准。代码相当长,但希望我提取出来的内容有意义。

internal class UtilsFabricationPipeLengths
{
    public static FailureDefinitionId illegalPipeLengthFailureId;
    public static FailureDefinitionId illegalPipeLengthFailureId2;
    public static FailureDefinitionId illegalPipeLengthFailureId3;
    // public static FailureDefinitionId illegalPipeLengthFailureId4;
    public static FailureDefinitionId illegalPipeLengthFailureId5;
    public static FailureDefinitionId illegalPipeLengthFailureId6;
    public static FailureDefinitionId illegalPipeLengthFailureId7;
    public class FabricationPipeLengthsUpdater : IUpdater
    {
        private readonly UpdaterId fabricationPipeLengthsUpdaterId;
        private FabricationDimensionDefinition fabricationDimensionDefinition;
        public FabricationPipeLengthsUpdater(AddInId id)
        {
            fabricationPipeLengthsUpdaterId = new UpdaterId(id, new Guid("B0C85739-93DB-44A6-BC74-92280998063E"));
        }
        public void Execute(UpdaterData data)
        {
            try
            {
                {
                    string brazed = "Brazed";
                    string pressJoint = "Press Joint";
                    string soldered = "Soldered";
                    string threaded = "Threaded";
                    string buttWeld = "ButtWeld";
                    string pexJoint = "Pex Joint";

                    Document doc = data.GetDocument();
                    List<ElementId> ids = new List<ElementId>();
                    ids.AddRange(data.GetAddedElementIds());
                    ids.AddRange(data.GetModifiedElementIds());
                    foreach (ElementId id in ids)
                    {
                        if (doc.GetElement(id) is FabricationPart fabricationPart)
                        {
                            Autodesk.Revit.DB.Parameter lLength = fabricationPart.LookupParameter("Length");
                            lLength.AsDouble().ToString();
                            Autodesk.Revit.DB.Parameter fpdsd = fabricationPart.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH);
                            fpdsd.AsDouble().ToString();

                            // 不要执行如果更改不是由所需的参数触发的
                            if (!data.IsChangeTriggered(id, Element.GetChangeTypeParameter(new ElementId(BuiltInParameter.FABRICATION_PART_LENGTH))))
                            {
                                break;
                            }
                            if (!fabricationPart.IsValidObject)
                                continue;
                            // Press To Existing Soldered
                            if (fabricationPart.ProductInstallType == pressJoint &&
                                (decimal)lLength.AsDouble() < 0.2239583333m &&
                                fabricationPart.ProductSizeDescription == "1-1/4" ||
                                fabricationPart.ProductInstallType == pressJoint &&
                                (decimal)lLength.AsDouble() < 0.2812500000m &&
                                fabricationPart.ProductSizeDescription == "1-1/2" ||
                                fabricationPart.ProductInstallType == pressJoint &&
                                (decimal)lLength.AsDouble() < 0.3229166667m &&
                                fabricationPart.ProductSizeDescription == "2")
                            {
                                // 执行某些操作
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        public UpdaterId GetUpdaterId()
        {
            UpdaterId id = fabricationPipeLengthsUpdaterId;
            return (id);
        }
        public ChangePriority GetChangePriority()
        {
            return ChangePriority.MEPSystems;
        }
        public string GetUpdaterName()
        {
            return "Fabrication Pipe Updater";
        }
        public string GetAdditionalInformation()
        {
            return "Fabrication Pipe updater example: checks the lengths of pipe to make sure " +
                "all pipes meet minimum pipe length requirements.";
        }
    }
}

变量pressJoint的字符串值可以在"Press Joint"、"ProPress"和"PressFit"之间变化。

英文:

It closed my original question. Fairly new to c# & I'm wondering if there is a way to do the following:

string joint = &quot;Press Joint&quot; || &quot;ProPress&quot;; 

Would I achieve this using a switch statement? I.e. set the string to be empty & programmatically pull a value & switch the case depending on the value? I've created an updater utility to check the models in Revit to enforce minimum pipe length standards. The code is pretty lengthy but hopefully what I pulled out makes sense.

    internal class UtilsFabricationPipeLengths
{
public static FailureDefinitionId illegalPipeLengthFailureId;
public static FailureDefinitionId illegalPipeLengthFailureId2;
public static FailureDefinitionId illegalPipeLengthFailureId3;
// public static FailureDefinitionId illegalPipeLengthFailureId4;
public static FailureDefinitionId illegalPipeLengthFailureId5;
public static FailureDefinitionId illegalPipeLengthFailureId6;
public static FailureDefinitionId illegalPipeLengthFailureId7;
public class FabricationPipeLengthsUpdater : IUpdater
{
private readonly UpdaterId fabricationPipeLengthsUpdaterId;
private FabricationDimensionDefinition fabricationDimensionDefinition;
public FabricationPipeLengthsUpdater(AddInId id)
{
fabricationPipeLengthsUpdaterId = new UpdaterId(id, new Guid(&quot;B0C85739-93DB-44A6-BC74-92280998063E&quot;));
}
public void Execute(UpdaterData data)
{
try
{
{
string brazed = &quot;Brazed&quot;;
string pressJoint = &quot;Press Joint&quot;;
string soldered = &quot;Soldered&quot;;
string threaded = &quot;Threaded&quot;;
string buttWeld = &quot;ButtWeld&quot;;
string pexJoint = &quot;Pex Joint&quot;; 
Document doc = data.GetDocument();
List&lt;ElementId&gt; ids = new List&lt;ElementId&gt;();
ids.AddRange(data.GetAddedElementIds());
ids.AddRange(data.GetModifiedElementIds());
foreach (ElementId id in ids)
{
if (doc.GetElement(id) is FabricationPart fabricationPart)
{
Autodesk.Revit.DB.Parameter lLength = fabricationPart.LookupParameter(&quot;Length&quot;);
lLength.AsDouble().ToString();
Autodesk.Revit.DB.Parameter fpdsd = fabricationPart.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH);
fpdsd.AsDouble().ToString();
//Don&#39;t execute if the change was not trigerred by the wanted parameter
if (!data.IsChangeTriggered(id, Element.GetChangeTypeParameter(new ElementId(BuiltInParameter.FABRICATION_PART_LENGTH))))
{
break;
}
if (!fabricationPart.IsValidObject)
continue; 
#region Press To Existing Soldered
if ( fabricationPart.ProductInstallType == pressJoint &amp;&amp;
(decimal)lLength.AsDouble() &lt; 0.2239583333m &amp;&amp;
fabricationPart.ProductSizeDescription == &quot;1-1/4&quot; ||
fabricationPart.ProductInstallType == pressJoint &amp;&amp;
(decimal)lLength.AsDouble() &lt; 0.2812500000m &amp;&amp;
fabricationPart.ProductSizeDescription == &quot;1-1/2&quot; ||
fabricationPart.ProductInstallType == pressJoint &amp;&amp;
(decimal)lLength.AsDouble() &lt; 0.3229166667m &amp;&amp;
fabricationPart.ProductSizeDescription == &quot;2&quot;)
#endregion
else
{
continue;
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
public UpdaterId GetUpdaterId()
{
UpdaterId id = fabricationPipeLengthsUpdaterId;
return (id);
}
public ChangePriority GetChangePriority()
{
return ChangePriority.MEPSystems;
}
public  string GetUpdaterName()
{
return &quot;Fabrication Pipe Updater&quot;;
}
public string GetAdditionalInformation()
{
return &quot;Fabrication Pipe updater example: checks the lenghts of pipe to make sure &quot; +
&quot;all pipes meet minimum pipe length requirements.&quot;;
}
}
}

The string value pressJoint can vary with what is being checked between "Press Joint", "ProPress", & "PressFit".

答案1

得分: 3

No. A string is one value. If you want to test whether a string is something or something else - or one of some set of options, then:

  1. for constant strings, you can use if (value is "abc" or "def") {...}
  2. otherwise, create some kind of collection (perhaps a HashSet<string>) and use if (options.Contains(value)) {...}
英文:

No. A string is one value. If you want to test whether a string is something or something else - or one of some set of options, then:

  1. for constant strings, you can use if (value is &quot;abc&quot; or &quot;def&quot;) {...}
  2. otherwise, create some kind of collection (perhaps a HashSet&lt;string&gt;) and use if (options.Contains(value)) {...}

答案2

得分: 2

你似乎想要能够在同一个变量中存储两个值。对此,你不需要使用字符串。我建议你使用一个List<string>()

List&lt;string&gt; joints = new List&lt;string&gt;();

然后添加到列表中,

joints.Add("Press Joint");
joints.Add("ProPress");

要检查是否存在匹配,

if (joints.Contains(brazed))
{ 
   //进行一些操作 
}

希望这能帮到你。

英文:

You appear to want to be able to store two values in the same variable. You don't want a string for that. I would suggest you use a List<string>()

List&lt;string&gt; joints = new List&lt;string&gt;();

Then to add to the list,

joints.Add(&quot;Press Joint&quot;);
joints.Add(&quot;ProPress&quot;);

To check for a match,

if (joints.Contains(brazed)
{ 
//do something 
}

I hope this helps.

huangapple
  • 本文由 发表于 2023年6月15日 23:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定