problem in Class
-
I have a class and two form in my project i want to validate a filed from form1 and use this field's value in form2 but when i want to use it i cant't access to this value becuase every time that i new ، object from class filed's value was destroyed how can i do that ?
-
I have a class and two form in my project i want to validate a filed from form1 and use this field's value in form2 but when i want to use it i cant't access to this value becuase every time that i new ، object from class filed's value was destroyed how can i do that ?
-
I think you need to post some code here. I'm sure once you do that someone will be able to help you. In the meanwhile, also check if the singleton pattern can help you in anyway.
My signature "sucks" today
class class1 { public int a; } form1 : class1 s=new class1(); s.a=10 form2: class1 c=new class1(); messageBox.show(c.a.tostring()); // I want to see 10 here
-
I have a class and two form in my project i want to validate a filed from form1 and use this field's value in form2 but when i want to use it i cant't access to this value becuase every time that i new ، object from class filed's value was destroyed how can i do that ?
What you need is a static class that can be shared between forms. You can use Session or Application state for this if you are using ASP.NET, however, if the object is large it may not be the best way to handle it. You can also pass the value you are interested in from form1 to form2 via properties, events, constructors.
I know the language. I've read a book. - _Madmatt
-
class class1 { public int a; } form1 : class1 s=new class1(); s.a=10 form2: class1 c=new class1(); messageBox.show(c.a.tostring()); // I want to see 10 here
SajjadZare wrote:
I want to see 10 here
only people unfamiliar to object orientation would expect or accept that to happen. you have two statements containing
new class1()
which means two objects get created, i.e. two different instantiations of theclass1
class. Now if one object is changed, that should not reflect on its siblings. If your house has ten windows and you happen to break one of them, you would not like all of them to break into pieces, would you? There are a couple of ways to get what you want. They all have one thing in common: they create only one instance of the thing that needs to be accessed at several locations, instead they pass a reference around. And there is a plethora of articles on the subject, maybe this one[^] can help you. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
class class1 { public int a; } form1 : class1 s=new class1(); s.a=10 form2: class1 c=new class1(); messageBox.show(c.a.tostring()); // I want to see 10 here
Because what you are doing is creating two separate instances, you can only do this if there is a single variable "a" that is common to all instances of "class1". You do this with the
static
keyword.:class class1
{
public static int a;
}You can then use it in any instance. form1:
class1 s=new class1();
class1.a=10form2:
class1 c = new class1();
MessageBox.Show(class1.a.ToString());But you can't use "s.a" or "c.a" because it would look confusing - you might think that "a" was specific to the particular instance of class1 rather than shared between them. It's a bit like cars: They can all have a colour, but they don't share it: YourCar has a different colour (blue) to MyCar (red). They do share common information: NumberOfWheels (4) So you can't say "What colour is a car?" but you can say "How many wheels has a car?" You can say "What colour is my car?" or "What colour is your car?" because this specifies exactly which car you are talking about. If you feel you must access "a" via an instance, you can declare a property which does that for you (but it isn't really recommended):
class class1
{
private static int a;
public int A
{
get { return a; }
set { a = value;}
}
}You can now access "A" via "s.A" and "c.A" to get the same value which is shared between all instances of class1.
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
Because what you are doing is creating two separate instances, you can only do this if there is a single variable "a" that is common to all instances of "class1". You do this with the
static
keyword.:class class1
{
public static int a;
}You can then use it in any instance. form1:
class1 s=new class1();
class1.a=10form2:
class1 c = new class1();
MessageBox.Show(class1.a.ToString());But you can't use "s.a" or "c.a" because it would look confusing - you might think that "a" was specific to the particular instance of class1 rather than shared between them. It's a bit like cars: They can all have a colour, but they don't share it: YourCar has a different colour (blue) to MyCar (red). They do share common information: NumberOfWheels (4) So you can't say "What colour is a car?" but you can say "How many wheels has a car?" You can say "What colour is my car?" or "What colour is your car?" because this specifies exactly which car you are talking about. If you feel you must access "a" via an instance, you can declare a property which does that for you (but it isn't really recommended):
class class1
{
private static int a;
public int A
{
get { return a; }
set { a = value;}
}
}You can now access "A" via "s.A" and "c.A" to get the same value which is shared between all instances of class1.
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
I'm not so sure this is a good suggestion. Maybe what he wants to share deserves to be static and unique, but I didn't get a clue in that direction; simply adding "static" to get something to work is not OK in my books. As I guess you read similar books, maybe I missed something? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
I'm not so sure this is a good suggestion. Maybe what he wants to share deserves to be static and unique, but I didn't get a clue in that direction; simply adding "static" to get something to work is not OK in my books. As I guess you read similar books, maybe I missed something? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
Oh I agree - static is best reserved for when it is really needed - but since I haven't a clue what he wants to do with it, I didn't want to just leap in with "you can't do it", so I tried to explain a bit as to when you use static. I don't think I have used static for anything other than methods in C# yet - just haven't found the need... I started writing my response before you posted yours, and saw yours there after I pressed "Post Message". I'm just a slow typist sometimes - especially when the wife is trying to use the internet so I have to stop and sort her out every few minutes. :)
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
Oh I agree - static is best reserved for when it is really needed - but since I haven't a clue what he wants to do with it, I didn't want to just leap in with "you can't do it", so I tried to explain a bit as to when you use static. I don't think I have used static for anything other than methods in C# yet - just haven't found the need... I started writing my response before you posted yours, and saw yours there after I pressed "Post Message". I'm just a slow typist sometimes - especially when the wife is trying to use the internet so I have to stop and sort her out every few minutes. :)
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
Oh great, blame the wife for giving an overly static/Shared and slow response. :laugh: Most of my classes do have a couple of static data members, often for statistical purposes (counting or holding a list of all instances), and for logging parameters. But these are just tools, they don't reflect application-domain properties. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
modified on Friday, June 4, 2010 5:55 PM
-
Oh great, blame the wife for giving an overly static/Shared and slow response. :laugh: Most of my classes do have a couple of static data members, often for statistical purposes (counting or holding a list of all instances), and for logging parameters. But these are just tools, they don't reflect application-domain properties. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
modified on Friday, June 4, 2010 5:55 PM
She is a natural VB user - she finds out about a tool, then she uses it for everything. :laugh: Everything can be a hammer, if you hit it hard enough...
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
She is a natural VB user - she finds out about a tool, then she uses it for everything. :laugh: Everything can be a hammer, if you hit it hard enough...
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
Now it all makes sense; I have fixed my earlier message accordingly. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).