VBForums >
.NET >
C# > But I get error: Object reference not set to an instyance of an object.
Click to See Complete Forum and Search --> : But I get error: Object reference not set to an instyance of an object.
tutus
Oct 4th, 2006, 10:18 AM
When I define my class like:
public class ProductEntity
{
private int productID;
// Public properties, to expose the state of the entity
public int ProductID
{
get { return productID; }
set { productID = value; }
}
......
then to bind the ProductID to my textbox.Text in a form I do:
textBox1.DataBindings.Add("Text", myProductObject, "ProductID");
But I get error: Object reference not set to an instyance of an object.
But When I initialize the value of productID like in
private int productID=0;
that fixes the problem
My question:
I don t see why I should initialize that value.
Is there a way not to get the error without initializing productID .
and what s a good practice for this pls (I mean binding Business entity classes to form controls)
Thanks
P.S: this is C# but VB.Net is practically the same syntax.
JAKSupport
Oct 4th, 2006, 10:41 AM
How can you define nullability within an object? You cannot, therefore the ProductID or the Product object entity itself cannot have a nullable state,especially when it is bound.
Remember null is something but not nothing...actually null can include the universe..or not..or so...or nevermind, you get the idea.
PS: why do you use databindings ? Why not just use functions within your class that return / (set / get) statements to get values back and store them into a textbox control ?
Atheist
Oct 4th, 2006, 10:44 AM
if youre aware that this is c# code, then why dont you post in c# forum? ;)
JAKSupport
Oct 4th, 2006, 10:48 AM
His issue can apply to any programming language, it is more of OOP fundamentals than it is to a programming language in general.
RobDog888
Oct 4th, 2006, 10:49 AM
Moved.
JAKSupport
Oct 4th, 2006, 11:32 AM
Didnt need to be moved but umm ok..
His question is not language specific.
RobDog888
Oct 4th, 2006, 11:35 AM
I understand that but having threads in their appropriate language forum aids in searches. What if you had an issue and searched in a specif .net forum but the solution wasnt posted in the appropriate forum, you wouldnt get it listed in your hit list. ;)
JAKSupport
Oct 4th, 2006, 11:41 AM
Keyword search...
RobDog888
Oct 4th, 2006, 11:49 AM
If you do an advanced search and get more then 500 hits it wont show them all, limited to 500. so you would refine your search by limiting the forums searched in. ;)
This has been the policy from day one. :)
JAKSupport
Oct 4th, 2006, 11:50 AM
Policy smolishy...my answer was not geared to your policy it was geared towards the original poster, forget the politics people are interested in answers, not a smart alec :).
RobDog888
Oct 4th, 2006, 11:54 AM
I'm being completely professional here and as such we shouldnt take this thread anymore off topic. ;)
JAKSupport
Oct 4th, 2006, 11:55 AM
Likewise
tutus
Oct 4th, 2006, 01:37 PM
Please guys. it s all my fault. sorry about the confusion. I didn t mean to get discussions off .Net topics. pls accept my appology.
Let s talk technology.
Thanks for your time amigos.
Harsh Gupta
Oct 4th, 2006, 02:19 PM
The DataBindings.Add method of a control has 3 parameters,
Control.DataBindings.Add("Text_property_of_control", DataSetName, "TableName.ColumnName");
So TableName.ColumnName, basically the 3rd parameter cannot be null. So, when you dont initialise the variable, that parameter is null by default and hence cannot be accepted by the Add function. So either initialise the variable or set a value before binding it to any data.
When you bind the dataset to the control, column will give some value to that parameter so that the control could display it.
Also, use [New Binding] keyword.
Control.DataBindings.Add(New Binding("Text_property_of_control", DataSetName, "TableName.ColumnName"));
tutus
Oct 4th, 2006, 08:31 PM
why is it better to use .Add(New...... [B]Binding pls
Thanks
Harsh Gupta
Oct 5th, 2006, 02:33 AM
why is it better to use .Add(New...... [B]Binding pls
Thanks
Just a opinion. Most of the times, not using these keywords results in unexpected results.
Also, Databindings property returns a ControlBindingsCollection type, that contains the Binding objects for the control. I don't know how to explain it, but just like you use New keyword when dealing with Arraylists or some sort of collections (it is somewhat required), the same concept goes for this thing. No matter how may controls you bind, it will return the same ControlBindingsCollection.
jmcilhinney
Oct 5th, 2006, 02:53 AM
You don't need to use the "New Binding" bit. The ControlBindingsCollection.Add method is overloaded so you're actually calling two different methods. One is named "Add" and takes three arguments of types String, Object and String The other is also named "Add" and takes a single argument of type Binding. There are several other overloads of the Add method too. If you call the one that takes a Binding argument it just adds that Binding. If you call one of the others they just create a Binding object using the values you specify and then call the one that takes a Binding argument. It's exactly the same result regardless with the only difference being whether you want to explicitly create a Binding object or let the system create on implicitly.
tutus
Oct 14th, 2006, 07:44 PM
jmcilhinney, can u pls demistify the term overload for me. what s a simple definition for that in plain english (sorry i don t have strong software background)
thanks
jmcilhinney
Oct 14th, 2006, 07:51 PM
To overload a member means to provide two or more implementations with the same name but different signatures, i.e. different argument lists. Here (http://msdn2.microsoft.com/en-us/library/system.windows.forms.controlbindingscollection.add.aspx) is the overload list for the ControlBindingsCollection.Add method. There are seven overloaded implementations of the Add method. That means that you can call Add and pass a Binding object that you've already created or various combinations of values required to build a Binding object for you. The system is smart enough to know which overload to invoke based on the types of the parameters you pass when you call it. If your parameters are ambiguous, i.e. they could apply to more than one overload, then the compiler will throw an error and refuse to continue until you clarify the parameters such that they can apply to only one overload.
tutus
Oct 14th, 2006, 08:04 PM
Yes yes thanks for making the right association for me. I have seen that in MSDN documentation and had no idea what it s called. Now I got it. thank you
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.