仅检查字符串格式和数字

时间:2018-02-22 10:50:24

标签: c#

我有这个来检查数字:

 private bool IsDigitsOnly(string str)
    {
        foreach (char c in str)
        {
            if (c < '0' || c > '9')
                return false;
        }
        return true;
    }

这是检查字符串len

 private bool CorrectFormat(string str)
    {
        if (str.Length == 9)
            return true;
        return false;
    }

我在属性SET

上使用它
 public string NIF
    {
        get { return nif; }
        set { if (IsDigitsOnly(nif) && CorrectFormat(nif))
                nif = value;
        }

但是当我尝试创建一个对象实例时,我收到了一个错误       当我在参数中使用123456789时,“系统空引用异常”在以前通过两个方法检查时始终为true。

一些帮助?

1 个答案:

答案 0 :(得分:9)

您必须在属性设置器中使用import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { RouterModule } from '@angular/router'; import { HttpModule } from '@angular/http'; import { AddressComponent } from './address.component'; import { AddressService } from './shared/addressService/address.service'; import { ContactInfoService } from './shared/contactService/contactInfo.service'; import { PacsService } from './shared/pacsService/pacs.service'; import { TransportizationService } from './shared/transportizationService/transportization.service'; @NgModule({ imports: [ CommonModule, FormsModule, ReactiveFormsModule, RouterModule, HttpModule ], declarations: [ AddressComponent ], exports: [ AddressComponent ], providers: [ AddressService, ContactInfoService, PacsService, TransportizationService ] }) export class addressModule { } 而不是value

nif

在支票后,您将public string NIF { get { return nif; } set { if (IsDigitsOnly(value) && CorrectFormat(value)) nif = value; } } 分配给支持字段value