Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts, i'm a newbie in C#. I wanna ask u about struct in C#. I've a problem about this one :

C#
class Program
  {
    static void Main(string[] args)
    {
      Add();
      Multiply();
      Console.ReadLine();
    }
    private static void Add()
    {
      S sd = new S();
      sd.data1.Add(0, 3);
      sd.data1.Add(1, 5);
      sd.bil1 = sd.data1[0];
      sd.bil2 = sd.data1[1];
      int hasil = sd.bil1 + sd.bil2;
      Console.WriteLine("Hasil penjumlahan : {0}", hasil);
    }
    private static void Multiply()
    {
      S sd = new S();
      sd.data1.Add(0, 6);
      sd.data1.Add(1, 2);
      sd.bil1 = sd.data1[0];
      sd.bil2 = sd.data1[1];
      int hasil = sd.bil1 * sd.bil2;
      Console.WriteLine("Hasil perkalian : {0}", hasil);
    }
    struct S
    {
      public IDictionary<int, int> data1 { get; set; }
      public int bil1;
      public int bil2;
    }
  }


There is an error occured :( , would u tell me how to use struct in good programming based on my problem above.. :( not based on my mistake.
Cause, i'm really want to understand about struct.. but in a good programming. Thank you.. :)
Posted
Updated 24-Aug-10 19:58pm
v3
Comments
Member 14375245 16-May-19 14:17pm
   
I have gizmox webgui installed for visual studio 2010 but when open form ask for licensing but there ia no way to continue as trial also. How can i start to use?

The error message normally gives a clue to the reason for the exception.

"Object reference not set to an instance of an object"
   
when u use data1 from struct S u must implement ur
interface variable .

acoording to ur code
when u use

"sd.data1.Add(0, 3);"
an exception messqage is SHOWN
"Object reference not set to an instance of an object."

so u must implement every interface variable of struct S at every use
of data1 data1
like

S sd = new S();

sd.data1=new Dictionary<int,int>();

sd.data1.Add(0, 3);



an so on for all variables
   
Comments
Teamsar Muliadi 25-Aug-10 22:18pm
   
Thank you Mohammad :)
Please, go to the following link to have a better understanding of structs

http://msdn.microsoft.com/en-us/library/ms173109.aspx[^]


http://msdn.microsoft.com/en-us/library/0taef578.aspx[^]
Please, let me know if you really want to know more... :)
   
v2
Comments
Teamsar Muliadi 25-Aug-10 2:54am
   
Hehehehe... you have just made a joke.. :D It's need much time to read one by one the article.. :) Would u make a simple sample for guiding me a good programming in struct based on my problmem ?.. hehehe.. :) Thank u.
senguptaamlan 25-Aug-10 3:24am
   
making a good programming needs an indepth knowledge of the programming construct that you want to use, with respect to memory, performance and scope..if you want to use struct effectively in a good way then you obviously need to invest some time..code project is not msdn guide which gives you How Tos of various programming language. I can easily give you 10 structure example, but deciding whether that particular implementation suits your requirement needs indept knowledge.
Teamsar Muliadi 25-Aug-10 5:36am
   
Thank u.. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900