小编Dav*_*een的帖子

使用Match和Regex

我正在研究一个解析传入文本文件的项目。我正在学习C#。我当前用于挑选所需信息的方法如下:

string MyMatchString = @"a pattern to match";
Match MyMatch = Regex.Match(somestringinput, MyMatchString);
if (MyMatch.Succes)
{
   DoSomething(MyMatch.Value);
}
Run Code Online (Sandbox Code Playgroud)

我正在做很多事情。我希望能够将比赛和测试成功结合在一起。浏览类列表,Regex有一个IsMatch()方法,但是看来我不能访问匹配的值(假设它成功了)。我想我需要一个Match实例。我试过了

if ((Match MyMatch = Regex.Match(somestringinput, MyMatchString).Success)
Run Code Online (Sandbox Code Playgroud)

但是当然有一个编译错误。

我在考虑采用匹配模式然后输入然后返回布尔值的静态方法。然后,我可以测试是否成功,如果可以,则获取匹配的值。

c# regex

5
推荐指数
1
解决办法
3854
查看次数

将形状添加到新的Visio文档

我有这个代码创建一个新的Visio文档并添加一个矩形.它有效,但我不想打开另一个文档来获取Masters集合.问题是新文档有一个空的Masters形状集合.我在Document类中找不到一个方法来向Masters集合添加形状,并且我可以找到添加形状的所有示例,假设您有一个现有文档.有没有更好的方法来做我想要的?

// create the new application
Visio.Application va = new Microsoft.Office.Interop.Visio.Application();

        // add a document
        va.Documents.Add(@"");

       // Visio.Documents vdocs = va.Documents;

        // we need this document to get its Masters shapes collection
        // since our new document has none 
        Visio.Document vu = vdocs.OpenEx(@"C:\Program Files (x86)\Microsoft       Office\Office12\1033\Basic_U.vss", (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked);

        // set the working  document to our new document
        Visio.Document vd = va.ActiveDocument;

        // set the working page to the active page
        Microsoft.Office.Interop.Visio.Page vp = va.ActivePage;

      // if we try this from the …
Run Code Online (Sandbox Code Playgroud)

c# visio office-interop

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

while()没有{}?

我在VS2010的一个项目中有这个代码 - 它是一个我尚未完全实现的占位符方法.我今天开始实施.请注意,while语句的if/else周围没有{}.这编译了很多次 - 这已经有一段时间了.这是VS中的一个错误吗?我认为循环都需要{}

private void ParsefCIPProfiles(string block)
{
 StringReader reader = new StringReader(block);
 string readline = reader.readline();

 while (readline != null)
  if ()
  {}
  else
  {}
}
Run Code Online (Sandbox Code Playgroud)

c#

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

WPF - 当我调整窗口按钮的大小时不要移动

这是XAML.两个问题.当窗口打开时,两个按钮都在底部被切断,"Save_Search"按钮在右侧被切断.第二个问题是,当我调整窗口大小时,列表视图会像我预期的那样变长,但按钮都会在列表视图的中间结束,而不是相对于窗口边框移动.我搜索的时间并不多.

    Title="Queries" Height="274" Width="540">



<DockPanel Height="Auto" HorizontalAlignment="Stretch" Name="dockPanel1" VerticalAlignment="Stretch" Width="Auto">
    <Grid Height="Auto" Width="Auto" Margin="0,0,0,0">

        <TextBox Height="27" HorizontalAlignment="Left" Margin="256,6,0,0" Name="SearchTermsTextBox" VerticalAlignment="Top" Width="227" 
                  Text="Enter search terms separated by `;`"/>

        <ListView Name="ResultsListView" Margin="6,41,13,48"    ItemsSource="{Binding}" Width="auto">
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock FontSize="15" FontWeight="Bold" Text="{Binding Name}"/>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListView.GroupStyle>

            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="Width" Value="Auto" />
                    <Setter Property="FontSize" Value="10.4"  />
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>


        <Button Content="Save Search" Margin="425,203,12.6,17.6"  Name="Save_Search"  Width="96" Height="25"  />
        <Button Name="Query_Button" Content="Ports" Margin="310,203,127.6,0" Height="25" Width="96" VerticalAlignment="Top"></Button>
    </Grid> …
Run Code Online (Sandbox Code Playgroud)

wpf xaml

3
推荐指数
1
解决办法
6916
查看次数

String StartsWith()任何方法避免2次检查?

我有这个代码.当时并不明显,但所写的代码总是会选择第一个选项,因为"fc"和"fcip"都以"fc"开头.

string fcportdelimit = "fc";
string fcipportdelimit = "fcip";

if (BlockToProcess[0].StartsWith(fcportdelimit)) 
{
    try
    {
        this.ParseFCInterface(BlockToProcess);
    }
    catch (Exception E)
    {
        throw;
    } 
}
else if (BlockToProcess[0].StartsWith(fcipportdelimit)) 
{
    try
    {
        this.ParseFCIPInterface(BlockToProcess);
    }
    catch (Exception E)
    {
        throw;
    } 
}
Run Code Online (Sandbox Code Playgroud)

我查看了字符串类,但没有看到将模式作为输入的StartsWith()或Contains().我正在测试的字符串要么是patttern fcN/N,要么是fcipN,其中N是数字.所以,我想我必须做这样的事情?

if (BlockToProcess[0].StartsWith(fcportdelimit || fcipportdelimit) 
{ 
    if (BlockToProcess[0].StartsWith(fcipportdelimit)
    { 
       // do something here
    } 
    else
    { 
       //since fcipportdelimit didn't match it must be an fcport
       //so do something else
    }
}
Run Code Online (Sandbox Code Playgroud)

c#

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

如何编写更复杂的LINQ查询

假设我有:

public class Cluster
{
   List<Host>  HostList = new List<Host>();
}
public class Host
{
   List<VDisk> VDiskList = new List<VDisk>();
} 

public class VDisk
{
   public string Name {get; set}
}
Run Code Online (Sandbox Code Playgroud)

我需要具有给定名称的VDisk的Cluster对象中的所有主机.我可以用foreach做,但宁愿有LINQ查询.我尝试了一个SelectMany(),但它返回VDisk而不是Hosts.我是否需要实现自定义Comparer才能执行此操作?

这是我试过的:

Cluster CurrentCluster = new Cluster();

// add some hosts here

VDisk vdisk = new VDisk();
vdisk.Name="foo";
Run Code Online (Sandbox Code Playgroud)

所以现在我想要所有拥有名为"foo"的虚拟磁盘的主机

这将返回虚拟磁盘,而不是主机:

CurrentCluster.Hosts.SelectMany(h => h.VDisks.Where(v => v.Name == vdisk.Name));
Run Code Online (Sandbox Code Playgroud)

c# linq

2
推荐指数
1
解决办法
1400
查看次数

寻找集合中的差异

我有这个类(部分列表):

class CiscoSwitch 
{
  private string _SwitchName = string.Empty;
  public SwitchName {get {return _SwitchName;} set{_SwitchName=value; }}
}
Run Code Online (Sandbox Code Playgroud)

我有两个CiscoSwitch对象列表.我试图比较它们来挑选那些不重复的.我只想要重复.我尝试了一个Lambda表达式,但得到了一个编译器错误,即CiscoSwitch是非delgate类型.

我现在想知道这样的事情 - 它允许我使用List.Except()方法(我认为):

static class SwitchComparer
{ 
  static bool CompareSwitchNames(CiscoSwitch s1, CiscoSwitch s2)
         {
            if (sw1.SwitchName == s2.SwitchName) {return true;}
             else {return false;}
         }
}

     // to find the differences 
 // this is a method of the CiscoSwitchClass
private List<CiscoSwitch> FindDifferences(List<CiscoSwitch> List1, List<CiscoSwitch> List2)
{
       return List1.Except(List2, SwitchComparer.CompareSwitchNames();
 }
Run Code Online (Sandbox Code Playgroud)

这也可以用foreach完成,但我认为这种方式更清晰,如果它是正确的.我也在想有一天我可能想比较一下CiscoSwitch的其他属性,所以可以根据需要在SwitchComparer类中添加方法.

c#

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

C# 转换不起作用

我有这些课程,部分列出了相关位。

public class IVRTopology {}

public abstract class SANSwitch 
{  public string name { get; set; }
}

public class CiscoSwitch : SANSwitch
{
  public IVRTopology IVRTop = new IVRToplogy()
}

 class SwitchViewModel : INotifyPropertyChanged
{
   public SANSwitch sanswitch {  get;  set;  }
}
Run Code Online (Sandbox Code Playgroud)

当我做这样的事情时:

SwitchViewModel svm = new SwitchViewModel();
svm.sanswitch = new CiscoSwitch();
IVRTopology topo = svm.sanswitch.IVRTop;
Run Code Online (Sandbox Code Playgroud)

如果我尝试这样访问 IVRTop,编译器会警告我 svm.sanswitch 属于 SANSwitch 类型,并且没有 IVRTop 的定义:

IVRTopology topo = (CiscoSwitch)svm.SANSwitch.IVRTop
Run Code Online (Sandbox Code Playgroud)

也不行。我不想将 IVRTopology 的定义添加到抽象类中,因为我将有不使用它的其他子类。演员阵容有问题吗?我怎样才能做到这一点?我希望视图模型最终支持不同类型的开关。

c# casting

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

标签 统计

c# ×7

casting ×1

linq ×1

office-interop ×1

regex ×1

visio ×1

wpf ×1

xaml ×1