In programming, novice, so any advice in writing code will be greatly appreciated! I am writing a program for getting a list of friends from VK.

private void button1_Click(object sender, EventArgs e) { string email = txtlogin.Text.ToString(); string pass = txtpass.Text.ToString(); var api = new VkApi(); Settings settings = Settings.All; int appId = appid; Form2 form2; form2 = new Form2(); if (txtlogin.Text != "" || txtpass.Text != "") { try { api.Authorize(appId, email, pass, settings); } catch { MessageBox.Show("Неверный логин или пароль"); } } else { MessageBox.Show("Заполните поля", ""); } form2.Show();//открываю вторую форму после успешной авторизации this.Hide();// закрываем первую форму } 

As I think, and it is probably obvious that the second form does not see that the authorization has been successfully passed and you can contact VC.

The question is, how should I be? Thank!

Here is what I am writing in the second form:

  var api = new VkApi(); var group = api.Utils.ResolveScreenName("etorostov"); long id = group.Id.Value; int totalCount = 1; int count = 5; var wallpost = api.Wall.Get(-id, out totalCount, count); foreach (var one in wallpost) { listBox1.Items.Add(one.Text); } 

Gives an error message

 An unhandled exception of type 'VkNet.Exception.AccessTokenInvalidException' occurred in VkNet.dll 
  • one
    in what form should she “see” that the authorization is passed? Should it open only if the authentication is successful? - PashaPash
  • After authorization, it opens, everything is ok. Well, how do I continue to work? The second form does not know that authorization has passed and everything is OK? - shatoidil
  • Well, then open it only in case of successful authorization, as I suggested to you for the answer (and as it was intended, judging by your comments in the code). Then the second form will always know that everything is ok, and it can start working right after the opening - because in the case of “not ok” it would not open. - PashaPash
  • OK, let's take it from the other side. What should happen with unsuccessful authorization? should the second form seem like this? what happens next? user corrects the name and password and again presses the button? Can the second form do something until the authorization is completed? - PashaPash
  • An unhandled exception of type 'VkNet.Exception.AccessTokenInvalidException' occurred in VkNet.dll in the second form it gives this error - shatoidil

1 answer 1

Apparently, the second form is not workable at all if the login fails. So it should not be displayed in this case. Rearrange the creation and display of the second form in the if , then it will be displayed only after a successful login.

in the first form:

 if (txtlogin.Text != "" || txtpass.Text != "") { try { api.Authorize(appId, email, pass, settings); Form2 form2; form2 = new Form2(api); form2.Show();//открываю вторую форму после успешной авторизации this.Hide();// закрываем первую форму } catch { MessageBox.Show("Неверный логин или пароль"); } } else { MessageBox.Show("Заполните поля", ""); } 

In the second form, add a property and a parameter to the VkApi parameter type VkApi :

 VkApi Api { get; set; } public Form2(VkApi api) { this.Api = api; // тут то, что уже есть в конструкторе InitializeComponent(); } 

... and use the same object about the work from the second form:

 var api = this.Api; // <-- то, что передали из form1 через конструктор var group = api.Utils.ResolveScreenName("etorostov"); long id = group.Id.Value; int totalCount = 1; int count = 5; var wallpost = api.Wall.Get(-id, out totalCount, count); foreach (var one in wallpost) { listBox1.Items.Add(one.Text); } 

By the way, using exceptions for normal control flow is not good. The Authorize method should return bool , and not fall with the exception in a completely normal (from the point of view of the program) situation - an incorrect login and password.

  • You misunderstood, the second form opens, how can I make it to see that the authorization is successful and you can already work with the command line? - shatoidil
  • What does "saw" mean? - PashaPash
  • VkNet.Exception.AccessTokenInvalidException - shatoidil
  • Have you tried the solution from the answer? - PashaPash
  • Yes, everything is fine but the second form throws out with an error - shatoidil