defining an variable dimension in C# ?

B

Brian

First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold more
data in C# ?
 
A

Arne Vajhøj

First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

Arne
 
E

Ernst Sauer

Am 09.06.2013 17:07, schrieb Brian:
First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold more
data in C# ?

Look at

Array.Resize(...);

E.S.
 
B

Brian

Arne Vajhøj said:
First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

Arne

How do you access the aaa that has been stored? Can it be searched for?
 
M

mick

Arne Vajhøj said:
First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

Arne

How do you access the aaa that has been stored? Can it be searched for?

Arne Vajhøj said:
First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

Arne

How do you access the aaa that has been stored? Can it be searched for?

Yes you can either search for it or via its index

string result = lst.Find(i => i == "aaa");

or

string s = lst[0];

mick
 
J

Jeff Johnson

First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

At the risk of going massively off-topic, what "limits of Visual Basic" are
you talking about? Granted, we know about things like the lack of pointers
in VB, but I can't remember the last time I dropped into unsafe code in C#
to use pointers.

What I'm trying to say is that these days, the overlap of functionality
between VB.NET and C# is like--pulling a number out of my butt--99%. Do you
really need that 1% of functionality in your program? If not, and you're
already strong in VB*, I see no particular push to switch to C#.

Of course, if you just WANT to, that's another story. (Personally, I'm a VB
guy who switched to C# years ago because that's the standard where I work,
and I really like C# now and have no desire to go back to VB, so I could
understand that line of thinking.)


*Unless you mean you're coming from CLASSIC VB (version 6 or lower), in
which case it's a whole new world....
 
A

Arne Vajhøj

At the risk of going massively off-topic, what "limits of Visual Basic" are
you talking about? Granted, we know about things like the lack of pointers
in VB, but I can't remember the last time I dropped into unsafe code in C#
to use pointers.

What I'm trying to say is that these days, the overlap of functionality
between VB.NET and C# is like--pulling a number out of my butt--99%. Do you
really need that 1% of functionality in your program? If not, and you're
already strong in VB*, I see no particular push to switch to C#.

Of course, if you just WANT to, that's another story. (Personally, I'm a VB
guy who switched to C# years ago because that's the standard where I work,
and I really like C# now and have no desire to go back to VB, so I could
understand that line of thinking.)

*Unless you mean you're coming from CLASSIC VB (version 6 or lower), in
which case it's a whole new world....

That was how I read the question.

Collections is a basic part of VB.NET as well.

Arne
 
A

Arne Vajhøj

Arne Vajhøj said:
First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

How do you access the aaa that has been stored? Can it be searched for?

You can access as:

lst[0]
lst[1]

There is a ton of ways to search in and manipulate List<>.

If you explain how you want to search, then I can create an
example.

Arne
 
A

Arne Vajhøj

Arne Vajhøj said:
On 6/9/2013 11:07 AM, Brian wrote:
First of all I'm a Visual Basic programmer who is wondering if C#
can get
over some of the limits of Visual Basic so I hope I can make myself
clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim
var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold
more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

Arne

How do you access the aaa that has been stored? Can it be searched for?

Arne Vajhøj said:
On 6/9/2013 11:07 AM, Brian wrote:
First of all I'm a Visual Basic programmer who is wondering if C#
can get
over some of the limits of Visual Basic so I hope I can make myself
clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim
var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold
more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

How do you access the aaa that has been stored? Can it be searched for?

Yes you can either search for it or via its index

string result = lst.Find(i => i == "aaa");

Find is a very useful method.

I am just not convinced the above is a good example.
or

string s = lst[0];

Arne
 
B

Brian

Jeff Johnson said:
At the risk of going massively off-topic, what "limits of Visual Basic" are
you talking about? Granted, we know about things like the lack of pointers
in VB, but I can't remember the last time I dropped into unsafe code in C#
to use pointers.

What I'm trying to say is that these days, the overlap of functionality
between VB.NET and C# is like--pulling a number out of my butt--99%. Do you
really need that 1% of functionality in your program? If not, and you're
already strong in VB*, I see no particular push to switch to C#.

Of course, if you just WANT to, that's another story. (Personally, I'm a VB
guy who switched to C# years ago because that's the standard where I work,
and I really like C# now and have no desire to go back to VB, so I could
understand that line of thinking.)


*Unless you mean you're coming from CLASSIC VB (version 6 or lower), in
which case it's a whole new world....

As programming languages are developing with better ways of creating a
program its difficult to keep up to date. I did some research into finding
a way in VB Net Express, The free version of Visual Basic, so that I did
not need to define the number of data items that I wanted to load into a
variable...for example Dim Objectname(10) as string. ReDim seemed to be the
only way to extend any limit I had on defining the storage limit of an
array. It seemed bad programming to increase the array limit by 1 each time
the limit was exceeded.
So I started to look at other languages that were close to VB Net such as
C#. The other reason for my interest in this language is that I have been
programming in Visual Basic for a long time and wanted to try another
programming language and like you say you had no regrets in changing to C#.
 
B

Brian

Arne Vajhøj said:
Arne Vajhøj said:
On 6/9/2013 11:07 AM, Brian wrote:
First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

How do you access the aaa that has been stored? Can it be searched for?

You can access as:

lst[0]
lst[1]

There is a ton of ways to search in and manipulate List<>.

If you explain how you want to search, then I can create an
example.

Arne

A search in VB Net would normally involve a look and a comparing two
variables.

For example

For X = 1 to MaxObjects
If ObjectTest = ObjectName(X) Then
Carryout_Actions
Exit For
Next X
 
B

Brian

Arne Vajhøj said:
That was how I read the question.

Collections is a basic part of VB.NET as well.

Arne

Thanks Arne. I'll do some research in how to use collection for VB Net, as
C# and VB Nt are both written by Microsoft I can understand the things like
Collections being in both programming languages.
 
A

Arne Vajhøj

As programming languages are developing with better ways of creating a
program its difficult to keep up to date. I did some research into finding
a way in VB Net Express, The free version of Visual Basic, so that I did
not need to define the number of data items that I wanted to load into a
variable...for example Dim Objectname(10) as string. ReDim seemed to be the
only way to extend any limit I had on defining the storage limit of an
array. It seemed bad programming to increase the array limit by 1 each time
the limit was exceeded.
So I started to look at other languages that were close to VB Net such as
C#. The other reason for my interest in this language is that I have been
programming in Visual Basic for a long time and wanted to try another
programming language and like you say you had no regrets in changing to C#.

I believe that collections more specifically List<> aka List(Of) is
what you are looking for.

Arne
 
A

Arne Vajhøj

Thanks Arne. I'll do some research in how to use collection for VB Net, as
C# and VB Nt are both written by Microsoft I can understand the things like
Collections being in both programming languages.

Technically they are not in either C# or VB.NET language but in the
..NET framework which is shared by C# and VB.NET.

Arne
 
A

Arne Vajhøj

Arne Vajhøj said:
On 6/9/2013 11:07 AM, Brian wrote:
First of all I'm a Visual Basic programmer who is wondering if C# can get
over some of the limits of Visual Basic so I hope I can make myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to contain 10
items then is the variable dimension automatically adjusted to hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

How do you access the aaa that has been stored? Can it be searched for?

You can access as:

lst[0]
lst[1]

There is a ton of ways to search in and manipulate List<>.

If you explain how you want to search, then I can create an
example.

A search in VB Net would normally involve a look and a comparing two
variables.

For example

For X = 1 to MaxObjects
If ObjectTest = ObjectName(X) Then
Carryout_Actions
Exit For
Next X

In C# and VB.NET you could use the Find method with
a delegate/lambda that compares a property of each value with
what you are looking for.

Arne
 
A

Arne Vajhøj

Arne Vajhøj said:
On 6/12/2013 10:06 AM, Brian wrote:
On 6/9/2013 11:07 AM, Brian wrote:
First of all I'm a Visual Basic programmer who is wondering if C#
can get
over some of the limits of Visual Basic so I hope I can make
myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim
var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to
contain 10
items then is the variable dimension automatically adjusted to
hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

How do you access the aaa that has been stored? Can it be searched for?

You can access as:

lst[0]
lst[1]

There is a ton of ways to search in and manipulate List<>.

If you explain how you want to search, then I can create an
example.

A search in VB Net would normally involve a look and a comparing two
variables.

For example

For X = 1 to MaxObjects
If ObjectTest = ObjectName(X) Then
Carryout_Actions
Exit For
Next X

In C# and VB.NET you could use the Find method with
a delegate/lambda that compares a property of each value with
what you are looking for.

Example:

using System;
using System.Collections.Generic;

namespace E
{
public class Data
{
public int A { get; set; }
public string B { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
List<Data> lst = new List<Data>();
lst.Add(new Data { A=123, B="ABC" });
lst.Add(new Data { A=456, B="DEF" });
Data d123 = lst.Find(d => d.A==123);
Console.WriteLine(d123.B);
Data ddef = lst.Find(d => d.B=="DEF");
Console.WriteLine(ddef.A);
Console.ReadKey();
}
}
}

Arne
 
A

Arne Vajhøj

On 6/12/2013 10:06 AM, Brian wrote:
On 6/9/2013 11:07 AM, Brian wrote:
First of all I'm a Visual Basic programmer who is wondering if C#
can get
over some of the limits of Visual Basic so I hope I can make
myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim
var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to
contain 10
items then is the variable dimension automatically adjusted to
hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

How do you access the aaa that has been stored? Can it be searched
for?

You can access as:

lst[0]
lst[1]

There is a ton of ways to search in and manipulate List<>.

If you explain how you want to search, then I can create an
example.

A search in VB Net would normally involve a look and a comparing two
variables.

For example

For X = 1 to MaxObjects
If ObjectTest = ObjectName(X) Then
Carryout_Actions
Exit For
Next X

In C# and VB.NET you could use the Find method with
a delegate/lambda that compares a property of each value with
what you are looking for.

Example:

using System;
using System.Collections.Generic;

namespace E
{
public class Data
{
public int A { get; set; }
public string B { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
List<Data> lst = new List<Data>();
lst.Add(new Data { A=123, B="ABC" });
lst.Add(new Data { A=456, B="DEF" });
Data d123 = lst.Find(d => d.A==123);
Console.WriteLine(d123.B);
Data ddef = lst.Find(d => d.B=="DEF");
Console.WriteLine(ddef.A);
Console.ReadKey();
}
}
}

Translated to VB.NET:

Imports System
Imports System.Collections.Generic

Namespace E
Public Class Data
Public Property A() As Integer
Get
Return m_A
End Get
Set
m_A = Value
End Set
End Property
Private m_A As Integer
Public Property B() As String
Get
Return m_B
End Get
Set
m_B = Value
End Set
End Property
Private m_B As String
End Class
Public Class Program
Public Shared Sub Main(args As String())
Dim lst As New List(Of Data)()
lst.Add(New Data() With { .A = 123, .B = "ABC" })
lst.Add(New Data() With { .A = 456, .B = "DEF" })
Dim d123 As Data = lst.Find(Function(d) d.A = 123)
Console.WriteLine(d123.B)
Dim ddef As Data = lst.Find(Function(d) d.B = "DEF")
Console.WriteLine(ddef.A)
Console.ReadKey()
End Sub
End Class
End Namespace

Arne
 
B

Brian

Arne Vajhøj said:
On 6/12/2013 10:35 PM, Brian wrote:
On 6/12/2013 10:06 AM, Brian wrote:
On 6/9/2013 11:07 AM, Brian wrote:
First of all I'm a Visual Basic programmer who is wondering if C#
can get
over some of the limits of Visual Basic so I hope I can make
myself clean
on what I would like to know.

Do you need to define a space for variables in C# such as Dim
var(10) as
string so you can store 10 strings
Example var(1) = "String 1" etc?

If you have 12 items but have only dimensioned variable to
contain 10
items then is the variable dimension automatically adjusted to
hold more
data in C# ?

Not for arrays but collections has this ability.

string[] a - new string[2];
a[0] = "aaa";
a[1] = "bbb";

but:

List<string> lst = new List<string>();
lst.add("aaa");
lst.add("bbb");

How do you access the aaa that has been stored? Can it be searched
for?

You can access as:

lst[0]
lst[1]

There is a ton of ways to search in and manipulate List<>.

If you explain how you want to search, then I can create an
example.

A search in VB Net would normally involve a look and a comparing two
variables.

For example

For X = 1 to MaxObjects
If ObjectTest = ObjectName(X) Then
Carryout_Actions
Exit For
Next X

In C# and VB.NET you could use the Find method with
a delegate/lambda that compares a property of each value with
what you are looking for.

Example:

using System;
using System.Collections.Generic;

namespace E
{
public class Data
{
public int A { get; set; }
public string B { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
List<Data> lst = new List<Data>();
lst.Add(new Data { A=123, B="ABC" });
lst.Add(new Data { A=456, B="DEF" });
Data d123 = lst.Find(d => d.A==123);
Console.WriteLine(d123.B);
Data ddef = lst.Find(d => d.B=="DEF");
Console.WriteLine(ddef.A);
Console.ReadKey();
}
}
}

Translated to VB.NET:

Imports System
Imports System.Collections.Generic

Namespace E
Public Class Data
Public Property A() As Integer
Get
Return m_A
End Get
Set
m_A = Value
End Set
End Property
Private m_A As Integer
Public Property B() As String
Get
Return m_B
End Get
Set
m_B = Value
End Set
End Property
Private m_B As String
End Class
Public Class Program
Public Shared Sub Main(args As String())
Dim lst As New List(Of Data)()
lst.Add(New Data() With { .A = 123, .B = "ABC" })
lst.Add(New Data() With { .A = 456, .B = "DEF" })
Dim d123 As Data = lst.Find(Function(d) d.A = 123)
Console.WriteLine(d123.B)
Dim ddef As Data = lst.Find(Function(d) d.B = "DEF")
Console.WriteLine(ddef.A)
Console.ReadKey()
End Sub
End Class
End Namespace

Arne

Thanks for that Arne. It will be useful to study.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top