Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problem in Class

problem in Class

Scheduled Pinned Locked Moved C#
questionhelp
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SajjadZare
    wrote on last edited by
    #1

    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 ?

    A N 2 Replies Last reply
    0
    • S SajjadZare

      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 ?

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • A Abhinav S

        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

        S Offline
        S Offline
        SajjadZare
        wrote on last edited by
        #3

        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

        L OriginalGriffO 2 Replies Last reply
        0
        • S SajjadZare

          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 ?

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • S SajjadZare

            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

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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 the class1 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).


            1 Reply Last reply
            0
            • S SajjadZare

              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

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              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=10

              form2:

              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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              L 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                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=10

                form2:

                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.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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).


                OriginalGriffO 1 Reply Last reply
                0
                • L Luc Pattyn

                  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).


                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  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.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  L 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    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.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    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

                    OriginalGriffO 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      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

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      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.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      L 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        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.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        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).


                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups