Limited Size String

M

Michel Racicot

In some language, it's possible to specify a "predefined" size for string
objects.

Exemple: string[10] which is a string of 10 characters.

Is it possible to do this in C#?

Thank you
 
N

Nicholas Paldino [.NET/C# MVP]

Michel,

No, it is not. As a workaround, you might want to use an array of
characters which is set to 10 characters. When you need a string, you can
pass the array to the constructor of the string class and it will create a
new string based on that array.

Hope this helps.
 
R

Ravi Ambros Wallau

A string object is imutable. Once a string is created, it cannot be changed.
string[10] is a vector of strings in C#...
You can create a vector of char, if this is what you want to do...

What do you plan to do?
 
M

Michel Racicot

That particular datatype will be used to represent a unique plugin ID.
The data is part of the header of a structure that will be sent via Sockets.
The communication protocol have to be sure that the sent header is always
the same fixed size (the body can have variying size as I pass along the
size of the content in the header also).

Of course, I can always pad or truncate the PluginID to 10 characters and
ensure that when the plugins are loaded, that the plugin id value in the XML
definition of the user plugin doesn't contain more that 10 characters...

Ravi Ambros Wallau said:
A string object is imutable. Once a string is created, it cannot be
changed.
string[10] is a vector of strings in C#...
You can create a vector of char, if this is what you want to do...

What do you plan to do?

Michel Racicot said:
In some language, it's possible to specify a "predefined" size for string
objects.

Exemple: string[10] which is a string of 10 characters.

Is it possible to do this in C#?

Thank you
 
P

prempel

Just create a new class that extends string. Override the .ToString()
method to pad or truncate as necessary.
 
M

michael.lang

Create a PlugIn class with an Id property of type string. The get
and/or set accessors can ensure the proper length. You could also add
an IsValid property, or Validate(string id) method that the UI or other
id source can call to ensure a proper id is specified.

- Mike
 
G

Guest

Or....

class Program
{
[STAThread]
static void Main(string[] args)
{

String10 s = "0123456789";

Console.WriteLine("String: " + s);
Console.Read();

}

}

public struct String10
{
string value;

public String10(string value)
{
if (value.Length > 10) throw new ArgumentException("string must be 10
characters or less in length.","value");
this.value = value;
}

public static implicit operator String10(string s)
{
return new String10(s);
}

public override string ToString()
{
return value.ToString();
}

}
 
M

michael.lang

A potential problem I have with "String10" is that if you ever change
the rule for the length of a plugin id, is that string10 may not
reflect the actual rule anymore. Maybe you'll have to increase the
length of the Id someday to 12 (or more)? Then either you rename
string10 to string12 and all locations where it is used, or string10
really holds more characters that it leads you to believe.

- Mike
 
G

Guest

Well then call the class FixedString and modify it to allow for setting the
max length at runtime.

public struct FixedString
{
string value;

public FixedString(int maxLength,string value)
{
if (value.Length > maxLength) throw new ArgumentException("string must be
" + maxLength.ToString() + " characters or less in length.","value");
this.value = value;
}

public override string ToString()
{
return value.ToString();
}

}
 

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