|
Hi,
That worked. thanks a lot. I have no syntax errors, however the problem crashes in the load_data() function when I attempt to insert a value into the array.
friends[i].name = name;
causes the following error:
Object reference not set to an instance of an object
|
|
|
|
|
Jerry Graham,
In your code, you only create an array, you dont create any instances. So, you need to do:
friendType myFriend = new friendType();
myFriend.name = "bob";
friends[i] = myFriend;
On a side note, i'd personnly change your array to a generic list. eg:
List-friendType- friendArray = new List-friendType-;
(Change the "-" to "<" and ">", it wouldnt let me add them, guess CP doesnt allow for naughty HTML to be inserted)
This is because as you create the array, you set its size to be 10, now if you always populate that array with 10 friends, then its fine. But what if you want 11, your array doesn't handle that or if you only have 9 friends, then you have a null item. Just a tip.
Also, its a good idea to follow the standard naming convention.
http://msdn2.microsoft.com/en-us/library/ms229045.aspx[^]
Regards,
Gareth.
modified on Tuesday, March 25, 2008 3:17 PM
|
|
|
|
|
I guess what is a bit puzzling to me is that this same code works if I use a single main function. it is only after I extract the reading of the data file into the load_data() function that these problems occur.
|
|
|
|
|
Your suggestion worked. However could you tell me why this works?
--------------------------------------------------------------------------------------------------------------------using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace friendstruct
{
class Program
{
struct friendType
{
public string name;
public string age;
}
public static void Main(string[] args)
{
int age;
string str_age;
string name;
friendType[] friends = new friendType[10];
//load the data from the data file
FileStream file = new FileStream("Marilia.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(file);
for (int i = 0; i<3; i++)
{
name = reader.ReadLine();
str_age = reader.ReadLine();
age = Convert.ToInt16(str_age);
friends[i].name = name;
friends[i].age = str_age;
}
reader.Close();
//print the data file out
for (int i = 0; i < 3; i++)
Console.WriteLine("{0} {1} ", friends[i].name, friends[i].age);
}
}
}
-------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
When you create an array of structures, the array contains the instances of the structure.
When you create an array of objects, the array contains references to the objects. All references are null when you create the array, so you have to create the instances and assign the references to the array items.
Structures are tricky to use, especially if they are mutable, as your structure is. Unless you have a really good reason to use a structure, just keep to using classes.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thank you! I guess C# struct/class relationship is different from c++ class/struct.
|
|
|
|
|
Jerry Graham wrote: I guess C# struct/class relationship is different from c++ class/struct.
It certainly is. In C++ the struct is just a definition, and how you use it decides if it becomes a value type or a reference type. In C#, a struct is always a value type and a class is always a reference type.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hello,
I have a web app where I am able to authenticate logins against Active Directory locally. However when I deploy the application to the production server and try to authenticate it says "The server is not operational." Can anyone please advise what I'm doing wrong?...Thanks in advance.
|
|
|
|
|
Hi All
Have spent all day trying to extract data from any one of the ready made containers that are easily bound to a SQL source without any success, this has been extremely frustrating.
I would really appreciate some help and a very simple step by step explanation of how to resolve this issue.
This is what I am trying to achieve - Steps 3 & 4 are not a problem.
Step1, Get 1 value from a container (bs_bond_price)
Step2, Stuff the value into a local variable
Step3, Get 1 value from a user textbox external to the container
Step4, Multiply value 1 by value 2 and show in a label
Example
----------------------------------------------------------------------------------
<asp:DataList ID="DataList1" runat="server" DataKeyField="bs_bond_price" DataSourceID="test"
Style="z-index: 108; left: 16px; position: absolute; top: 394px" OnSelectedIndexChanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
bs_bond_price:
<asp:Label ID="bs_bond_priceLabel" runat="server" Text='<%# Eval("bs_bond_price") %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
------------------------------------------------------------------------------------
I would really appreciate some help
many thanks in advance
Regards
DD
modified on Tuesday, March 25, 2008 1:47 PM
|
|
|
|
|
I'd recommend asking this in the ASP.NET forum.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks for coming back
Although:
1, I need advice on how to get data values from C# objects (hence why I asked here)
2, I have not got a clue where you got that function name from. There are no Functions/Methods in the ASP example I gave (the reason for giving the ASP.NET example by the way is all C# objects are noted here)
So I resume my request for somebody to help me please
As I mentioned previously I really want to get a data value from a container and use it externally of the container.
Regards
DD
|
|
|
|
|
1 - you're using ASP.NET, and unless you're wanting to do what you're talking about on the server, you want to do it with javascript. It's not clear from your post, but, either way, it's an ASP.NET question.
2 - what function ? You're not thinking my sig block is part of my answer, are you ?
If you can explain yourself a little more clearly, do you want to set up a textbox so that it has a specific value for the lifetime of the page, or do you want the value to change as they user interacts with the page ?
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
dropped you a mail with some more details on
and yes I was being a complete plank and got your confused with your signblock - been a log day and frustrating day so far. ![Frown | :(](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_frown.gif)
|
|
|
|
|
OK - I am not at home right now, I'll look at it when I get home. But, it might make more sense in the meantime to post the details here, someone else may well be able to jump in and help
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Christian
Thanks for the reply earlier.
I have managed to fathom out the code which was
x = gridview1.Rows[0].Cells[0].Text;
I had seen something like this earlier when I had been scanning the web, however I thought I would do something simple and test it on a new webpage and sure enough got it working
cheers
DD
|
|
|
|
|
Hi there,
I am looking for a way to retrieve all the Requests that a browser makes after we invoke the WebRequest.Create("http://www.cnn.com"). Within a website, once we invoke the URL, there are many requests has been made from the client to the server, for images, java scripts, css, flash files etc.
I do want to get all those Request object to do some programming.
Any help plz.
Thanks
|
|
|
|
|
|
Hey Guyz
Ive had some troubles with Application.Exit() , I sometimes call it but the application doesnt exit. Ive Rectified this by calling Environment.Exit(0); instead
note that im sending 0 as the exit code, i have NO idea what this is or why its there, nevermiond the implications of it. I tried different numbers aswell and it also worked.
Im scared that this comes back to bite me in the ass once we've shipped so what do you guys think of calling Environment.Exit(0); ?
thanx
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
HarveySaayman wrote: note that im sending 0 as the exit code, i have NO idea what this is or why its there, nevermiond the implications of it.
Did it not occur to you to check the documentation[^]?
Its basically the exit code passed to the OS.
A simple google search can answer your question on the difference, but in brief your app will need permission to run unmanaged code in order to call Environment.Exit. I would suggest that Application.Exit is the better/safer to use and the reason you're having problems with it is probably to do with some shabby threading code.
|
|
|
|
|
what is this "documentation" thing of which you speak?
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
|
I would have the form call its Close() method.
|
|
|
|
|
Hi,
the exitCode parameter is the value returned to the process that launched your app;
it is the same value good old C/C++ programs could return in their main function.
If you launch from a .bat file you can get its value, and use it to signal a
success/error situation with zero/nonzero.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
A C# program's main can return a value too.
|
|
|
|
|
Hi PIEBALD,
thanks for pointing this out.
int main indeed returns an exitCode, and it will override a possible prior assignment
to Environment.ExitCode
I was not aware of this, basically because I did not need it so far, and Visual Studio
generates a void main for free, not an int one (for "better performance"!?)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|