Click here to Skip to main content
15,907,000 members
Home / Discussions / C#
   

C#

 
AnswerRe: about tokens Pin
Judah Gabriel Himango25-Oct-06 5:11
sponsorJudah Gabriel Himango25-Oct-06 5:11 
QuestionHow to check all the check boxes in a listview from the start. Pin
Support12325-Oct-06 0:34
Support12325-Oct-06 0:34 
AnswerRe: How to check all the check boxes in a listview from the start. Pin
snorkie25-Oct-06 2:25
professionalsnorkie25-Oct-06 2:25 
AnswerRe: How to check all the check boxes in a listview from the start. Pin
Shy Agam25-Oct-06 2:31
Shy Agam25-Oct-06 2:31 
GeneralRe: How to check all the check boxes in a listview from the start. Pin
Support12325-Oct-06 2:39
Support12325-Oct-06 2:39 
QuestionChanging windows service account Pin
kulile25-Oct-06 0:32
kulile25-Oct-06 0:32 
Questiondrive formating for USB Stick (2nd try) Pin
Martin#25-Oct-06 0:15
Martin#25-Oct-06 0:15 
QuestionRe-implementing string object - Binding issues Pin
GottaseeMe24-Oct-06 23:47
GottaseeMe24-Oct-06 23:47 
Hi all

I've re implemented the string object so the compareto method can detect numbers in a string and sort correctly.

sorted string values normaly appear like
test1
test10
test2
test20
test3

My mthod will sort like the following:
test1
test2
test3
test10
test20
test30

The only problem i have with it is that i cannot bind the object.

I've appended the code below:
using System;

namespace SortableString
{

using System;
using System.ComponentModel;

///
/// Summary description for SortableString.
///


#region SortableString

///
/// Serves as a container to String with the added ability to represent nulls.
///

[System.Diagnostics.DebuggerStepThrough()]
[Bindable(true)]
[Category("Data")]
[RefreshProperties(RefreshProperties.All)]
public struct SortableString : IComparable, IFormattable, IEditableObject
{
///
/// Represents a null String.
///

public static readonly SortableString Null;

// Memeber variables
private string m_Value;
private bool m_HasValue;
private bool inTxn;
private string backupData;

#region Constructors

///
/// Constructor to create a SortableString with an actual String value.
///

/// <param name="value" />
public SortableString(string value)
{
m_Value = value;
backupData = value;
m_HasValue = true;
inTxn = false;
}

///
/// Constructor to create a SortableString from another one.
///

/// <param name="obj" />
public SortableString(SortableString obj)
{
// Copy the internal members from the other object.
this.m_Value = obj.m_Value;
this.m_HasValue = obj.m_HasValue;
this.backupData = obj.backupData;
inTxn = false;
}

#endregion

#region Public properties

///
/// Exposes the actual String value.
/// This property will throw an exception when null.
///

public string Value
{
get
{
// Is there a value?
if (m_HasValue)
{
// Return the value
return m_Value;
}
else
{
// The object represents null, throw an exception.
throw new InvalidOperationException("Cannot provide a value when null.");
}
}
set
{
// Set the value
m_Value = value;
m_HasValue = true;
}
}

///
/// Indicates whether or not the value represents null.
///

public bool IsNull
{
// IsNull is the opposite of HasValue
get {return !m_HasValue;}
set {m_HasValue=!value;}
}

///
/// Readonly. Indicates whether the nullable type has a value (not null).
///

public bool HasValue {get {return m_HasValue;}}

///
/// Exposes the value for binding consumers.
/// The object in this property can be either null or an actual String value.
/// Trying to set the property to anything else would throw an exception.
///

public object BindableValue
{
get
{
// Return null for Nulls or the actual String value otherwise.
if (m_HasValue)
return m_Value;
else
return null;
}
set
{
// Consumer setting the value to null.
if (value==null || value==System.DBNull.Value)
SetToNull();
else
{
try
{
// Set the value
m_Value = (string)value;
m_HasValue = true;
}
catch (Exception ex) {throw new InvalidOperationException("SortableString: BindableValue only accept null or a String.", ex);}
}
}
}

///
/// Exposes the value for working with data objects.
/// The object in this property can be either DBNull.Value or an actual String value.
/// Trying to set the property to anything else would throw an exception.
///

public object DataValue
{
get
{
// Return null for Nulls or the actual String value otherwise.
if (m_HasValue)
return m_Value;
else
return DBNull.Value;
}
set
{
// Consumer setting the value to null.
if (value==DBNull.Value)
SetToNull();
else
{
try
{
// If not null, accept only valid String values.
m_Value = (string)value;
m_HasValue = true;
}
catch (Exception ex) {throw new InvalidOperationException("SortableString: DataValue only accept DBNull.Value or a String.", ex);}
}
}
}

///
/// Returns the value if one exists or a default value otherwise.
/// This property will always return a value and never throw an exception.
///

public string SafeValue
{
get
{
// Do we have a value?
if (this.m_HasValue)
return this.m_Value; // Return the real value
else
return string.Empty; // Return a default value
}
set
{
// Set the value
m_Value = value;
m_HasValue = true;
}
}

#endregion

#region Casting operations

///
/// Implicit cast from a string to a SortableString.
/// Note: The cast is implicit because we never lose information when converting a string to a SortableString.
///

/// <param name="value" />
/// <returns>
public static implicit operator SortableString(string value)
{
return new SortableString(value);
}

public static implicit operator string (SortableString value)
{
return value.Value;
//return new SortableString(value.SafeValue);
}

///
/// Explicit cast from SortableString to a string.
/// Note: The cast is explicit because it isn't always possible to convert a SortableString to a string.
/// When the SortableString represents null trying to cast it to a string will throw an exception.
///

/// <param name="value" />
/// <returns>
// public static explicit operator string(SortableString value)
// {
// if (value.m_HasValue)
// {
// // Otherwise, return the actual value.
// return value.m_Value;
// }
// else
// {
// return string.Empty;
// }
// }

#endregion

#region Methods

///
/// Sets the value to null.
///

public void SetToNull() {this.m_HasValue = false;}

public override int GetHashCode()
{
// Have we got a value?
if (this.m_HasValue)
{
// Otherwise use the value's hash code.
return this.m_Value.GetHashCode();
}
else
{
// This represents null so always return 0 (because the value is then irrelevant).
return 0;
}

}

///
/// Check two SortableString objects for eqaulity.
///

/// <param name="obj" />
/// <returns>
public override bool Equals(object obj)
{
// Is the provided object of the right type?
if (obj is SortableString)
{
// Are they equal (both null or both have the same value)?
return this.DataValue.Equals(((SortableString)obj).DataValue);
}
else
{
// The object is of the wrong type, cannot compare.
return false;
}
}

public override string ToString() { return ToString(null, null); }

public string ToString(String format, IFormatProvider fp)
{
//Ignore any formatting
return Value;
}

#endregion

#region IComparable implementation.

public static int Compare(SortableString obj1, SortableString obj2)
{
return obj1.CompareTo(obj2);
}

public int CompareTo(object obj)
{
//Check if Equal
if (obj.ToString() == Value) return 0;


//Format the strings so the numbers are padded with zeros
string s2 = ConvertToNumberedString(obj.ToString());
string s1 = ConvertToNumberedString(Value);

//Now check which ones greater.
return s1.CompareTo(s2);
}

///
/// Will Format a string with numbers so the numbers have leading zeros.
/// eg "this10" becomes "this00010"
/// eg "this1Is2Good" becomes "this00001Is00002Good"
///

/// <param name="s" />
/// <returns>
string ConvertToNumberedString(string s)
{
string op = string.Empty;
string sNum = string.Empty;


for (int i = 0; i < s.Length; i++)
{
//If Digit in Character then add the character to the number field.
if (char.IsDigit(s,i))
sNum = sNum + s[i].ToString();
else
{
//If we have numbers in the number field Format them.
if (sNum.Length > 0)
{
op = op + sNum.PadLeft(5,'0');
sNum = string.Empty; //Clear the number field incase of more than one group of digits.
}

op = op + s[i].ToString();
}
}

//Flush any remaining numbers at the end of the text
if (sNum.Length > 0)
op = op + sNum.PadLeft(5,'0');

//return the string back.
return op;
}

#endregion

#region IEditable Object Implementaion

// Implements IEditableObject
void IEditableObject.BeginEdit()
{
if (!inTxn)
{
this.backupData = m_Value;
inTxn = true;
}
}

void IEditableObject.CancelEdit()
{
if (inTxn)
{
this.m_Value = backupData;
inTxn = false;
}
}

void IEditableObject.EndEdit()
{
if (inTxn)
{
backupData = new SortableString();
inTxn = false;
}
}


#endregion


}//end class SortableString

#endregion // SortableString
}


using System;

namespace SortableString
{
///
/// Summary description for Class1.
///

public class C1
{
private string m_string1;
private string m_string2;
private SortableString m_sString;

public C1()
{
//Set the conttrols
m_sString = string.Empty;
m_string1 = string.Empty;
m_string2 = string.Empty;
}


public string String1
{
get { return m_string1; }
set { m_string1 = value; }
}

public string String2
{
get { return m_string2; }
set { m_string2 = value; }
}


public SortableString SS
{
get { return m_sString; }
set { m_sString = value; }
}
}
}

added a new form and create an new instance of c1 then bind value ss to a text box and try editing the value.

If anyone can tell me where i'm going wrong or can point me to a useful example or topic that would be great.

Thank you
QuestionCom Type VT_STREAM in .net 2.0 Pin
chris_do24-Oct-06 22:41
chris_do24-Oct-06 22:41 
Questionhowto: pass parameters to a webservice from browser address bar Pin
baajhan24-Oct-06 22:40
baajhan24-Oct-06 22:40 
AnswerRe: howto: pass parameters to a webservice from browser address bar Pin
chris_do24-Oct-06 23:45
chris_do24-Oct-06 23:45 
QuestionRe: howto: pass parameters to a webservice from browser address bar Pin
baajhan24-Oct-06 23:55
baajhan24-Oct-06 23:55 
AnswerRe: howto: pass parameters to a webservice from browser address bar Pin
chris_do25-Oct-06 0:11
chris_do25-Oct-06 0:11 
QuestionSerialization of a large dataset Pin
TeamWild24-Oct-06 22:40
TeamWild24-Oct-06 22:40 
AnswerRe: Serialization of a large dataset Pin
V.25-Oct-06 0:11
professionalV.25-Oct-06 0:11 
QuestionRandom numbers Pin
Jad Jadallah24-Oct-06 22:20
Jad Jadallah24-Oct-06 22:20 
AnswerRe: Random numbers Pin
quiteSmart24-Oct-06 22:40
quiteSmart24-Oct-06 22:40 
AnswerRe: Random numbers Pin
J4amieC24-Oct-06 22:41
J4amieC24-Oct-06 22:41 
AnswerRe: Random numbers Pin
V.25-Oct-06 0:08
professionalV.25-Oct-06 0:08 
QuestionHelp me to implement the GridView RowCommand method. Pin
chandu4codeproj24-Oct-06 22:15
chandu4codeproj24-Oct-06 22:15 
QuestionUtf-8 xml string or removing encoding="..." Pin
Csupor Jenő24-Oct-06 22:08
Csupor Jenő24-Oct-06 22:08 
AnswerRe: Utf-8 xml string or removing encoding="..." Pin
Csupor Jenő24-Oct-06 22:38
Csupor Jenő24-Oct-06 22:38 
Questioncount specific character in string Pin
Vipin.d24-Oct-06 21:05
Vipin.d24-Oct-06 21:05 
AnswerRe: count specific character in string Pin
quiteSmart24-Oct-06 21:22
quiteSmart24-Oct-06 21:22 
AnswerRe: count specific character in string Pin
Stefan Troschuetz24-Oct-06 21:24
Stefan Troschuetz24-Oct-06 21:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.