DNS和AddressFamily上的“使用命名空间”问题

时间:2013-06-25 01:24:55

标签: c# .net

是C#的新手,通常我的强项是C ++和VB。我有两个问题,其中我评论了代码:

1。)蓝色波浪线出现在3使用它说“”使用命名空间指令只能应用于名称空间; 'System.Object'......“

2。)我无法从我声明为public的变量LocalIP中获取输出IP地址。

提前致谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

//Blue squiggly line appear on this 2 Uses
using Dns = System.Net.Dns;
using AddressFamily = System.Net.Sockets.AddressFamily;


namespace WindowsFormsApplication1
{
public partial class frm_Log : Form
{
    public String localIP;

    public string LocalIPAddress()
    {
        IPHostEntry host;
        //string localIP = "";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                localIP = ip.ToString();
                break;
            }
        }
        return localIP;
    }

    ....
    ....

    // variable localIP.Trim() does not giveout any output
    MyValue="LogAccept,"+this.txt_UserName.Text.Trim()+","+this.txt_Password.Text.Trim() + "," + localIP.Trim() ;

    ....
    ....

3 个答案:

答案 0 :(得分:1)

System.Object是一个班级。 System是命名空间,您已将其定义为文件顶部的第一个using

答案 1 :(得分:1)

http://msdn.microsoft.com/en-US/library/sf0df423(v=vs.80).aspx建议使用以下语法:

using Dns = System.Net.Dns;

using(using System.Linq;)的标准版本只能定位名称空间,而不能定位命名空间中的类。但是,如果已导入整个命名空间,则无需专门导入其中的任何内容。

答案 2 :(得分:0)

原始代码是

using System
....
using System.Object;
using System.Net.Dns;
using System.Net.Sockets.AddressFamily;
....

分辨率

//using System.Object --> since uses system already defined.
using Dns = System.Net.Dns;
using AddressFamily = System.Net.Sockets.AddressFamily;