I have created some nested classes but don't unterstand how to set the variables in the classes. My code so far only gives me a error:
System.NullReferenceException: Object reference not set to an instance of an object
Code:
class Felddaten
{
public string data;
}
class Feld
{
public string fieldName;
public Felddaten[] fieldData;
}
class Tabelle
{
public string tableName;
public Feld[] field;
}
class Program
{
static void Main(string[] args)
{
Tabelle table = new Tabelle();
table.tableName = "T100";
RFCConnector connector = new RFCConnector();
connector.getFieldNames(table.tableName, out List<string> fieldN);
table.field = new Feld[fieldN.Capacity];
for (int i = 0; i < fieldN.Capacity; i++)
{
table.field[0].fieldName = fieldN[0];
}
}
}
The error is at this line of code:
table.field[0].fieldName = fieldN[0];
You have only initialized the array table.field, not the ITEMS in the array. You need to initialize each item before you can access its members:
for(int i=0; i<table.field.Length; i++)
table.field[i] = new Feld();
Related
Here is a simplified version of my code:
class House
{
private string owner;
private int[] roomArea = new int[10];
public string Owner { get; set; }
public int[] RoomArea { get; set; }
}
class Program
{
static void Main(string[] args)
{
House[] london = new House[100];
for (int i = 0; i < 100; i++)
{
london[i] = new House();
}
london[0].Owner = "Timmy";
london[0].RoomArea[0] = 15; // Error points to this line
Console.WriteLine("Room 1 in " + london[0].Owner + "s house has the area of " + london[0].RoomArea[0] + "square meters");
}
}
I get the following error:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
I've looked at this this question/solution, yet I can't pinpoint what's exactly wrong with my code.
You need to initialize RoomArea. Even though you initialize inside the class it is creating it's own member , but in order to add values you need to initialize it
london[0].RoomArea = new int[10];
london[0].RoomArea[0] = 15;
Ok, so in a console application I'm working on, I have a list (myList) in Class01
class Class01
{
public List<string> myList = new List<string>();
public void _addsList()
{
myList.Add("0001Test01");
myList.Add("0002Test02");
myList.Add("0003Test03");
myList.Add("0004Test04");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(myList[i] + ",");
}
}
}
and I need to read that list in Class02
class Class02
{
public void _callList()
{
var class02 = new Class01();
string wits2;
List<string> buffer = new List<string>();
for (int i = 0; i < class02.myList.Count; i++)
{
wits2 = class02.myList[i].Substring(0, 4);
Console.WriteLine(class02.myList[i]);
}
Console.ReadKey();
}
}
The Output of this program should write this to the Console:
0001Test01,
0002Test02,
0003Test03,
0004Test04,
0001
0002
0003
0004
Now I've seen GetList used to do this
public class MyClass {
private List<string> myList = new List<string>();
public List<string> GetList()
{
return myList;
}
}
public class CallingClass {
MyClass myClass = new MyClass();
public void GetList()
{
List<string> calledList = myClass.GetList();
///More code here...
}
}
But I for the life of me can not get this to work. I don't know if I'm missing a namespace or what. I don't even know if GetList works in console application.
So I would really appreciate the help.
Thanks-
Though not totally clear from your question what is not working. But I suspect you need to put values in the list. If you mean it is not displaying anything in the console try this:
var class02 = new Class01();
string wits2;
class02._addsList();
EDIT: after reading your comment, I think this will remedy it:
for (int i = 0; i < class02.myList.Count; i++)
{
wits2 = class02.myList[i].Substring(0, 4);
Console.WriteLine(class02.myList[i]);
buffer.Add(wits2);//Add it to list declared in this function
}
NOTE: If you mean you are getting output when calling _addsList on class object the make sure that you have called this function once before using an object of Class01. From second block of code for Class02 you are not calling _addsList function on object of Class01.
On your Class02:
var class02 = new Class01();
class02._addsList(); //Add this on your declaration
string wits2;
Then change the variable you are passing on your Console.WriteLine:
List<string> buffer = new List<string>();
for (int i = 0; i < class02.myList.Count; i++)
{
wits2 = class02.myList[i].Substring(0, 4);
//Console.WriteLine(class02.myList[i]); //Remove this
Console.WriteLine(wits2); //Use wits2 instead since this is what you get on your Substring
}
I think most important thing you are missing is that your classes are not marked as public
so please add public before class declarations as below
public class Class02
{
......
One more thing, to get your required output, in your class02 you are assigning substring to 'wits' variable but not printing it. You are instead passing value from list to Console.Writeline.
Hope this helps
Thanks to TheVillageIdiot. I got this working now. So thank you. My code looks like this now:
public class Class01
{
public List<string> myList = new List<string>();
public void _addsList()
{
myList.Add("0001Test01");
myList.Add("0002Test02");
myList.Add("0003Test03");
myList.Add("0004Test04");
}
}
class Class02
{
public void _callList()
{
var class01 = new Class01();
class01._addsList(); //<--- Had to add this line
for (int i = 0; i < class01.myList.Count; i++)
{
Console.WriteLine(class01.myList[i] + ",");
}
string wits2;
List<string> buffer = new List<string>();
for (int i = 0; i < class01.myList.Count; i++)
{
wits2 = class01.myList[i].Substring(0, 4);
Console.WriteLine(wits2);
}
Console.ReadKey();
}
}
For toughs who didn't understand the problem, Class02 could not read the List in Class01.
Thanks for the Help!
I want to bind Foo.Data property and I need it to be two dimensional ObservableCollection. Since I don't want Data property to be changed outside of the Foo class I'm trying to expose it as ReadOnlyObservableCollection. But updating the encapsulated _data field don't update the Data property.
public sealed class Foo
{
private readonly ObservableCollection<ObservableCollection<string>> _data;
public Foo()
{
_data = new ObservableCollection<ObservableCollection<string>>();
// this line gives compile time error
// cannot convert from ObservableCollection<ObservableCollection<string>> to ObservableCollection<ReadOnlyObservableCollection<string>>
// Data = new ReadOnlyObservableCollection<ReadOnlyObservableCollection<string>>(_data);
// Fill the _data variable
for (var i = 0; i<10; i++)
{
var t = new ObservableCollection<string>();
for (var j = 0; j<10; j++)
{
t.Add(i + "+" + j);
}
_data.Add(t);
}
var tmp = new ObservableCollection<ReadOnlyObservableCollection<string>>();
foreach (var row in _data)
{
tmp.Add(new ReadOnlyObservableCollection<string>(row));
}
Data = new ReadOnlyObservableCollection<ReadOnlyObservableCollection<string>>(tmp);
}
public ReadOnlyObservableCollection<ReadOnlyObservableCollection<string>> Data { get; }
public void RemoveItem(int x, int y)
{
_data[x].RemoveAt(y); // This does update the Data collection.
}
public void RemoveRow(int x)
{
_data.RemoveAt(x); // This does not update the Data collection.
}
}
Here is a working sample.
FYI: If you want to edit or rerun the code, just click on fork.
I've searched the web about it but I couldn't find anything. Is not two dimensional ObservableCollections a thing, is there a better practice?
The RemoveRow method does not update the Data collection because _data is not related to Data (except that they share some content). The collection related to Data is tmp, so I would try the following:
Make tmp class member and give it a name (e.g. _rowData)
rename _data to _itemData
use _rowData in RemoveRow
This is the result:
public sealed class Foo
{
private readonly ObservableCollection<ObservableCollection<string>> _itemData;
private readonly ObservableCollection<ReadOnlyObservableCollection<string>> _rowData;
public Foo()
{
_itemData = new ObservableCollection<ObservableCollection<string>>();
// Fill the _itemData variable
for (var i = 0; i<10; i++)
{
var t = new ObservableCollection<string>();
for (var j = 0; j<10; j++)
{
t.Add(i + "+" + j);
}
_itemData .Add(t);
}
_rowData = new ObservableCollection<ReadOnlyObservableCollection<string>>();
foreach (var row in _itemData )
{
_rowData.Add(new ReadOnlyObservableCollection<string>(row));
}
Data = new ReadOnlyObservableCollection<ReadOnlyObservableCollection<string>>(_rowData);
}
public ReadOnlyObservableCollection<ReadOnlyObservableCollection<string>> Data { get; }
public void RemoveItem(int x, int y)
{
_itemData[x].RemoveAt(y);
}
public void RemoveRow(int x)
{
_rowData.RemoveAt(x);
}
}
I am trying to declare an array of buildings but the floors associated with each building are never initialised. I want to provide values to the data members for each instance of floor that is associated with each instance of building:
class Floor
{
public int number;
public int rooms;
//constructor
Floor()
{
rooms = 5;
number= 0;
}
}
class Building
{
public Floor[] floors= new Floor[6];
}
public partial class frmF2 : Form
{
Building[] x = new Building[7];
...
}
But it is very ugly.
class Building
{
Building()
{
floors = new Floor[6];
for(int i=0; i<6;++i)
floors[i] = new Floor();
}
public Floor[] floors;
}
I want to learn classes atm and here is what I came up with:
Class level to create a level. This class has an object array that fills itself with rooms (raeume) which is the other class. Now, I want to access the objects in the object array from room, after I inserted them. Here is what I want to type:
wohnung.rooms[i].raumname.toString();
Here are the two classes
class raum
{
static object[] o_nebenraum = new object[3]; //N-O-S-W
static string s_raumname = "";
public object[] nebenraume
{
get
{
return o_nebenraum;
}
set
{
o_nebenraum = value;
}
}
public string raumname
{
get
{
return s_raumname;
}
set
{
s_raumname = value;
}
}
}
class level
{
static object[] o_rooms = new object[100];
public object[] rooms
{
get
{
return o_rooms;
}
set
{
o_rooms = value;
}
}
}
Here is how I set everything up.
level wohnung = new level();
raum keller = new raum();
raum wohnzimmer = new raum();
raum kueche = new raum();
raum schlafzimmer = new raum();
wohnung.rooms[0] = keller;
wohnung.rooms[1] = wohnzimmer;
wohnung.rooms[2] = kueche;
wohnung.rooms[3] = schlafzimmer;
keller.raumname = "Keller";
wohnzimmer.raumname = "Wohnzimmer";
kueche.raumname = "Küche";
schlafzimmer.raumname = "Schlafzimmer";
for (uint i = 0; i < 3; i++)
{
Console.WriteLine("Wohnung beinhaltet jetzt " + *MISSING CODE PART, I WANT TO GET THE .raumname out of the object array from wohnung.room*);
}
Console.ReadKey();
You have to use generic typed list List<T> (See on MSDN) instead of array, in this case you'll have the indexed access for the typed list elements
So instead of:
static object[] o_rooms = new object[100];
public object[] rooms
Use:
static IList<raum> o_rooms = new List<Raum>(100);
public IList<raum> rooms
Try this(in the for Loop):
Console.WriteLine("Wohnung beinhaltet jetzt " + (wohnung.rooms[i] as raum).raumname );
You would be better off using generics though in which case the class level would now look like:
class level
{
static List<raum> o_rooms = new List<raum>();
public List<raum> rooms
{
get { return o_rooms; }
set { o_rooms = value; }
}
}
and the for loop can be replaced with a foreach loop as follows:
foreach(raum room in wohnung.rooms)
{
Console.WriteLine("Wohnung beinhaltet jetzt " + room.raumname );
}