Getting info from classes in another form

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 871 Views - Last Post: 26 February 2013 - 08:54 AM Rate Topic: -----

#1 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Getting info from classes in another form

Posted 17 February 2013 - 12:11 PM

Hi there.

In form1, as global variable, I have this:

public Swords[] sw = new Swords[2];


And, in the main:

sw[0] = new Swords("sword_1.jpg");
sw[1] = new Swords("sword_2.jpg");

Store ST = new Store();
ST.ShowDialog();


Then, in main of form Store, I have this just to test:
Form1 f = new Form1();
MessageBox.Show(f.sw[0].GetSword());

And it gives an break exception, that in resume says that I dont have any kind of information in sw[0] - its all Null.

How can I solve it?

Is This A Good Question/Topic? 0
  • +

Replies To: Getting info from classes in another form

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Getting info from classes in another form

Posted 17 February 2013 - 12:14 PM

This wreaks of poor design. A couple of tutorials -

Passing Data between forms

How to get values from form1 to form2
Was This Post Helpful? 1
  • +
  • -

#3 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 17 February 2013 - 12:18 PM

Nop. I'm passing the info correctly, I also use it for other areas without problem.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Getting info from classes in another form

Posted 17 February 2013 - 12:21 PM

Well you do have bad design as for starters your array of Swords is public - it should never be public.

Quote

And, in the main:


Quote

Then, in main of form Store, I have this just to test:


What do you mean by 'main'. If you mean the main method then you have some larger knowledge issues here.
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,746
  • Joined: 02-June 10

Re: Getting info from classes in another form

Posted 17 February 2013 - 12:42 PM

View PostKeylogger, on 17 February 2013 - 01:18 PM, said:

Nop. I'm passing the info correctly, I also use it for other areas without problem.


Just because you get info from A to B doesn't really mean it is 'correct' or good design. Ryano121 is right when he tells you this is badly designed. I'm going to tell you if you keep doing it this way you are going to make it an unbreakable bad habit that will keep you from writing more complex programs, and if you get tested at job interview will keep you from getting hired. Take the time now to break this habit and learn the right way to do things.


[*]Q: ...get Form/class 'A' to make a change or talk to Form/class 'B'
Spoiler


View PostKeylogger, on 17 February 2013 - 01:11 PM, said:

And it gives an break exception, that in resume says that I dont have any kind of information in sw[0] - its all Null.


there is no such thing as a 'break exception'. Don't paraphrase these things. Provide the actual info copy/pasted from Visual Studio.

If you mean a "null reference exception" it is the first error covered in this tutorial.
What does this error message mean?
It basically means you're trying to access an object before you give the object a value.

widget x; // No value assigned, so it is null.
widget x = new widget(); // now X has a value and is a constructed widget object


I think Ryan was right about there being deeper 'lack of understanding' issues at work here. There is a tutorial on objects linked in my signature block. You probably should also go back 2-3 chapters in your textbook and re-read until you *comprehend* the material.
Was This Post Helpful? 0
  • +
  • -

#6 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 17 February 2013 - 01:08 PM

"Main" is the method Load of form.

The code I put in the first post is, in my point of view, correct.
What is the part of the code you don't understand? (I'm really asking)

It's a quite simple code. I got a class, that will get several values as array. I fill those values with something, before I call the other form.

When I call the other form, those arrays on the first form already have items/information on them, so I don't get why i'm getting error saying that the fields are all null (it means that they don't have any kind of value).

And by "Fields are all null", i mean:

sw[0] = null;
sw[1] = null;

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,746
  • Joined: 02-June 10

Re: Getting info from classes in another form

Posted 17 February 2013 - 02:21 PM

Quote

And by "Fields are all null", i mean:

Did you read my response in post #5?
Its null because you haven't given sw[0] any value yet.

Quote

Form1 f = new Form1();
MessageBox.Show(f.sw[0].GetSword());


You're making the form, then trying to access a value in an array in the form. If you don't give a value in the class constructor then where would it get a value from? Until you give sw[0] an actual value it is null.


Quote

The code I put in the first post is, in my point of view, correct.


Its not. Two experts are trying to tell you its really bad design. You want to keep saying you're right and we're wrong that's your choice as its you're program.

We can give you links to tutorials on classes, and how to debug (as we've done) but seeing as I'm just talking to the wind - I have other projects that I can devote my time to without feeling like I'm wasting the effort. Best of luck with your program.

You need to stop trying to guess at what you're doing.
Stop just for a little while and do some tutorials and get a better grip on understanding some foundation concepts of C# and .NET.

Bulding an application - Part 1
[*]Building an application - Part 2

C# Learning Series
Was This Post Helpful? 1
  • +
  • -

#8 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 17 February 2013 - 03:47 PM

View PosttlhIn`toq, on 17 February 2013 - 02:21 PM, said:

Quote

Form1 f = new Form1();
MessageBox.Show(f.sw[0].GetSword());


You're making the form, then trying to access a value in an array in the form. If you don't give a value in the class constructor then where would it get a value from? Until you give sw[0] an actual value it is null.

I set the values in the load of the form, so the array get values. If the array get values, even in other form, it should be able to get that info.

If controls get values in their properties you can access those properties in others forms without any problem.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,746
  • Joined: 02-June 10

Re: Getting info from classes in another form

Posted 17 February 2013 - 04:09 PM

I promise the debugger isn't lieing with you just to make you crazy. If it says the values are null then they are null. IE: NO VALUE ASSIGNED
Stop trying to prove that you're right and the debugger is wrong. The sooner you accept that you can make mistakes and start using the debugger to find them the sooner you can make progress.

The arrays don't get values or they wouldn't be null when you debug! Think!

Read the in-code comments

Quote

Form1 f = new Form1();
// f has only been constructed at this point but not loaded
// therefore the loaded event handler method has not executed
MessageBox.Show(f.sw[0].GetSword());
// at this point sw[0] is null because no value is assigned.



Line 1 you make the new instance of form1 class
Line 4 you try to stuff with sw[0].
But this instance of form1 has not loaded yet!
It gets constructed then you start doing shit with it.
If you would debug it and put a breakpoint in the load method you'd see it didn't get called.
Was This Post Helpful? 0
  • +
  • -

#10 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 18 February 2013 - 06:24 AM

I understand what you say, and I agree with that. In VB.NET we have modules..so it helps.

The thing is, I need to pass the whole array information of form1 to form2, and then in the form2 update the array info of form1.

And I don't really know how to do it. What I see about passing info between forms is with textboxs and stuff like that. E
Was This Post Helpful? 0
  • +
  • -

#11 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Getting info from classes in another form

Posted 18 February 2013 - 07:58 AM

Quote

I understand what you say, and I agree with that. In VB.NET we have modules..so it helps.


We've got static classes in C#, which are effectively the C# equivalent of VB.NET modules. However, you shouldn't be using a module or a static class to do things like this.

If class B needs an item from class A, pass the item to class B in it's constructor. Don't create an external (essentially global) dependency that both share and access.

If Form2 really needs write access to the entire array, the simplest solution would be to pass the array to Form2's constructor:

Form2 frm = new Form2(sw); //where 'sw' is the array, or a copy of the array


Bear in mind that if you pass the reference to the actual array directly to Form2, Form2 is completely free to change the contents of the array referenced by Form1, without Form1 knowing. If you wanted to avoid this, one solution would be to pass in a copy of the array instead.

This post has been edited by CodingSup3rnatur@l-360: 18 February 2013 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#12 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 18 February 2013 - 09:28 AM

View PostCodingSup3rnatur@l-360, on 18 February 2013 - 07:58 AM, said:

Form2 frm = new Form2(sw); //where 'sw' is the array, or a copy of the array

That doesn't work.

Quote

Form2: does not contain a constructor that takes 1 arguments

And I understand by this error that Form2, have to receive array arguments, but remember my array is a type of class.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,746
  • Joined: 02-June 10

Re: Getting info from classes in another form

Posted 18 February 2013 - 09:39 AM

Then make a new constructor that takes the one argument. Sheesh!
Was This Post Helpful? 1
  • +
  • -

#14 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,518
  • Joined: 08-April 09

Re: Getting info from classes in another form

Posted 18 February 2013 - 02:52 PM

Keylogger, have you read anything about OOP(Classes, constructors in this case)?
If you have i don't think you understand them very well.
Was This Post Helpful? 0
  • +
  • -

#15 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Getting info from classes in another form

Posted 18 February 2013 - 03:54 PM

Yes I do. I have many classes implemented.

Passing information between forms is a new kind of thing, that I didn't have problem in VB.
VB makes it simple.

In C# is like finding a simple thing in hell. Huge parts of code to do just a tiny thing.

I really don't know how to do this: Open form2, and in the form2, get the info of array in form1.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2