how to get value from other method in C#












-2















as the title said, I have a problem getting my variable value into other method. because I want to call it in another class

My ViewModel Class



public VModel()
{
DataTable s = new DataTable();

using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
var b = (int)ClickedCommand2.Name;
adapter.SelectCommand = new MySqlCommand("Select * from anime_list where id_movie = '"+ (int)ClickedCommand2.Name +"'", connection);
adapter.Fill(s);
}
Selected = s.DefaultView;
Clicked2 = new ClickedCommand2(this);
}


Clicked Class



public class ClickedCommand2 : ICommand
{
private VModel _vModel;

public ClickedCommand2(VModel vModel)
{
_vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}
public int a;
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndex = id_movie - 1;
a = (int)(_vModel.Library2[rowIndex]["id_movie"]);
}

private static int b = a;
public static int Name
{
get { return b; }
set { b = value; }

}
}


EDIT : the question is how to get A value into my viewmodel class










share|improve this question

























  • Hi and welcome to stackoverflow! Your A value is only in scope of Execute. Declare var a outstide of the method to make it accessible outside of the method.

    – Josh Adams
    Nov 24 '18 at 3:07











  • @JoshAdams can you add an example? im a bit new to c#

    – Hans
    Nov 24 '18 at 3:09











  • NameOfYourClass.Name = 99 and that will set b to 99.

    – CodingYoshi
    Nov 24 '18 at 3:09













  • @CodingYoshi but A value is different whether i click on something? because im getting A value from sql data

    – Hans
    Nov 24 '18 at 3:12






  • 1





    Your question makes no sense. You need to provide more details.

    – CodingYoshi
    Nov 24 '18 at 3:16
















-2















as the title said, I have a problem getting my variable value into other method. because I want to call it in another class

My ViewModel Class



public VModel()
{
DataTable s = new DataTable();

using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
var b = (int)ClickedCommand2.Name;
adapter.SelectCommand = new MySqlCommand("Select * from anime_list where id_movie = '"+ (int)ClickedCommand2.Name +"'", connection);
adapter.Fill(s);
}
Selected = s.DefaultView;
Clicked2 = new ClickedCommand2(this);
}


Clicked Class



public class ClickedCommand2 : ICommand
{
private VModel _vModel;

public ClickedCommand2(VModel vModel)
{
_vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}
public int a;
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndex = id_movie - 1;
a = (int)(_vModel.Library2[rowIndex]["id_movie"]);
}

private static int b = a;
public static int Name
{
get { return b; }
set { b = value; }

}
}


EDIT : the question is how to get A value into my viewmodel class










share|improve this question

























  • Hi and welcome to stackoverflow! Your A value is only in scope of Execute. Declare var a outstide of the method to make it accessible outside of the method.

    – Josh Adams
    Nov 24 '18 at 3:07











  • @JoshAdams can you add an example? im a bit new to c#

    – Hans
    Nov 24 '18 at 3:09











  • NameOfYourClass.Name = 99 and that will set b to 99.

    – CodingYoshi
    Nov 24 '18 at 3:09













  • @CodingYoshi but A value is different whether i click on something? because im getting A value from sql data

    – Hans
    Nov 24 '18 at 3:12






  • 1





    Your question makes no sense. You need to provide more details.

    – CodingYoshi
    Nov 24 '18 at 3:16














-2












-2








-2


0






as the title said, I have a problem getting my variable value into other method. because I want to call it in another class

My ViewModel Class



public VModel()
{
DataTable s = new DataTable();

using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
var b = (int)ClickedCommand2.Name;
adapter.SelectCommand = new MySqlCommand("Select * from anime_list where id_movie = '"+ (int)ClickedCommand2.Name +"'", connection);
adapter.Fill(s);
}
Selected = s.DefaultView;
Clicked2 = new ClickedCommand2(this);
}


Clicked Class



public class ClickedCommand2 : ICommand
{
private VModel _vModel;

public ClickedCommand2(VModel vModel)
{
_vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}
public int a;
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndex = id_movie - 1;
a = (int)(_vModel.Library2[rowIndex]["id_movie"]);
}

private static int b = a;
public static int Name
{
get { return b; }
set { b = value; }

}
}


EDIT : the question is how to get A value into my viewmodel class










share|improve this question
















as the title said, I have a problem getting my variable value into other method. because I want to call it in another class

My ViewModel Class



public VModel()
{
DataTable s = new DataTable();

using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
var b = (int)ClickedCommand2.Name;
adapter.SelectCommand = new MySqlCommand("Select * from anime_list where id_movie = '"+ (int)ClickedCommand2.Name +"'", connection);
adapter.Fill(s);
}
Selected = s.DefaultView;
Clicked2 = new ClickedCommand2(this);
}


Clicked Class



public class ClickedCommand2 : ICommand
{
private VModel _vModel;

public ClickedCommand2(VModel vModel)
{
_vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}
public int a;
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndex = id_movie - 1;
a = (int)(_vModel.Library2[rowIndex]["id_movie"]);
}

private static int b = a;
public static int Name
{
get { return b; }
set { b = value; }

}
}


EDIT : the question is how to get A value into my viewmodel class







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 3:23







Hans

















asked Nov 24 '18 at 3:06









HansHans

207




207













  • Hi and welcome to stackoverflow! Your A value is only in scope of Execute. Declare var a outstide of the method to make it accessible outside of the method.

    – Josh Adams
    Nov 24 '18 at 3:07











  • @JoshAdams can you add an example? im a bit new to c#

    – Hans
    Nov 24 '18 at 3:09











  • NameOfYourClass.Name = 99 and that will set b to 99.

    – CodingYoshi
    Nov 24 '18 at 3:09













  • @CodingYoshi but A value is different whether i click on something? because im getting A value from sql data

    – Hans
    Nov 24 '18 at 3:12






  • 1





    Your question makes no sense. You need to provide more details.

    – CodingYoshi
    Nov 24 '18 at 3:16



















  • Hi and welcome to stackoverflow! Your A value is only in scope of Execute. Declare var a outstide of the method to make it accessible outside of the method.

    – Josh Adams
    Nov 24 '18 at 3:07











  • @JoshAdams can you add an example? im a bit new to c#

    – Hans
    Nov 24 '18 at 3:09











  • NameOfYourClass.Name = 99 and that will set b to 99.

    – CodingYoshi
    Nov 24 '18 at 3:09













  • @CodingYoshi but A value is different whether i click on something? because im getting A value from sql data

    – Hans
    Nov 24 '18 at 3:12






  • 1





    Your question makes no sense. You need to provide more details.

    – CodingYoshi
    Nov 24 '18 at 3:16

















Hi and welcome to stackoverflow! Your A value is only in scope of Execute. Declare var a outstide of the method to make it accessible outside of the method.

– Josh Adams
Nov 24 '18 at 3:07





Hi and welcome to stackoverflow! Your A value is only in scope of Execute. Declare var a outstide of the method to make it accessible outside of the method.

– Josh Adams
Nov 24 '18 at 3:07













@JoshAdams can you add an example? im a bit new to c#

– Hans
Nov 24 '18 at 3:09





@JoshAdams can you add an example? im a bit new to c#

– Hans
Nov 24 '18 at 3:09













NameOfYourClass.Name = 99 and that will set b to 99.

– CodingYoshi
Nov 24 '18 at 3:09







NameOfYourClass.Name = 99 and that will set b to 99.

– CodingYoshi
Nov 24 '18 at 3:09















@CodingYoshi but A value is different whether i click on something? because im getting A value from sql data

– Hans
Nov 24 '18 at 3:12





@CodingYoshi but A value is different whether i click on something? because im getting A value from sql data

– Hans
Nov 24 '18 at 3:12




1




1





Your question makes no sense. You need to provide more details.

– CodingYoshi
Nov 24 '18 at 3:16





Your question makes no sense. You need to provide more details.

– CodingYoshi
Nov 24 '18 at 3:16












1 Answer
1






active

oldest

votes


















0














Assuming (_vModel.Library2[rowIndex]["id_movie"]) is an integer, you can make the scope outside of Execute:



  public class ClickedCommand2 : ICommand
{
private VModel _vModel;

public ClickedCommand2(VModel vModel)
{
_vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}

private static int a;
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndex = id_movie - 1;
a = (int)(_vModel.Library2[rowIndex]["id_movie"]);
}

private static int b = a;
public static int Name
{
get { return b; }
set { b = value; }

}
}





share|improve this answer


























  • a field initializer cannot references to static field it says? on private static int b = a;

    – Hans
    Nov 24 '18 at 3:24











  • fixed for you, make a static

    – Josh Adams
    Nov 24 '18 at 3:37











  • it works, but it show nothing on my viewmodel class when it clicked or selected, if i change to number that i define it works

    – Hans
    Nov 24 '18 at 3:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454830%2fhow-to-get-value-from-other-method-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Assuming (_vModel.Library2[rowIndex]["id_movie"]) is an integer, you can make the scope outside of Execute:



  public class ClickedCommand2 : ICommand
{
private VModel _vModel;

public ClickedCommand2(VModel vModel)
{
_vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}

private static int a;
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndex = id_movie - 1;
a = (int)(_vModel.Library2[rowIndex]["id_movie"]);
}

private static int b = a;
public static int Name
{
get { return b; }
set { b = value; }

}
}





share|improve this answer


























  • a field initializer cannot references to static field it says? on private static int b = a;

    – Hans
    Nov 24 '18 at 3:24











  • fixed for you, make a static

    – Josh Adams
    Nov 24 '18 at 3:37











  • it works, but it show nothing on my viewmodel class when it clicked or selected, if i change to number that i define it works

    – Hans
    Nov 24 '18 at 3:44
















0














Assuming (_vModel.Library2[rowIndex]["id_movie"]) is an integer, you can make the scope outside of Execute:



  public class ClickedCommand2 : ICommand
{
private VModel _vModel;

public ClickedCommand2(VModel vModel)
{
_vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}

private static int a;
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndex = id_movie - 1;
a = (int)(_vModel.Library2[rowIndex]["id_movie"]);
}

private static int b = a;
public static int Name
{
get { return b; }
set { b = value; }

}
}





share|improve this answer


























  • a field initializer cannot references to static field it says? on private static int b = a;

    – Hans
    Nov 24 '18 at 3:24











  • fixed for you, make a static

    – Josh Adams
    Nov 24 '18 at 3:37











  • it works, but it show nothing on my viewmodel class when it clicked or selected, if i change to number that i define it works

    – Hans
    Nov 24 '18 at 3:44














0












0








0







Assuming (_vModel.Library2[rowIndex]["id_movie"]) is an integer, you can make the scope outside of Execute:



  public class ClickedCommand2 : ICommand
{
private VModel _vModel;

public ClickedCommand2(VModel vModel)
{
_vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}

private static int a;
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndex = id_movie - 1;
a = (int)(_vModel.Library2[rowIndex]["id_movie"]);
}

private static int b = a;
public static int Name
{
get { return b; }
set { b = value; }

}
}





share|improve this answer















Assuming (_vModel.Library2[rowIndex]["id_movie"]) is an integer, you can make the scope outside of Execute:



  public class ClickedCommand2 : ICommand
{
private VModel _vModel;

public ClickedCommand2(VModel vModel)
{
_vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}

private static int a;
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndex = id_movie - 1;
a = (int)(_vModel.Library2[rowIndex]["id_movie"]);
}

private static int b = a;
public static int Name
{
get { return b; }
set { b = value; }

}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 3:36

























answered Nov 24 '18 at 3:12









Josh AdamsJosh Adams

1,4132519




1,4132519













  • a field initializer cannot references to static field it says? on private static int b = a;

    – Hans
    Nov 24 '18 at 3:24











  • fixed for you, make a static

    – Josh Adams
    Nov 24 '18 at 3:37











  • it works, but it show nothing on my viewmodel class when it clicked or selected, if i change to number that i define it works

    – Hans
    Nov 24 '18 at 3:44



















  • a field initializer cannot references to static field it says? on private static int b = a;

    – Hans
    Nov 24 '18 at 3:24











  • fixed for you, make a static

    – Josh Adams
    Nov 24 '18 at 3:37











  • it works, but it show nothing on my viewmodel class when it clicked or selected, if i change to number that i define it works

    – Hans
    Nov 24 '18 at 3:44

















a field initializer cannot references to static field it says? on private static int b = a;

– Hans
Nov 24 '18 at 3:24





a field initializer cannot references to static field it says? on private static int b = a;

– Hans
Nov 24 '18 at 3:24













fixed for you, make a static

– Josh Adams
Nov 24 '18 at 3:37





fixed for you, make a static

– Josh Adams
Nov 24 '18 at 3:37













it works, but it show nothing on my viewmodel class when it clicked or selected, if i change to number that i define it works

– Hans
Nov 24 '18 at 3:44





it works, but it show nothing on my viewmodel class when it clicked or selected, if i change to number that i define it works

– Hans
Nov 24 '18 at 3:44




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454830%2fhow-to-get-value-from-other-method-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen