RubenPieters

i typed this: (dno if it works yet tho, was just trying it out)

object Timo = Database1DataSet.Tables[1].Rows[1].ItemArray[2];

in a buttonclick event but there is an error that says: An object reference is required for the nonstatic field, method, or property Test.Database1DataSet.Tables.get

what is an object reference

thx in advance



Re: Visual C# Express Edition object reference?

nobugz

Database1DataSet is probably a class, not an instance of a DataSet. You'll need to create the instance with the "new" keyword, initialize it, then you can access its properties. The only time you can use the class name in a reference is when the member is static (MessageBox.Show() for example).





Re: Visual C# Express Edition object reference?

RubenPieters

and how do i create an instance of the dataset :x



Re: Visual C# Express Edition object reference?

Figo Fei - MSFT

Hi,

Just like creating other objects' class instance, DataSet ds =  new DataSet();






Re: Visual C# Express Edition object reference?

herenvardo

Assuming that Database1DataSet is a class (and if the IDE is showing it in a different color, it most probably is), you would do something like this:

Database1DataSet myInstance = new Database1DataSet();

A bit of theory here, to let you understand it better:

Classes are a mean to create custom data types that are not already in the language. When you declare a class, you are actually adding a new data type to the already available ones. However, it'd be a pain for the compiler to figure out how much memory will need such type to store its value, so it's dinamically stored. This means that when you need to create an object, memory is assigned to it on-the-fly, just when the object is being created, and your variable will actually store just a reference of the object (this is, the address where it's stored in the physicall memory in the user's machine). This is what is called an object reference. To ask the runtime for memory for an object you need, in C# you use the 'new' keyword, followed by the class of which you want to create an instance, and then the parenthesis containing a list of parameters to be passed to its constructor (I'll get to this later). One of the best and more attractive features of C# is that it takes care of the memory for you. Once your code doesn't need the object at all (technically, when there is no reference pointing to it), the .NET runtime will mark that memory as available to be used by other objects and variables, and even other applications running in Windows.

It's important to know when your variables hold a reference and when they hold the actual value, because assignation works a bit different. For example, if you have these lines:

object a = new MyObject();
object b = a;

If you make any alteration to the object 'b', it will show up in 'a', because they are actually two references of the same object. On the other hand, if you have:
int a=5;
int b=a;
and then do this:
b++;
the value of 'b' will be 6, but 'a' will still be 5. ints are not handled as references, because the compiler knows pretty well that they need 32 bits of memory and can assign it at compile time. In a more general basis:

Structs (including most numeric types and chars) and enumerations (enum) are value types, while classes, interfaces and delegates are reference types. You might learn more about each at http://msdn2.microsoft.com/en-us/library/s1ax56ch.aspx and http://msdn2.microsoft.com/en-us/library/490f96s2.aspx. The key difference among them, quoting the reference, is:

Variable that are based on value types directly contain a values. Assigning one value type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself.

The constructor is a speciall method inside a class which is called when a new object is created based on that class. As it's a method, you may overload it and provide diferent versions which take different parameters (either different quantity or different type, so the compiler and the runtime can figure out which one are you calling each time). If you don't provide any constructor, the C# compiler will make a generic one for you, which takes no parameters. This might seem great, but it can lead to errors if you are not careful. If you provide any constructor, even if there is any parameter-less one, the compiler won't make any, and hence there will be no parameter-less constructors for that class. This goes worse when you inherit that class, since the implicit constructors call the parameter-less constructor of the base class. and there is not such one in this case, so you'd get an error unless it explicitly calls one of the existing constructors. However, if you're asking help to create an instance from a class, I may assume that you are not messing with constructors... yet ;)

A good thing, when you have to call a constructor, is that as soon as you write the ( the IDE will show you the different available constructors and the parameters they take. For the name of your class, I guess you haven't created it, but it has been autogenerated by the IDE when using some of the data access controls available. It will probably have a parameterless constructor, so you can safely use the line I put in the beginning of the post. If it requires parameters, however, you will need to figure out which parameters does it require and provide them: the names and types (which will appear as soon as you type the '(' ) are usualy good clues. If you need further help, you can just select a relevant identifier on your code and press F1. The help available in Visual Studio is very complete and useful, although the first times you use it you might get lost due to the huge ammount of information it contains. If you're unable to find the answers you need among it, I'll be glad to help you again ;)





Re: Visual C# Express Edition object reference?

RubenPieters

rly nice, ty :)



Re: Visual C# Express Edition object reference?

RubenPieters

hmm now it says that the row doesnt exist but how do i know what i need to type after .Rows



Re: Visual C# Express Edition object reference?

herenvardo

What code you have to type depends on what do you actually want to do... this is pretty obvious.

However, by how your code looks, it seems that you are just experimenting or trying out the data access features of the language... Is there actual data in the database you are using If you try to read the rows from a table that has no rows, it will obviously fail. Also, remember that indexing arrays, collections and similar structures always begins by 0. If you want the first item of one of these structures, you have to put [0]. If you put [1], you'll be accessing the second one. So, if you have only one row, trying to get Rows[1] will fail, because you'll be asking for the second row, which doesn't exist.

These are the basic concepts. If you need more help, please give more details about what do you want to do, so we can better aid you in achieving it. The more we know about your intentions, the better we can support you, ie: we'll be able to try the code suggestions and verify that they work as expected before posting them (you know, we are all humans). In addition, try to keep one question per thread and make sure the thread's title is descriptive about your actual question, so those people who better know about the precise topic can identify the question and drop you some lines.