2014-04-11 48 views
0

我在我的程序中创建了一个帮助部分,并且尽量使用我拥有的编码知识时尽可能高效。如果我按如下方式输入Help.whichHelp(some_int_here);,我已经设置了一个方法。所有这些代码都在一个文件中,Help.cs。但标签并没有改变,即使没有其他代码,但这应该改变标签。在静态上下文中更改标签的文本

public static void whichHelp(int index) 
    { 
     int allowedCount = 0; 
     foreach (string namesX in HWLib.Variables.helpDir) 
     { 
      allowedCount++; 
     } 
     if (index > allowedCount) 
     { 
      MessageBox.Show("Index " + index + " does not exist!"); 
     } 
     else 
     { 
      if (index == 0) Login(); 
     } 
    } 

这里是上面提到的“登录”方法,如果指数为0

public static void Login() 
    { 
     Help.getContent("Log In", "In order to use this program, you need to be able to log in. " + 
      "This is so we can track how many users we have. It also provides many user benefits to you! " + 
      "\n\n To login, use the username and password provided when you signed up on the website. " + 
      "if you did not signup, you should do so by clicking the \"SignUp\" button on the login screen."); 
    } 

这里是获取内容

static void getContent(String header, String body) 
    { 
     if (header == null || body == null) 
     { 
      MessageBox.Show("Error loading contents of program"); 
      Application.Exit(); 
     } 
     else 
     { 
      //Since "this" isn't valid for an identifier in static context 
      Help thisI = new Help(); 
      thisI.headerLabel.Text = header; 
      thisI.bodyLabel.Text = body; 
     } 
    } 
+0

你忘了问一个问题 –

+0

没有任何更多的信息,我认为答案是“在这种情况下不要使用静态” – vcsjones

+0

对不起,我陷入了这篇文章:) – user2678408

回答

0

标签永远不会因为改变方法其中

Help thisI = new Help(); 
thisI.headerLabel.Text = header; 
thisI.bodyLabel.Text = body; 

这些是仅存在于静态方法中的局部变量。

将Help对象传递给方法怎么样?

static void getContent(String header, String body, Help thisI) 

的这部分代码可以从

int allowedCount = 0; 
foreach (string namesX in HWLib.Variables.helpDir) 
{ 
    allowedCount++; 
} 
if (index > allowedCount) 
{ 
    MessageBox.Show("Index " + index + " does not exist!"); 
} 

改变这样的事情

if (index > HWLib.Variables.helpDir.Count()) 
{ 
    MessageBox.Show("Index " + index + " does not exist!"); 
} 
+0

我将不会有VS访问,直到星期一,所以我会尝试它然后 – user2678408