0

'我是 C# 的新手,通常我的强项是 C++ 和 VB。我有 2 个问题,我对代码发表了评论:

1.) 蓝色波浪线出现在 3 Uses it say ""一个 using namespace 指令只能应用于命名空间;'系统.对象' ...“

2.) 我无法从我声明为公共的变量 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() ;

    ....
    ....
4

3 回答 3

1

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

于 2013-06-25T01:26:53.347 回答
1

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

using Dns = System.Net.Dns;

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

于 2013-06-25T01:27:41.987 回答
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;
于 2013-06-25T01:39:04.440 回答