473,241 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Limited Size String

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
Feb 15 '06 #1
9 10360
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michel Racicot" <mr******@hotmail.com> wrote in message
news:ek*************@tk2msftngp13.phx.gbl...
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

Feb 15 '06 #2
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" <mr******@hotmail.com> wrote in message
news:ek*************@tk2msftngp13.phx.gbl...
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

Feb 15 '06 #3
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" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <mr******@hotmail.com> wrote in message
news:ek*************@tk2msftngp13.phx.gbl...
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


Feb 15 '06 #4
Just create a new class that extends string. Override the .ToString()
method to pad or truncate as necessary.

Feb 15 '06 #5
<pr*****@gmail.com> wrote:
Just create a new class that extends string. Override the .ToString()
method to pad or truncate as necessary.


Except that String is sealed...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 15 '06 #6
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

Feb 15 '06 #7
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();
}

}

--
-Demetri
"mi**********@xquisoft.com" wrote:
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

Feb 16 '06 #8
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

Feb 16 '06 #9
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();
}

}

--
-Demetri
"mi**********@xquisoft.com" wrote:
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

Feb 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
13
by: cvh | last post by:
Hello, In delphi I could do this : Var Str: String (String of size 2char) How would I do this with VB.NET in CF? Thank you,
9
by: Protoman | last post by:
Why doesn't this display the string? string decrypt(const string& ciphertext,const string& key) { string decrypted; decrypted.resize(ciphertext.size()); // allocates the string for the correct...
0
by: Ken Varn | last post by:
I have a managed C++ assembly in which I need to interact with some 'C' APIs that take fixed size 'C' data blocks. I need to wrap these data blocks into a managed object. It seems like a lot of...
5
by: gopi2ks | last post by:
hi one and all i have so many controls on a form,but the form has a limited size,how to increase the size of a form more than a window size by using scroll bars ,to place all the controls in single...
10
by: John Brown | last post by:
Hi there, Does anyone know how to go about reading/writing a type to a file in a language (culture) independent way. For instance, let's say you're dealing with the native "System.Drawing.Size"...
3
by: mark4asp | last post by:
Stack of limited size containing unique items? Hi guys, How would I implement a stack of limited size containing unique items? For example. Suppose my stack has . I add 2 to it and it is now...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.